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;
const int N = 2e3+4;
const ll inf = 1e18;
int n,m;
int pos[N],p[N];
vector<pll> adj[N];
priority_queue<pll, vector<pll>, greater<pll>> d;
ll dis[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i=1; i<=m; i++) cin >> pos[i] >> p[i];
for (int i=1; i<=m; i++) for (int j=1; j<=m; j++) if (pos[i]%p[i] == pos[j]%p[i]){
// cout << i << " -> " << j << "\n";
adj[i].push_back({j, abs(pos[j]-pos[i])/p[i]});
}
d.push({0, 1});
for (int i=2; i<=m; i++) dis[i] = inf;
for (; d.size(); ){ auto [w, u] = d.top(); d.pop();
if (w != dis[u]) continue;
for (auto [v, we] : adj[u]) if (dis[v] > w + we)
d.push({dis[v] = w + we, v});
}
cout << (dis[2] == inf ? -1 : dis[2]) << "\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... |