#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<pair<int, int>>> G; // first = destination, second = cost
vector<vector<int>> building;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
G.resize(N);
building.resize(N);
int start, end, p;
cin >> start >> p; building[start].emplace_back(p);
cin >> end >> p; building[end].emplace_back(p);
for (int i = 2; i < M; i++) {
int b, p;
cin >> b >> p;
building[b].emplace_back(p);
}
for (auto &n : building) {
sort(n.begin(), n.end());
n.resize(unique(n.begin(), n.end()) - n.begin());
}
/* Build Graph */
for (int i = 0; i < N; i++) {
for (auto p : building[i]) {
for (int step = 1; i + step * p < N; step++) { // forward
G[i].emplace_back(i + step * p, step);
if (binary_search(building[i + step * p].begin(), building[i + step * p].end(), p)) break;
}
for (int step = 1; i - step * p >= 0; step++) { // backward
G[i].emplace_back(i - step * p, step);
if (binary_search(building[i - step * p].begin(), building[i - step * p].end(), p)) break;
}
}
}
for (auto &n : G) {
sort(n.begin(), n.end());
n.resize(unique(n.begin(), n.end()) - n.begin());
}
/* Dijkstra on created graph */
int ans = INT_MAX;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, start);
vector<int> dist(N, INT_MAX);
dist[start] = 0;
while (!pq.empty()) {
int n = pq.top().second, d = pq.top().first;
pq.pop();
if (dist[n] != d)
continue;
if (n == end) {
ans = d;
break;
}
for (auto v : G[n]) {
if (dist[v.first] == INT_MAX || dist[v.first] > d + v.second) {
dist[v.first] = d + v.second;
pq.emplace(dist[v.first], v.first);
}
}
}
if (ans == INT_MAX)
ans = -1;
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
11 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
632 KB |
Output is correct |
25 |
Correct |
4 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
4 ms |
760 KB |
Output is correct |
29 |
Correct |
9 ms |
1144 KB |
Output is correct |
30 |
Correct |
4 ms |
760 KB |
Output is correct |
31 |
Correct |
6 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
760 KB |
Output is correct |
33 |
Correct |
15 ms |
1656 KB |
Output is correct |
34 |
Correct |
14 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
4 ms |
760 KB |
Output is correct |
29 |
Correct |
9 ms |
1144 KB |
Output is correct |
30 |
Correct |
4 ms |
760 KB |
Output is correct |
31 |
Correct |
6 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
760 KB |
Output is correct |
33 |
Correct |
15 ms |
1788 KB |
Output is correct |
34 |
Correct |
14 ms |
1784 KB |
Output is correct |
35 |
Correct |
16 ms |
1656 KB |
Output is correct |
36 |
Correct |
5 ms |
632 KB |
Output is correct |
37 |
Correct |
23 ms |
2680 KB |
Output is correct |
38 |
Correct |
27 ms |
2424 KB |
Output is correct |
39 |
Correct |
23 ms |
2424 KB |
Output is correct |
40 |
Correct |
23 ms |
2424 KB |
Output is correct |
41 |
Correct |
23 ms |
2424 KB |
Output is correct |
42 |
Correct |
8 ms |
632 KB |
Output is correct |
43 |
Correct |
9 ms |
760 KB |
Output is correct |
44 |
Correct |
8 ms |
632 KB |
Output is correct |
45 |
Correct |
53 ms |
4856 KB |
Output is correct |
46 |
Correct |
53 ms |
4756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
632 KB |
Output is correct |
25 |
Correct |
4 ms |
632 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
4 ms |
760 KB |
Output is correct |
29 |
Correct |
9 ms |
1144 KB |
Output is correct |
30 |
Correct |
4 ms |
760 KB |
Output is correct |
31 |
Correct |
5 ms |
888 KB |
Output is correct |
32 |
Correct |
5 ms |
760 KB |
Output is correct |
33 |
Correct |
14 ms |
1784 KB |
Output is correct |
34 |
Correct |
15 ms |
1784 KB |
Output is correct |
35 |
Correct |
17 ms |
1660 KB |
Output is correct |
36 |
Correct |
4 ms |
636 KB |
Output is correct |
37 |
Correct |
23 ms |
2680 KB |
Output is correct |
38 |
Correct |
23 ms |
2428 KB |
Output is correct |
39 |
Correct |
23 ms |
2424 KB |
Output is correct |
40 |
Correct |
24 ms |
2424 KB |
Output is correct |
41 |
Correct |
23 ms |
2424 KB |
Output is correct |
42 |
Correct |
8 ms |
632 KB |
Output is correct |
43 |
Correct |
9 ms |
760 KB |
Output is correct |
44 |
Correct |
8 ms |
760 KB |
Output is correct |
45 |
Correct |
53 ms |
4828 KB |
Output is correct |
46 |
Correct |
53 ms |
4728 KB |
Output is correct |
47 |
Correct |
81 ms |
10484 KB |
Output is correct |
48 |
Correct |
31 ms |
4340 KB |
Output is correct |
49 |
Correct |
22 ms |
3576 KB |
Output is correct |
50 |
Correct |
20 ms |
3704 KB |
Output is correct |
51 |
Correct |
53 ms |
6456 KB |
Output is correct |
52 |
Correct |
61 ms |
6776 KB |
Output is correct |
53 |
Correct |
23 ms |
3572 KB |
Output is correct |
54 |
Correct |
8 ms |
2296 KB |
Output is correct |
55 |
Correct |
11 ms |
2804 KB |
Output is correct |
56 |
Correct |
17 ms |
3832 KB |
Output is correct |
57 |
Correct |
48 ms |
10740 KB |
Output is correct |
58 |
Correct |
16 ms |
3056 KB |
Output is correct |
59 |
Correct |
18 ms |
3444 KB |
Output is correct |
60 |
Correct |
23 ms |
4084 KB |
Output is correct |
61 |
Correct |
24 ms |
3192 KB |
Output is correct |
62 |
Correct |
38 ms |
7980 KB |
Output is correct |
63 |
Correct |
224 ms |
23672 KB |
Output is correct |
64 |
Correct |
282 ms |
26740 KB |
Output is correct |
65 |
Correct |
431 ms |
36888 KB |
Output is correct |
66 |
Correct |
813 ms |
63076 KB |
Output is correct |
67 |
Correct |
807 ms |
62996 KB |
Output is correct |