This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
// #pragma GCC optimize (03)
#define int long long
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int maxn = 3e4 + 5, INF = 0x3f3f3f3f;
vector<int> doges[maxn];
int dist[maxn];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
pii doge0, doge1; cin >> doge0.ff >> doge0.ss >> doge1.ff >> doge1.ss;
doges[doge0.ff].pb(doge0.ss);
doges[doge1.ff].pb(doge1.ss);
for(int i = 2; i < m; i++){
int bi, pi; cin >> bi >> pi;
doges[bi].pb(pi);
}
for(int i = 0; i < n; i++){
sort(all(doges[i]));
doges[i].erase(unique(all(doges[i])), doges[i].end());
// cout << doges[i].size() << endl;
}
for(int i = 0; i < n; i++) dist[i] = INF;
pq.push({0,doge0.ff});
dist[doge0.ff] = 0;
while(pq.size()){
auto[d,u] = pq.top(); pq.pop();
if(dist[u]<d) continue;
// cout << u << ": " << dist[u] << endl;
if(u==doge1.ff) continue;
for(int x : doges[u]){
for(int i = u+x; i < n; i+=x){
int wv = (i-u)/x;
if(dist[i] > dist[u]+wv)
dist[i] = dist[u]+wv, pq.push({dist[i], i});
}
for(int i = u-x; i >= 0; i-=x){
int wv = (u-i)/x;
if(dist[i] > dist[u]+wv)
dist[i] = dist[u]+wv, pq.push({dist[i], i});
}
}
}
if(dist[doge1.ff] == INF) cout << -1;
else cout << dist[doge1.ff];
cout << '\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... |