#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;
}
set<int>rem;
for(int i=0; i<n; ++i) {
rem.insert(i);
}
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;
rem.erase(v);
for(int i : rem) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2432 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
1 ms |
2380 KB |
Output is correct |
7 |
Correct |
1 ms |
2324 KB |
Output is correct |
8 |
Correct |
1 ms |
2380 KB |
Output is correct |
9 |
Correct |
1 ms |
2380 KB |
Output is correct |
10 |
Correct |
1 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
2380 KB |
Output is correct |
17 |
Correct |
16 ms |
2508 KB |
Output is correct |
18 |
Correct |
19 ms |
2680 KB |
Output is correct |
19 |
Correct |
6 ms |
2664 KB |
Output is correct |
20 |
Correct |
99 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
7 ms |
2636 KB |
Output is correct |
23 |
Correct |
15 ms |
2660 KB |
Output is correct |
24 |
Correct |
57 ms |
2740 KB |
Output is correct |
25 |
Correct |
24 ms |
2760 KB |
Output is correct |
26 |
Correct |
6 ms |
2636 KB |
Output is correct |
27 |
Correct |
5 ms |
2636 KB |
Output is correct |
28 |
Correct |
51 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
3 ms |
2636 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
4 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2456 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 |
2508 KB |
Output is correct |
17 |
Correct |
16 ms |
2616 KB |
Output is correct |
18 |
Correct |
16 ms |
2676 KB |
Output is correct |
19 |
Correct |
6 ms |
2636 KB |
Output is correct |
20 |
Correct |
74 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
2508 KB |
Output is correct |
22 |
Correct |
7 ms |
2672 KB |
Output is correct |
23 |
Correct |
12 ms |
2636 KB |
Output is correct |
24 |
Correct |
46 ms |
2744 KB |
Output is correct |
25 |
Correct |
25 ms |
2636 KB |
Output is correct |
26 |
Correct |
6 ms |
2636 KB |
Output is correct |
27 |
Correct |
5 ms |
2636 KB |
Output is correct |
28 |
Correct |
52 ms |
2636 KB |
Output is correct |
29 |
Correct |
3 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
33 |
Correct |
3 ms |
2636 KB |
Output is correct |
34 |
Correct |
4 ms |
2644 KB |
Output is correct |
35 |
Correct |
139 ms |
3788 KB |
Output is correct |
36 |
Correct |
37 ms |
2780 KB |
Output is correct |
37 |
Correct |
266 ms |
3612 KB |
Output is correct |
38 |
Correct |
284 ms |
4084 KB |
Output is correct |
39 |
Correct |
277 ms |
4112 KB |
Output is correct |
40 |
Correct |
291 ms |
4112 KB |
Output is correct |
41 |
Correct |
270 ms |
4144 KB |
Output is correct |
42 |
Correct |
9 ms |
2636 KB |
Output is correct |
43 |
Correct |
8 ms |
2636 KB |
Output is correct |
44 |
Correct |
85 ms |
2764 KB |
Output is correct |
45 |
Correct |
14 ms |
4044 KB |
Output is correct |
46 |
Correct |
26 ms |
4168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2380 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 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 |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
17 ms |
2508 KB |
Output is correct |
18 |
Correct |
18 ms |
2636 KB |
Output is correct |
19 |
Correct |
7 ms |
2636 KB |
Output is correct |
20 |
Correct |
83 ms |
2764 KB |
Output is correct |
21 |
Correct |
3 ms |
2508 KB |
Output is correct |
22 |
Correct |
7 ms |
2636 KB |
Output is correct |
23 |
Correct |
12 ms |
2660 KB |
Output is correct |
24 |
Correct |
47 ms |
2636 KB |
Output is correct |
25 |
Correct |
23 ms |
2756 KB |
Output is correct |
26 |
Correct |
6 ms |
2636 KB |
Output is correct |
27 |
Correct |
5 ms |
2636 KB |
Output is correct |
28 |
Correct |
52 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2636 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
3 ms |
2636 KB |
Output is correct |
34 |
Correct |
5 ms |
2636 KB |
Output is correct |
35 |
Correct |
150 ms |
3816 KB |
Output is correct |
36 |
Correct |
35 ms |
2764 KB |
Output is correct |
37 |
Correct |
252 ms |
3584 KB |
Output is correct |
38 |
Correct |
309 ms |
4108 KB |
Output is correct |
39 |
Correct |
285 ms |
4108 KB |
Output is correct |
40 |
Correct |
303 ms |
4104 KB |
Output is correct |
41 |
Correct |
295 ms |
4148 KB |
Output is correct |
42 |
Correct |
13 ms |
2676 KB |
Output is correct |
43 |
Correct |
9 ms |
2636 KB |
Output is correct |
44 |
Correct |
90 ms |
2764 KB |
Output is correct |
45 |
Correct |
11 ms |
4044 KB |
Output is correct |
46 |
Correct |
20 ms |
4072 KB |
Output is correct |
47 |
Execution timed out |
1086 ms |
5540 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |