이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ull unsigned long long
#define fi first
#define se second
#define ld long double
ll n, m, dist[30005];
vector<pair<ll, ll>>edge[30005];
struct struk{
ll b, p;
}; struk arr[30005];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1;i<=n;++i) dist[i]=4e18;
for(int i=1;i<=m;++i){
cin >> arr[i].b >> arr[i].p;
arr[i].b++;
}
for(int i=1;i<=m;++i){
for(int j=1;j<=m;++j){
if(j == i) continue;
if(abs(arr[j].b - arr[i].b) % arr[i].p == 0){
edge[i].pb({j, abs(arr[j].b - arr[i].b) / arr[i].p});
}
}
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>pq;
pq.push({0, 1});
dist[1]=0;
while(!pq.empty()){
ll gerak=pq.top().fi, idx=pq.top().se;
pq.pop();
for(auto x:edge[idx]){
if(dist[x.fi] > gerak+x.se){
dist[x.fi] = gerak+x.se;
pq.push({dist[x.fi], x.fi});
}
}
}
cout << (dist[2] == 4e18 ? -1 : dist[2]) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |