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;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int N = 3e4+4;
const ll inf = 1e18;
int n,m;
map<int,vector<int>> d1[N];
bool vis[N];
vector<pii> adj[N];
ll dis[N];
priority_queue<pll, vector<pll>, greater<pll>> d3;
int S,T;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i=0,pos,p; i<m; i++){ cin >> pos >> p;
d1[p][pos%p].push_back(pos);
if (i == 0) S = pos;
if (i == 1) T = pos;
}
for (int p=0; p<n; p++) for (auto &[x, d2] : d1[p]){ int l = x;
for (int i : d2) vis[i] = 1;
for (int last=-1; l<n; l+=p){
if (last != -1) adj[last].push_back({l, (l-last)/p});
if (vis[l]) last = l;
}
l -= p;
for (int last=-1; l>=0; l-=p){
if (last != -1) adj[last].push_back({l, (last-l)/p});
if (vis[l]) last = l;
}
for (int i : d2) vis[i] = 0;
}
for (int i=0; i<n; i++) dis[i] = inf;
d3.push({dis[S] = 0, S});
for (; d3.size(); ){ auto [w, u] = d3.top(); d3.pop();
if (w != dis[u]) continue;
for (auto [v, we] : adj[u]) if (dis[v] > w + we)
d3.push({dis[v] = w + we, v});
}
cout << (dis[T] == inf ? -1 : dis[T]) << "\n";
return 0;
}
# | 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... |