#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e4 + 10;
set<int> pos[maxn];
vector<pair<int, int>> g[maxn];
using ll = long long;
ll d[maxn];
ll dijkstra(int src, int snk) {
memset(d, 1, sizeof d);
d[src] = 0;
using pii = pair<ll, int>;
priority_queue<pii, vector<pii>, greater<pii>> Q;
Q.push({0, src});
while (!Q.empty()) {
auto q = Q.top(); Q.pop();
if (q.second == snk) return q.first;
if (q.first != d[q.second]) continue;
for (auto e : g[q.second]) {
ll temp = q.first + e.second;
if (temp < d[e.first]) {
d[e.first] = temp;
Q.push({temp, e.first});
}
}
}
return -1;
}
int main() {
int n, m;
cin >> n >> m;
set<pair<int, int>> t;
int x = -1, y = -1;
for (int i = 0; i < m; ++i) {
int b, p;
cin >> b >> p;
t.insert({p, b});
pos[b].insert(p);
if (x == -1) x = b;
else if (y == -1) y = b;
}
auto same = [](int b, int p) {
return !pos[b].empty() && pos[b].count(p);
};
for (auto q : t) {
for (int i = 1; ; ++i) {
int temp = q.second + i * q.first;
if (temp >= n) break;
g[q.second].emplace_back(temp, i);
if (same(temp, q.first)) break;
}
for (int i = 1; ; ++i) {
int temp = q.second - i * q.first;
if (temp < 0) break;
g[q.second].emplace_back(temp, i);
if (same(temp, q.first)) break;
}
}
cout << dijkstra(x, y) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
3 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
4 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
3 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
5 ms |
2924 KB |
Output is correct |
12 |
Correct |
4 ms |
2668 KB |
Output is correct |
13 |
Correct |
3 ms |
2668 KB |
Output is correct |
14 |
Correct |
4 ms |
2924 KB |
Output is correct |
15 |
Correct |
5 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
3 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
5 ms |
2924 KB |
Output is correct |
12 |
Correct |
4 ms |
2668 KB |
Output is correct |
13 |
Correct |
3 ms |
2668 KB |
Output is correct |
14 |
Correct |
6 ms |
2924 KB |
Output is correct |
15 |
Correct |
5 ms |
2956 KB |
Output is correct |
16 |
Correct |
4 ms |
2796 KB |
Output is correct |
17 |
Correct |
6 ms |
3052 KB |
Output is correct |
18 |
Correct |
3 ms |
2796 KB |
Output is correct |
19 |
Correct |
3 ms |
2796 KB |
Output is correct |
20 |
Correct |
4 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
3 ms |
2796 KB |
Output is correct |
23 |
Correct |
3 ms |
2924 KB |
Output is correct |
24 |
Correct |
5 ms |
3052 KB |
Output is correct |
25 |
Correct |
5 ms |
2924 KB |
Output is correct |
26 |
Correct |
3 ms |
2796 KB |
Output is correct |
27 |
Correct |
4 ms |
2796 KB |
Output is correct |
28 |
Correct |
5 ms |
3052 KB |
Output is correct |
29 |
Correct |
6 ms |
3700 KB |
Output is correct |
30 |
Correct |
4 ms |
3052 KB |
Output is correct |
31 |
Correct |
4 ms |
3308 KB |
Output is correct |
32 |
Correct |
4 ms |
3200 KB |
Output is correct |
33 |
Correct |
9 ms |
4752 KB |
Output is correct |
34 |
Correct |
8 ms |
4732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2672 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
3 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
5 ms |
2924 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
4 ms |
2668 KB |
Output is correct |
14 |
Correct |
6 ms |
2924 KB |
Output is correct |
15 |
Correct |
4 ms |
2884 KB |
Output is correct |
16 |
Correct |
3 ms |
2796 KB |
Output is correct |
17 |
Correct |
5 ms |
3052 KB |
Output is correct |
18 |
Correct |
4 ms |
2884 KB |
Output is correct |
19 |
Correct |
3 ms |
2796 KB |
Output is correct |
20 |
Correct |
4 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
3 ms |
2796 KB |
Output is correct |
23 |
Correct |
4 ms |
2924 KB |
Output is correct |
24 |
Correct |
5 ms |
3052 KB |
Output is correct |
25 |
Correct |
4 ms |
2924 KB |
Output is correct |
26 |
Correct |
3 ms |
2796 KB |
Output is correct |
27 |
Correct |
4 ms |
2796 KB |
Output is correct |
28 |
Correct |
5 ms |
3052 KB |
Output is correct |
29 |
Correct |
7 ms |
3700 KB |
Output is correct |
30 |
Correct |
4 ms |
3052 KB |
Output is correct |
31 |
Correct |
4 ms |
3308 KB |
Output is correct |
32 |
Correct |
4 ms |
3180 KB |
Output is correct |
33 |
Correct |
9 ms |
4732 KB |
Output is correct |
34 |
Correct |
8 ms |
4732 KB |
Output is correct |
35 |
Correct |
41 ms |
6388 KB |
Output is correct |
36 |
Correct |
9 ms |
3308 KB |
Output is correct |
37 |
Correct |
37 ms |
6892 KB |
Output is correct |
38 |
Correct |
54 ms |
7660 KB |
Output is correct |
39 |
Correct |
52 ms |
7524 KB |
Output is correct |
40 |
Correct |
50 ms |
7532 KB |
Output is correct |
41 |
Correct |
57 ms |
7660 KB |
Output is correct |
42 |
Correct |
17 ms |
3052 KB |
Output is correct |
43 |
Correct |
17 ms |
3052 KB |
Output is correct |
44 |
Correct |
21 ms |
3324 KB |
Output is correct |
45 |
Correct |
58 ms |
10584 KB |
Output is correct |
46 |
Correct |
59 ms |
10620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
4 ms |
2876 KB |
Output is correct |
12 |
Correct |
3 ms |
2696 KB |
Output is correct |
13 |
Correct |
3 ms |
2668 KB |
Output is correct |
14 |
Correct |
6 ms |
2924 KB |
Output is correct |
15 |
Correct |
4 ms |
2924 KB |
Output is correct |
16 |
Correct |
4 ms |
2796 KB |
Output is correct |
17 |
Correct |
5 ms |
3052 KB |
Output is correct |
18 |
Correct |
5 ms |
2796 KB |
Output is correct |
19 |
Correct |
3 ms |
2944 KB |
Output is correct |
20 |
Correct |
4 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
4 ms |
2796 KB |
Output is correct |
23 |
Correct |
4 ms |
3072 KB |
Output is correct |
24 |
Correct |
6 ms |
3052 KB |
Output is correct |
25 |
Correct |
5 ms |
2924 KB |
Output is correct |
26 |
Correct |
5 ms |
2796 KB |
Output is correct |
27 |
Correct |
6 ms |
2796 KB |
Output is correct |
28 |
Correct |
5 ms |
3052 KB |
Output is correct |
29 |
Correct |
6 ms |
3700 KB |
Output is correct |
30 |
Correct |
3 ms |
3052 KB |
Output is correct |
31 |
Correct |
5 ms |
3308 KB |
Output is correct |
32 |
Correct |
4 ms |
3180 KB |
Output is correct |
33 |
Correct |
11 ms |
4732 KB |
Output is correct |
34 |
Correct |
8 ms |
4692 KB |
Output is correct |
35 |
Correct |
52 ms |
6380 KB |
Output is correct |
36 |
Correct |
10 ms |
3308 KB |
Output is correct |
37 |
Correct |
39 ms |
6892 KB |
Output is correct |
38 |
Correct |
65 ms |
7636 KB |
Output is correct |
39 |
Correct |
56 ms |
7532 KB |
Output is correct |
40 |
Correct |
50 ms |
7532 KB |
Output is correct |
41 |
Correct |
65 ms |
7660 KB |
Output is correct |
42 |
Correct |
19 ms |
3052 KB |
Output is correct |
43 |
Correct |
17 ms |
3052 KB |
Output is correct |
44 |
Correct |
18 ms |
3180 KB |
Output is correct |
45 |
Correct |
57 ms |
10600 KB |
Output is correct |
46 |
Correct |
54 ms |
10620 KB |
Output is correct |
47 |
Correct |
120 ms |
14940 KB |
Output is correct |
48 |
Correct |
44 ms |
7336 KB |
Output is correct |
49 |
Correct |
47 ms |
6380 KB |
Output is correct |
50 |
Correct |
35 ms |
5740 KB |
Output is correct |
51 |
Correct |
78 ms |
10304 KB |
Output is correct |
52 |
Correct |
117 ms |
10840 KB |
Output is correct |
53 |
Correct |
53 ms |
7028 KB |
Output is correct |
54 |
Correct |
4 ms |
3308 KB |
Output is correct |
55 |
Correct |
6 ms |
3820 KB |
Output is correct |
56 |
Correct |
42 ms |
6636 KB |
Output is correct |
57 |
Correct |
20 ms |
11888 KB |
Output is correct |
58 |
Correct |
17 ms |
4108 KB |
Output is correct |
59 |
Correct |
27 ms |
4336 KB |
Output is correct |
60 |
Correct |
22 ms |
5116 KB |
Output is correct |
61 |
Correct |
27 ms |
4552 KB |
Output is correct |
62 |
Correct |
72 ms |
10856 KB |
Output is correct |
63 |
Correct |
175 ms |
31452 KB |
Output is correct |
64 |
Correct |
160 ms |
39904 KB |
Output is correct |
65 |
Correct |
199 ms |
49860 KB |
Output is correct |
66 |
Correct |
260 ms |
79724 KB |
Output is correct |
67 |
Correct |
291 ms |
79724 KB |
Output is correct |