#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);
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;
}
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;
}
}
}
printf("%d\n", D[E] < 2e9 ? D[E] : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8400 KB |
Output is correct |
2 |
Correct |
0 ms |
8400 KB |
Output is correct |
3 |
Correct |
0 ms |
8400 KB |
Output is correct |
4 |
Correct |
0 ms |
8400 KB |
Output is correct |
5 |
Correct |
1 ms |
8400 KB |
Output is correct |
6 |
Correct |
0 ms |
8400 KB |
Output is correct |
7 |
Correct |
0 ms |
8400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8400 KB |
Output is correct |
2 |
Correct |
0 ms |
8400 KB |
Output is correct |
3 |
Correct |
0 ms |
8400 KB |
Output is correct |
4 |
Correct |
0 ms |
8400 KB |
Output is correct |
5 |
Correct |
0 ms |
8400 KB |
Output is correct |
6 |
Correct |
0 ms |
8400 KB |
Output is correct |
7 |
Correct |
0 ms |
8400 KB |
Output is correct |
8 |
Correct |
0 ms |
8400 KB |
Output is correct |
9 |
Correct |
0 ms |
8400 KB |
Output is correct |
10 |
Correct |
0 ms |
8400 KB |
Output is correct |
11 |
Correct |
0 ms |
8400 KB |
Output is correct |
12 |
Correct |
0 ms |
8400 KB |
Output is correct |
13 |
Correct |
0 ms |
8400 KB |
Output is correct |
14 |
Correct |
0 ms |
8400 KB |
Output is correct |
15 |
Correct |
0 ms |
8400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8400 KB |
Output is correct |
2 |
Correct |
0 ms |
8400 KB |
Output is correct |
3 |
Correct |
0 ms |
8400 KB |
Output is correct |
4 |
Correct |
0 ms |
8400 KB |
Output is correct |
5 |
Correct |
0 ms |
8400 KB |
Output is correct |
6 |
Correct |
0 ms |
8400 KB |
Output is correct |
7 |
Correct |
0 ms |
8400 KB |
Output is correct |
8 |
Correct |
0 ms |
8400 KB |
Output is correct |
9 |
Correct |
0 ms |
8400 KB |
Output is correct |
10 |
Correct |
0 ms |
8400 KB |
Output is correct |
11 |
Correct |
0 ms |
8400 KB |
Output is correct |
12 |
Correct |
0 ms |
8400 KB |
Output is correct |
13 |
Correct |
0 ms |
8400 KB |
Output is correct |
14 |
Correct |
0 ms |
8400 KB |
Output is correct |
15 |
Correct |
0 ms |
8400 KB |
Output is correct |
16 |
Correct |
0 ms |
8400 KB |
Output is correct |
17 |
Correct |
2 ms |
8400 KB |
Output is correct |
18 |
Correct |
0 ms |
8400 KB |
Output is correct |
19 |
Correct |
1 ms |
8400 KB |
Output is correct |
20 |
Correct |
2 ms |
8400 KB |
Output is correct |
21 |
Correct |
0 ms |
8400 KB |
Output is correct |
22 |
Correct |
0 ms |
8400 KB |
Output is correct |
23 |
Correct |
0 ms |
8400 KB |
Output is correct |
24 |
Correct |
0 ms |
8400 KB |
Output is correct |
25 |
Correct |
0 ms |
8400 KB |
Output is correct |
26 |
Incorrect |
0 ms |
8400 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8400 KB |
Output is correct |
2 |
Correct |
0 ms |
8400 KB |
Output is correct |
3 |
Correct |
1 ms |
8400 KB |
Output is correct |
4 |
Correct |
0 ms |
8400 KB |
Output is correct |
5 |
Correct |
0 ms |
8400 KB |
Output is correct |
6 |
Correct |
0 ms |
8400 KB |
Output is correct |
7 |
Correct |
0 ms |
8400 KB |
Output is correct |
8 |
Correct |
0 ms |
8400 KB |
Output is correct |
9 |
Correct |
0 ms |
8400 KB |
Output is correct |
10 |
Correct |
0 ms |
8400 KB |
Output is correct |
11 |
Correct |
0 ms |
8400 KB |
Output is correct |
12 |
Correct |
0 ms |
8400 KB |
Output is correct |
13 |
Correct |
0 ms |
8400 KB |
Output is correct |
14 |
Correct |
0 ms |
8400 KB |
Output is correct |
15 |
Correct |
0 ms |
8400 KB |
Output is correct |
16 |
Correct |
0 ms |
8400 KB |
Output is correct |
17 |
Correct |
0 ms |
8400 KB |
Output is correct |
18 |
Correct |
0 ms |
8400 KB |
Output is correct |
19 |
Correct |
0 ms |
8400 KB |
Output is correct |
20 |
Correct |
0 ms |
8400 KB |
Output is correct |
21 |
Correct |
0 ms |
8400 KB |
Output is correct |
22 |
Correct |
0 ms |
8400 KB |
Output is correct |
23 |
Correct |
0 ms |
8400 KB |
Output is correct |
24 |
Correct |
0 ms |
8400 KB |
Output is correct |
25 |
Correct |
2 ms |
8400 KB |
Output is correct |
26 |
Incorrect |
0 ms |
8400 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8400 KB |
Output is correct |
2 |
Correct |
0 ms |
8400 KB |
Output is correct |
3 |
Correct |
0 ms |
8400 KB |
Output is correct |
4 |
Correct |
0 ms |
8400 KB |
Output is correct |
5 |
Correct |
0 ms |
8400 KB |
Output is correct |
6 |
Correct |
0 ms |
8400 KB |
Output is correct |
7 |
Correct |
0 ms |
8400 KB |
Output is correct |
8 |
Correct |
0 ms |
8400 KB |
Output is correct |
9 |
Correct |
0 ms |
8400 KB |
Output is correct |
10 |
Correct |
1 ms |
8400 KB |
Output is correct |
11 |
Correct |
0 ms |
8400 KB |
Output is correct |
12 |
Correct |
0 ms |
8400 KB |
Output is correct |
13 |
Correct |
0 ms |
8400 KB |
Output is correct |
14 |
Correct |
0 ms |
8400 KB |
Output is correct |
15 |
Correct |
0 ms |
8400 KB |
Output is correct |
16 |
Correct |
0 ms |
8400 KB |
Output is correct |
17 |
Correct |
0 ms |
8400 KB |
Output is correct |
18 |
Correct |
1 ms |
8400 KB |
Output is correct |
19 |
Correct |
0 ms |
8400 KB |
Output is correct |
20 |
Correct |
0 ms |
8400 KB |
Output is correct |
21 |
Correct |
0 ms |
8400 KB |
Output is correct |
22 |
Correct |
0 ms |
8400 KB |
Output is correct |
23 |
Correct |
0 ms |
8400 KB |
Output is correct |
24 |
Correct |
1 ms |
8400 KB |
Output is correct |
25 |
Correct |
0 ms |
8400 KB |
Output is correct |
26 |
Incorrect |
0 ms |
8400 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |