#include <bits/stdc++.h>
using namespace std;
const int N = 30010, M = 30010;
int n, m, b[M], p[M], dist[N];
bool mark[N];
map<pair<int, int>, bool> seen;
vector<int> vec[N];
int main(){
cin >> n >> m;
for (int i = 0; i < m; i ++){
cin >> b[i] >> p[i];
vec[b[i]].push_back(p[i]);
}
for (int i = 0; i < n; i ++){
sort(vec[i].begin(), vec[i].end());
vec[i].resize(unique(vec[i].begin(), vec[i].end()) - vec[i].begin());
}
memset(dist, 63, sizeof dist);
dist[b[0]] = 0;
for (int ii = 0; ii < n; ii ++){
int v = -1;
for (int j = 0; j < n; j ++)
if (!mark[j] and (v == -1 or dist[v] > dist[j]))
v = j;
if (v == b[1]) break;
mark[v] = 1;
for (int d : vec[v]){
if (seen.find({v, d}) != seen.end()) continue;
for (int i = 1; v + d * i < n and dist[v] + i < dist[b[1]]; i ++){
if (seen.find({v + d * i, d}) != seen.end()) break;
if (dist[v + d * i] > dist[v] + i){
dist[v + d * i] = dist[v] + i;
seen[{v + d * i, d}] = 1;
}
}
for (int i = 1; v - d * i >= 0 and dist[v] + i < dist[b[1]]; i ++){
if (seen.find({v - d * i, d}) != seen.end()) break;
if (dist[v - d * i] > dist[v] + i){
dist[v - d * i] = dist[v] + i;
seen[{v - d * i, d}] = 1;
}
}
}
}
if (dist[b[1]] > 1e9)
dist[b[1]] = -1;
cout << dist[b[1]] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1148 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1200 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
5 ms |
1124 KB |
Output is correct |
19 |
Correct |
5 ms |
1116 KB |
Output is correct |
20 |
Correct |
7 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
5 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1372 KB |
Output is correct |
24 |
Correct |
7 ms |
1476 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
7 ms |
1368 KB |
Output is correct |
27 |
Correct |
6 ms |
1372 KB |
Output is correct |
28 |
Correct |
8 ms |
1372 KB |
Output is correct |
29 |
Correct |
14 ms |
1960 KB |
Output is correct |
30 |
Correct |
8 ms |
1372 KB |
Output is correct |
31 |
Correct |
10 ms |
1692 KB |
Output is correct |
32 |
Correct |
9 ms |
1592 KB |
Output is correct |
33 |
Correct |
20 ms |
2396 KB |
Output is correct |
34 |
Correct |
16 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
5 ms |
1312 KB |
Output is correct |
19 |
Correct |
4 ms |
1116 KB |
Output is correct |
20 |
Correct |
8 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
1408 KB |
Output is correct |
24 |
Correct |
9 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1384 KB |
Output is correct |
26 |
Correct |
6 ms |
1372 KB |
Output is correct |
27 |
Correct |
7 ms |
1372 KB |
Output is correct |
28 |
Correct |
9 ms |
1372 KB |
Output is correct |
29 |
Correct |
14 ms |
2136 KB |
Output is correct |
30 |
Correct |
9 ms |
1372 KB |
Output is correct |
31 |
Correct |
9 ms |
1628 KB |
Output is correct |
32 |
Correct |
8 ms |
1372 KB |
Output is correct |
33 |
Correct |
20 ms |
2284 KB |
Output is correct |
34 |
Correct |
18 ms |
2396 KB |
Output is correct |
35 |
Correct |
13 ms |
2140 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
12 ms |
1976 KB |
Output is correct |
38 |
Correct |
11 ms |
1884 KB |
Output is correct |
39 |
Correct |
9 ms |
1884 KB |
Output is correct |
40 |
Correct |
9 ms |
1856 KB |
Output is correct |
41 |
Correct |
11 ms |
2056 KB |
Output is correct |
42 |
Correct |
11 ms |
1964 KB |
Output is correct |
43 |
Correct |
12 ms |
1884 KB |
Output is correct |
44 |
Correct |
13 ms |
1884 KB |
Output is correct |
45 |
Correct |
60 ms |
3000 KB |
Output is correct |
46 |
Correct |
39 ms |
3080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
5 ms |
1312 KB |
Output is correct |
19 |
Correct |
4 ms |
1288 KB |
Output is correct |
20 |
Correct |
7 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
5 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1372 KB |
Output is correct |
24 |
Correct |
6 ms |
1472 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
7 ms |
1372 KB |
Output is correct |
27 |
Correct |
6 ms |
1368 KB |
Output is correct |
28 |
Correct |
8 ms |
1372 KB |
Output is correct |
29 |
Correct |
14 ms |
1912 KB |
Output is correct |
30 |
Correct |
9 ms |
1368 KB |
Output is correct |
31 |
Correct |
10 ms |
1444 KB |
Output is correct |
32 |
Correct |
9 ms |
1584 KB |
Output is correct |
33 |
Correct |
21 ms |
2396 KB |
Output is correct |
34 |
Correct |
17 ms |
2376 KB |
Output is correct |
35 |
Correct |
13 ms |
2092 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
11 ms |
1884 KB |
Output is correct |
38 |
Correct |
11 ms |
1896 KB |
Output is correct |
39 |
Correct |
10 ms |
1800 KB |
Output is correct |
40 |
Correct |
9 ms |
1880 KB |
Output is correct |
41 |
Correct |
11 ms |
2140 KB |
Output is correct |
42 |
Correct |
12 ms |
1756 KB |
Output is correct |
43 |
Correct |
13 ms |
1880 KB |
Output is correct |
44 |
Correct |
17 ms |
1884 KB |
Output is correct |
45 |
Correct |
85 ms |
3008 KB |
Output is correct |
46 |
Correct |
40 ms |
2984 KB |
Output is correct |
47 |
Correct |
128 ms |
2900 KB |
Output is correct |
48 |
Correct |
388 ms |
2136 KB |
Output is correct |
49 |
Correct |
673 ms |
2132 KB |
Output is correct |
50 |
Correct |
789 ms |
1892 KB |
Output is correct |
51 |
Execution timed out |
1067 ms |
6352 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |