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;
#define MAXN 30005
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
int N, M, S, E;
int D[MAXN];
bool V[MAXN][200];
vector <int> doge[MAXN];
int main()
{
scanf("%d%d", &N, &M);
for (int i=0;i<M;i++){
int b, p;
scanf("%d%d", &b, &p);
if (p > 199 || !V[b][p]) doge[b].pb(p);
if (p < 200) V[b][p] = 1;
if (!i) S = b;
if (i == 1) E = b;
}
for (int i=0;i<N;i++) D[i] = 2e9;
priority_queue <pii> que;
D[S] = 0;
que.push(mp(0, S));
while (!que.empty()){
int q = que.top().second, d = -que.top().first; que.pop();
if (D[q] != d) continue;
for (int p: doge[q]){
for (int i=q-p;i>=0;i-=p){
if (D[i] > d+(q-i)/p)
D[i] = d+(q-i)/p, que.push(mp(-D[i], i));
if (p < 200 && V[i][p]) break;
if (p < 200) V[i][p] = 1;
}
for (int i=q+p;i<N;i+=p){
if (D[i] > d+(i-q)/p)
D[i] = d+(i-q)/p, que.push(mp(-D[i], i));
if (p < 200 && V[i][p]) break;
if (p < 200) V[i][p] = 1;
}
}
}
printf("%d\n", D[E] < 2e9 ? D[E] : -1);
}
# | 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... |