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 tii tuple<int,int,int>
#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][300];
priority_queue<tii, vector<tii>, greater<tii>> 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++) memset(dist[i], INF, sizeof dist[i]);
pq.push({0,doge0.ff, 0});
dist[doge0.ff][0] = 0;
while(pq.size()){
auto[d,u,extra] = pq.top(); pq.pop();
if(dist[u][extra]<d) continue;
// cout << u << ": " << dist[u] << endl;
if(u==doge1.ff) continue;
if(extra==0) for(int x : doges[u]){
if(x >= (int)sqrt(n)){
for(int i = u+x; i < n; i+=x){
int wv = (i-u)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
for(int i = u-x; i >= 0; i-=x){
int wv = (u-i)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
}
else{
if(u+x < n && dist[u+x][0] > d+1){
dist[u+x][0] = d+1, pq.push({dist[u+x][0], u+x, 0});
}
if(u+x < n && dist[u+x][x] > d+1){
dist[u+x][x] = d+1, pq.push({dist[u+x][x], u+x, x});
}
if(u-x >= 0 && dist[u-x][0] > d+1){
dist[u-x][0] = d+1, pq.push({dist[u-x][0], u-x, 0});
}
if(u-x >= 0 && dist[u-x][x] > d+1){
dist[u-x][x] = d+1, pq.push({dist[u-x][x], u-x, x});
}
}
}
if(extra){
if(u+extra < n && dist[u+extra][extra] > d+1){
dist[u+extra][extra] = d+1, pq.push({d+1, u+extra, extra});
}
if(u+extra < n && dist[u+extra][0] > d+1){
dist[u+extra][0] = d+1, pq.push({d+1, u+extra, 0});
}
if(u-extra >= 0 && dist[u-extra][extra] > d+1){
dist[u-extra][extra] = d+1, pq.push({d+1, u-extra, extra});
}
if(u-extra >= 0 && dist[u-extra][0] > d+1){
dist[u-extra][0] = d+1, pq.push({d+1, u-extra, 0});
}
}
}
if(dist[doge1.ff][0] == INF) cout << -1;
else cout << dist[doge1.ff][0];
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... |