#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int N, M, S, E;
int D[30005];
bool V[30005][200];
vector <int> doge[30005];
int main()
{
cin>>N>>M;
for (int i=0;i<M;i++){
int b, p;
cin>>b>>p;
if (p > 199 || !V[b][p]) doge[b].push_back(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(make_pair(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(make_pair(-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(make_pair(-D[i], i));
if (p < 200 && V[i][p]) break;
if (p < 200) V[i][p] = 1;
}
}
}
if (D[E]<2e9)cout<<D[E];
else cout<<-1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1148 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
3 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
3 ms |
1316 KB |
Output is correct |
6 |
Correct |
3 ms |
1328 KB |
Output is correct |
7 |
Correct |
3 ms |
1400 KB |
Output is correct |
8 |
Correct |
3 ms |
1400 KB |
Output is correct |
9 |
Correct |
3 ms |
1400 KB |
Output is correct |
10 |
Correct |
3 ms |
1420 KB |
Output is correct |
11 |
Correct |
4 ms |
1456 KB |
Output is correct |
12 |
Correct |
4 ms |
1456 KB |
Output is correct |
13 |
Correct |
4 ms |
1456 KB |
Output is correct |
14 |
Correct |
4 ms |
1556 KB |
Output is correct |
15 |
Correct |
4 ms |
1584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1584 KB |
Output is correct |
2 |
Correct |
3 ms |
1584 KB |
Output is correct |
3 |
Correct |
3 ms |
1584 KB |
Output is correct |
4 |
Correct |
3 ms |
1584 KB |
Output is correct |
5 |
Correct |
2 ms |
1584 KB |
Output is correct |
6 |
Correct |
2 ms |
1584 KB |
Output is correct |
7 |
Correct |
3 ms |
1584 KB |
Output is correct |
8 |
Correct |
3 ms |
1584 KB |
Output is correct |
9 |
Correct |
3 ms |
1584 KB |
Output is correct |
10 |
Correct |
3 ms |
1584 KB |
Output is correct |
11 |
Correct |
4 ms |
1584 KB |
Output is correct |
12 |
Correct |
4 ms |
1584 KB |
Output is correct |
13 |
Correct |
4 ms |
1584 KB |
Output is correct |
14 |
Correct |
3 ms |
1668 KB |
Output is correct |
15 |
Correct |
4 ms |
1668 KB |
Output is correct |
16 |
Correct |
3 ms |
1668 KB |
Output is correct |
17 |
Correct |
4 ms |
1848 KB |
Output is correct |
18 |
Correct |
4 ms |
1864 KB |
Output is correct |
19 |
Correct |
4 ms |
1864 KB |
Output is correct |
20 |
Correct |
4 ms |
2140 KB |
Output is correct |
21 |
Correct |
3 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
5 ms |
2140 KB |
Output is correct |
25 |
Correct |
5 ms |
2208 KB |
Output is correct |
26 |
Correct |
5 ms |
2208 KB |
Output is correct |
27 |
Correct |
5 ms |
2208 KB |
Output is correct |
28 |
Correct |
5 ms |
2260 KB |
Output is correct |
29 |
Correct |
5 ms |
2280 KB |
Output is correct |
30 |
Correct |
4 ms |
2288 KB |
Output is correct |
31 |
Correct |
4 ms |
2292 KB |
Output is correct |
32 |
Correct |
4 ms |
2364 KB |
Output is correct |
33 |
Correct |
6 ms |
2364 KB |
Output is correct |
34 |
Correct |
6 ms |
2468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2468 KB |
Output is correct |
2 |
Correct |
2 ms |
2468 KB |
Output is correct |
3 |
Correct |
3 ms |
2468 KB |
Output is correct |
4 |
Correct |
3 ms |
2468 KB |
Output is correct |
5 |
Correct |
3 ms |
2468 KB |
Output is correct |
6 |
Correct |
1 ms |
2468 KB |
Output is correct |
7 |
Correct |
3 ms |
2468 KB |
Output is correct |
8 |
Correct |
3 ms |
2468 KB |
Output is correct |
9 |
Correct |
3 ms |
2468 KB |
Output is correct |
10 |
Correct |
3 ms |
2468 KB |
Output is correct |
11 |
Correct |
4 ms |
2468 KB |
Output is correct |
12 |
Correct |
4 ms |
2468 KB |
Output is correct |
13 |
Correct |
4 ms |
2468 KB |
Output is correct |
14 |
Correct |
4 ms |
2468 KB |
Output is correct |
15 |
Correct |
4 ms |
2468 KB |
Output is correct |
16 |
Correct |
3 ms |
2468 KB |
Output is correct |
17 |
Correct |
4 ms |
2468 KB |
Output is correct |
18 |
Correct |
4 ms |
2468 KB |
Output is correct |
19 |
Correct |
3 ms |
2468 KB |
Output is correct |
20 |
Correct |
4 ms |
2468 KB |
Output is correct |
21 |
Correct |
3 ms |
2468 KB |
Output is correct |
22 |
Correct |
4 ms |
2468 KB |
Output is correct |
23 |
Correct |
4 ms |
2468 KB |
Output is correct |
24 |
Correct |
5 ms |
2480 KB |
Output is correct |
25 |
Correct |
5 ms |
2500 KB |
Output is correct |
26 |
Correct |
5 ms |
2500 KB |
Output is correct |
27 |
Correct |
5 ms |
2500 KB |
Output is correct |
28 |
Correct |
5 ms |
2580 KB |
Output is correct |
29 |
Correct |
5 ms |
2580 KB |
Output is correct |
30 |
Correct |
4 ms |
2580 KB |
Output is correct |
31 |
Correct |
4 ms |
2596 KB |
Output is correct |
32 |
Correct |
4 ms |
2596 KB |
Output is correct |
33 |
Correct |
6 ms |
2596 KB |
Output is correct |
34 |
Correct |
6 ms |
2768 KB |
Output is correct |
35 |
Correct |
20 ms |
2876 KB |
Output is correct |
36 |
Correct |
7 ms |
2876 KB |
Output is correct |
37 |
Correct |
18 ms |
3136 KB |
Output is correct |
38 |
Correct |
23 ms |
3552 KB |
Output is correct |
39 |
Correct |
23 ms |
3816 KB |
Output is correct |
40 |
Correct |
23 ms |
4164 KB |
Output is correct |
41 |
Correct |
23 ms |
4344 KB |
Output is correct |
42 |
Correct |
19 ms |
4344 KB |
Output is correct |
43 |
Correct |
18 ms |
4420 KB |
Output is correct |
44 |
Correct |
18 ms |
4644 KB |
Output is correct |
45 |
Correct |
22 ms |
5108 KB |
Output is correct |
46 |
Correct |
22 ms |
5320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5320 KB |
Output is correct |
2 |
Correct |
2 ms |
5320 KB |
Output is correct |
3 |
Correct |
2 ms |
5320 KB |
Output is correct |
4 |
Correct |
2 ms |
5320 KB |
Output is correct |
5 |
Correct |
2 ms |
5320 KB |
Output is correct |
6 |
Correct |
2 ms |
5320 KB |
Output is correct |
7 |
Correct |
3 ms |
5320 KB |
Output is correct |
8 |
Correct |
3 ms |
5320 KB |
Output is correct |
9 |
Correct |
3 ms |
5320 KB |
Output is correct |
10 |
Correct |
3 ms |
5320 KB |
Output is correct |
11 |
Correct |
4 ms |
5320 KB |
Output is correct |
12 |
Correct |
4 ms |
5320 KB |
Output is correct |
13 |
Correct |
4 ms |
5320 KB |
Output is correct |
14 |
Correct |
4 ms |
5320 KB |
Output is correct |
15 |
Correct |
4 ms |
5320 KB |
Output is correct |
16 |
Correct |
3 ms |
5320 KB |
Output is correct |
17 |
Correct |
4 ms |
5320 KB |
Output is correct |
18 |
Correct |
4 ms |
5320 KB |
Output is correct |
19 |
Correct |
3 ms |
5320 KB |
Output is correct |
20 |
Correct |
5 ms |
5320 KB |
Output is correct |
21 |
Correct |
4 ms |
5320 KB |
Output is correct |
22 |
Correct |
3 ms |
5320 KB |
Output is correct |
23 |
Correct |
4 ms |
5320 KB |
Output is correct |
24 |
Correct |
5 ms |
5336 KB |
Output is correct |
25 |
Correct |
3 ms |
5356 KB |
Output is correct |
26 |
Correct |
5 ms |
5356 KB |
Output is correct |
27 |
Correct |
4 ms |
5356 KB |
Output is correct |
28 |
Correct |
5 ms |
5408 KB |
Output is correct |
29 |
Correct |
5 ms |
5408 KB |
Output is correct |
30 |
Correct |
4 ms |
5564 KB |
Output is correct |
31 |
Correct |
4 ms |
5564 KB |
Output is correct |
32 |
Correct |
4 ms |
5564 KB |
Output is correct |
33 |
Correct |
6 ms |
5564 KB |
Output is correct |
34 |
Correct |
6 ms |
5620 KB |
Output is correct |
35 |
Correct |
21 ms |
5700 KB |
Output is correct |
36 |
Correct |
6 ms |
5700 KB |
Output is correct |
37 |
Correct |
16 ms |
6020 KB |
Output is correct |
38 |
Correct |
23 ms |
6436 KB |
Output is correct |
39 |
Correct |
23 ms |
6700 KB |
Output is correct |
40 |
Correct |
19 ms |
6964 KB |
Output is correct |
41 |
Correct |
23 ms |
7228 KB |
Output is correct |
42 |
Correct |
19 ms |
7228 KB |
Output is correct |
43 |
Correct |
19 ms |
7336 KB |
Output is correct |
44 |
Correct |
19 ms |
7544 KB |
Output is correct |
45 |
Correct |
22 ms |
8008 KB |
Output is correct |
46 |
Correct |
22 ms |
8236 KB |
Output is correct |
47 |
Correct |
38 ms |
11264 KB |
Output is correct |
48 |
Correct |
22 ms |
11264 KB |
Output is correct |
49 |
Correct |
21 ms |
11264 KB |
Output is correct |
50 |
Correct |
17 ms |
11264 KB |
Output is correct |
51 |
Correct |
62 ms |
15976 KB |
Output is correct |
52 |
Correct |
66 ms |
16256 KB |
Output is correct |
53 |
Correct |
46 ms |
16680 KB |
Output is correct |
54 |
Correct |
12 ms |
16680 KB |
Output is correct |
55 |
Correct |
13 ms |
16680 KB |
Output is correct |
56 |
Correct |
37 ms |
16680 KB |
Output is correct |
57 |
Correct |
43 ms |
16680 KB |
Output is correct |
58 |
Correct |
25 ms |
16680 KB |
Output is correct |
59 |
Correct |
31 ms |
16680 KB |
Output is correct |
60 |
Correct |
31 ms |
16680 KB |
Output is correct |
61 |
Correct |
29 ms |
16816 KB |
Output is correct |
62 |
Correct |
45 ms |
17780 KB |
Output is correct |
63 |
Correct |
57 ms |
18284 KB |
Output is correct |
64 |
Correct |
52 ms |
18284 KB |
Output is correct |
65 |
Correct |
66 ms |
18868 KB |
Output is correct |
66 |
Correct |
95 ms |
20104 KB |
Output is correct |
67 |
Correct |
126 ms |
20316 KB |
Output is correct |