#include <bits/stdc++.h>
using namespace std;
#define ll int
#define MAXN 30005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
ll n, m, d[MAXN], b0 = 0, b1 = 0;
vector<pair<ll, ll>> adj[MAXN];
set<ll> p[MAXN];
int main(){
cin >> n >> m;
FOR(i, 0, m - 1){
ll bi, pi; cin >> bi >> pi;
if(i == 0) b0 = bi;
if(i == 1) b1 = bi;
p[bi].insert(pi);
}
FOR(i, 0, n - 1){
for(ll pi : p[i]){
ll j = i;
while(0 <= j - pi){
j -= pi;
adj[i].push_back({j, (i - j) / pi});
if(p[j].find(pi) != p[j].end()) break;
}
j = i;
while(j + pi <= n - 1){
j += pi;
adj[i].push_back({j, (j - i) / pi});
if(p[j].find(pi) != p[j].end()) break;
}
}
d[i] = -1;
}
/*cout << "b0, b1 are " << b0 << " and " << b1 << endl;
FOR(i, 0, n - 1){
cout << "edges from " << i << ": " << endl;
for(auto j : adj[i]){
cout << " goes to " << j.first << " in " << j.second << " jumps" << endl;
}
}*/
priority_queue<pair<ll, ll>> pq;
d[b0] = 0, pq.push({0, b0});
while(!pq.empty()){
auto pqi = pq.top(); pqi.first *= -1;
pq.pop();
if(d[pqi.second] < pqi.first) continue;
if(pqi.second == b1) break;
for(auto i : adj[pqi.second]){
if(d[i.first] == -1 || pqi.first + i.second < d[i.first]){
d[i.first] = pqi.first + i.second;
pq.push({-d[i.first], i.first});
}
}
// cout << "HERE " << pqi.second << " w/ d = " << pqi.first << endl;
}
cout << d[b1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2648 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2560 KB |
Output is correct |
12 |
Correct |
2 ms |
2564 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2656 KB |
Output is correct |
15 |
Correct |
2 ms |
2564 KB |
Output is correct |
16 |
Correct |
2 ms |
2400 KB |
Output is correct |
17 |
Correct |
2 ms |
2788 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2656 KB |
Output is correct |
20 |
Correct |
3 ms |
2568 KB |
Output is correct |
21 |
Correct |
2 ms |
2572 KB |
Output is correct |
22 |
Correct |
2 ms |
2660 KB |
Output is correct |
23 |
Correct |
2 ms |
2660 KB |
Output is correct |
24 |
Correct |
3 ms |
2660 KB |
Output is correct |
25 |
Correct |
2 ms |
2572 KB |
Output is correct |
26 |
Correct |
2 ms |
2584 KB |
Output is correct |
27 |
Correct |
3 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2780 KB |
Output is correct |
29 |
Correct |
3 ms |
3080 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
5 ms |
3928 KB |
Output is correct |
34 |
Correct |
5 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2344 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2512 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2604 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2564 KB |
Output is correct |
22 |
Correct |
1 ms |
2564 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
4 ms |
2828 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2904 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2904 KB |
Output is correct |
29 |
Correct |
3 ms |
3072 KB |
Output is correct |
30 |
Correct |
2 ms |
2812 KB |
Output is correct |
31 |
Correct |
3 ms |
2904 KB |
Output is correct |
32 |
Correct |
2 ms |
2912 KB |
Output is correct |
33 |
Correct |
8 ms |
3888 KB |
Output is correct |
34 |
Correct |
5 ms |
3932 KB |
Output is correct |
35 |
Correct |
19 ms |
4956 KB |
Output is correct |
36 |
Correct |
4 ms |
2820 KB |
Output is correct |
37 |
Correct |
23 ms |
5468 KB |
Output is correct |
38 |
Correct |
24 ms |
5972 KB |
Output is correct |
39 |
Correct |
23 ms |
5824 KB |
Output is correct |
40 |
Correct |
32 ms |
5980 KB |
Output is correct |
41 |
Correct |
32 ms |
5712 KB |
Output is correct |
42 |
Correct |
9 ms |
2652 KB |
Output is correct |
43 |
Correct |
8 ms |
2652 KB |
Output is correct |
44 |
Correct |
9 ms |
2828 KB |
Output is correct |
45 |
Correct |
23 ms |
8284 KB |
Output is correct |
46 |
Correct |
23 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
2548 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2552 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
3 ms |
2696 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2656 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
3 ms |
2572 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
5 ms |
3144 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2704 KB |
Output is correct |
33 |
Correct |
5 ms |
3932 KB |
Output is correct |
34 |
Correct |
4 ms |
3932 KB |
Output is correct |
35 |
Correct |
21 ms |
4900 KB |
Output is correct |
36 |
Correct |
3 ms |
2820 KB |
Output is correct |
37 |
Correct |
17 ms |
5544 KB |
Output is correct |
38 |
Correct |
22 ms |
5748 KB |
Output is correct |
39 |
Correct |
22 ms |
5712 KB |
Output is correct |
40 |
Correct |
23 ms |
5964 KB |
Output is correct |
41 |
Correct |
32 ms |
5716 KB |
Output is correct |
42 |
Correct |
8 ms |
2652 KB |
Output is correct |
43 |
Correct |
8 ms |
2652 KB |
Output is correct |
44 |
Correct |
8 ms |
2908 KB |
Output is correct |
45 |
Correct |
22 ms |
8452 KB |
Output is correct |
46 |
Correct |
24 ms |
8272 KB |
Output is correct |
47 |
Correct |
38 ms |
12792 KB |
Output is correct |
48 |
Correct |
18 ms |
6092 KB |
Output is correct |
49 |
Correct |
15 ms |
5336 KB |
Output is correct |
50 |
Correct |
15 ms |
4952 KB |
Output is correct |
51 |
Correct |
30 ms |
8408 KB |
Output is correct |
52 |
Correct |
32 ms |
8796 KB |
Output is correct |
53 |
Correct |
25 ms |
5308 KB |
Output is correct |
54 |
Correct |
3 ms |
3160 KB |
Output is correct |
55 |
Correct |
4 ms |
3544 KB |
Output is correct |
56 |
Correct |
13 ms |
5088 KB |
Output is correct |
57 |
Correct |
13 ms |
11524 KB |
Output is correct |
58 |
Correct |
10 ms |
3796 KB |
Output is correct |
59 |
Correct |
12 ms |
4268 KB |
Output is correct |
60 |
Correct |
13 ms |
4824 KB |
Output is correct |
61 |
Correct |
15 ms |
4312 KB |
Output is correct |
62 |
Correct |
22 ms |
9424 KB |
Output is correct |
63 |
Correct |
58 ms |
25568 KB |
Output is correct |
64 |
Correct |
71 ms |
28588 KB |
Output is correct |
65 |
Correct |
83 ms |
38412 KB |
Output is correct |
66 |
Correct |
140 ms |
65600 KB |
Output is correct |
67 |
Correct |
130 ms |
65524 KB |
Output is correct |