#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9+5;
const int MX = 30001;
int n, m;
map<int,int>nodes[MX];
priority_queue<pair<int,int>>pq;
int dist[MX];
bool vis[MX];
vector<int>dvs[MX];
void PlayGround() {
cin >> n >> m;
for(int dv=1; dv<=n; ++dv) {
for(int i=1; dv*i<=n; ++i) {
dvs[dv*i].push_back(dv);
}
}
int b[2];
for(int i=0; i<m; ++i) {
int _b, p;
cin >> _b >> p;
if(i<2) {
b[i] = _b;
}
nodes[_b][p] = 1;
}
for(int i=0; i<n; ++i) {
dist[i] = INF;
}
dist[b[1]] = 0;
pq.emplace(0, b[1]);
while(!pq.empty()) {
int d, v;
tie(d, v) = pq.top();
d = -d;
pq.pop();
if(vis[v]) continue;
vis[v] = 1;
for(int i=0; i<n; ++i) if(i!=v) {
int gap = abs(i-v);
int need = INF;
for(int dv : dvs[gap]) {
if(nodes[i].count(dv)) {
need = min(need, gap/dv);
}
}
if(d+need<dist[i]) {
dist[i] = d+need;
pq.emplace(-dist[i], i);
}
}
}
if(dist[b[0]]==INF) {
dist[b[0]] = -1;
}
cout << dist[b[0]] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
25 ms |
2576 KB |
Output is correct |
18 |
Correct |
13 ms |
2508 KB |
Output is correct |
19 |
Correct |
4 ms |
2508 KB |
Output is correct |
20 |
Correct |
105 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
6 ms |
2596 KB |
Output is correct |
23 |
Correct |
10 ms |
2584 KB |
Output is correct |
24 |
Correct |
42 ms |
2664 KB |
Output is correct |
25 |
Correct |
18 ms |
2636 KB |
Output is correct |
26 |
Correct |
5 ms |
2508 KB |
Output is correct |
27 |
Correct |
4 ms |
2520 KB |
Output is correct |
28 |
Correct |
54 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2508 KB |
Output is correct |
30 |
Correct |
2 ms |
2508 KB |
Output is correct |
31 |
Correct |
2 ms |
2508 KB |
Output is correct |
32 |
Correct |
2 ms |
2508 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
4 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
26 ms |
2588 KB |
Output is correct |
18 |
Correct |
13 ms |
2608 KB |
Output is correct |
19 |
Correct |
4 ms |
2508 KB |
Output is correct |
20 |
Correct |
108 ms |
2692 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
5 ms |
2508 KB |
Output is correct |
23 |
Correct |
10 ms |
2508 KB |
Output is correct |
24 |
Correct |
43 ms |
2664 KB |
Output is correct |
25 |
Correct |
18 ms |
2636 KB |
Output is correct |
26 |
Correct |
5 ms |
2508 KB |
Output is correct |
27 |
Correct |
4 ms |
2508 KB |
Output is correct |
28 |
Correct |
52 ms |
2664 KB |
Output is correct |
29 |
Correct |
2 ms |
2508 KB |
Output is correct |
30 |
Correct |
2 ms |
2508 KB |
Output is correct |
31 |
Correct |
2 ms |
2508 KB |
Output is correct |
32 |
Correct |
2 ms |
2508 KB |
Output is correct |
33 |
Correct |
2 ms |
2592 KB |
Output is correct |
34 |
Correct |
4 ms |
2636 KB |
Output is correct |
35 |
Correct |
251 ms |
3780 KB |
Output is correct |
36 |
Correct |
49 ms |
2728 KB |
Output is correct |
37 |
Correct |
428 ms |
3548 KB |
Output is correct |
38 |
Correct |
478 ms |
4028 KB |
Output is correct |
39 |
Correct |
494 ms |
4012 KB |
Output is correct |
40 |
Correct |
478 ms |
4012 KB |
Output is correct |
41 |
Correct |
484 ms |
4164 KB |
Output is correct |
42 |
Correct |
8 ms |
2508 KB |
Output is correct |
43 |
Correct |
7 ms |
2580 KB |
Output is correct |
44 |
Correct |
114 ms |
2688 KB |
Output is correct |
45 |
Correct |
11 ms |
3936 KB |
Output is correct |
46 |
Correct |
19 ms |
3976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2380 KB |
Output is correct |
4 |
Correct |
1 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2380 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2508 KB |
Output is correct |
15 |
Correct |
2 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
27 ms |
2568 KB |
Output is correct |
18 |
Correct |
13 ms |
2608 KB |
Output is correct |
19 |
Correct |
4 ms |
2508 KB |
Output is correct |
20 |
Correct |
106 ms |
2704 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
5 ms |
2508 KB |
Output is correct |
23 |
Correct |
9 ms |
2508 KB |
Output is correct |
24 |
Correct |
45 ms |
2636 KB |
Output is correct |
25 |
Correct |
18 ms |
2636 KB |
Output is correct |
26 |
Correct |
5 ms |
2508 KB |
Output is correct |
27 |
Correct |
4 ms |
2596 KB |
Output is correct |
28 |
Correct |
53 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2508 KB |
Output is correct |
30 |
Correct |
2 ms |
2508 KB |
Output is correct |
31 |
Correct |
2 ms |
2508 KB |
Output is correct |
32 |
Correct |
2 ms |
2508 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
3 ms |
2636 KB |
Output is correct |
35 |
Correct |
262 ms |
3716 KB |
Output is correct |
36 |
Correct |
52 ms |
2728 KB |
Output is correct |
37 |
Correct |
425 ms |
3640 KB |
Output is correct |
38 |
Correct |
485 ms |
4016 KB |
Output is correct |
39 |
Correct |
477 ms |
4012 KB |
Output is correct |
40 |
Correct |
482 ms |
4012 KB |
Output is correct |
41 |
Correct |
489 ms |
4052 KB |
Output is correct |
42 |
Correct |
8 ms |
2508 KB |
Output is correct |
43 |
Correct |
7 ms |
2596 KB |
Output is correct |
44 |
Correct |
113 ms |
2688 KB |
Output is correct |
45 |
Correct |
11 ms |
3956 KB |
Output is correct |
46 |
Correct |
19 ms |
3968 KB |
Output is correct |
47 |
Execution timed out |
1062 ms |
5160 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |