#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9 + 12;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> v(m);
for(auto &z : v) cin >> z.first >> z.second;
set<pair<int, int>> where[n];
for(auto z : v) {
where[z.first].insert(z);
}
vector<pair<int, int>> adj[n];
for(int i = 0; i < n; ++i) {
for(auto z : where[i]) {
if(z.second == 0) continue;
for(int j = 1; j < n; ++j) {
int nx = j * z.second + i;
if(nx >= n) break;
adj[i].push_back({nx, j});
if(where[nx].count({nx, z.second})) break;
}
for(int j = 1; j < n; ++j) {
int nx = -j * z.second + i;
if(nx < 0) break;
adj[i].push_back({nx, j});
if(where[nx].count({nx, z.second})) break;
}
}
}
// for(int i = 0; i < n; ++i) {
// cerr << i << ": ";
// for(auto z : adj[i]) {
// cerr << "{" << z.first << ", " << z.second << "}";
// cerr << ", ";
// }
// cerr << endl;
// }
auto calc = [&](int start, int end) {
vector<int> dist(n, INF);
dist[start] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0, start});
while(!pq.empty()) {
auto tren = pq.top();
pq.pop();
if(dist[tren.second] != tren.first) continue;
for(auto e : adj[tren.second]) {
if(dist[e.first] > dist[tren.second] + e.second) {
dist[e.first] = dist[tren.second] + e.second;
pq.push({dist[e.first], e.first});
}
}
}
return dist[end];
};
int ans = calc(v[0].first, v[1].first);
cout << (ans >= INF ? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
268 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
712 KB |
Output is correct |
26 |
Correct |
1 ms |
584 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
2116 KB |
Output is correct |
34 |
Correct |
5 ms |
2012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
584 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
832 KB |
Output is correct |
32 |
Correct |
2 ms |
820 KB |
Output is correct |
33 |
Correct |
5 ms |
2012 KB |
Output is correct |
34 |
Correct |
5 ms |
2012 KB |
Output is correct |
35 |
Correct |
14 ms |
3072 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
17 ms |
3664 KB |
Output is correct |
38 |
Correct |
19 ms |
4128 KB |
Output is correct |
39 |
Correct |
21 ms |
4180 KB |
Output is correct |
40 |
Correct |
20 ms |
4044 KB |
Output is correct |
41 |
Correct |
21 ms |
4044 KB |
Output is correct |
42 |
Correct |
4 ms |
980 KB |
Output is correct |
43 |
Correct |
4 ms |
972 KB |
Output is correct |
44 |
Correct |
4 ms |
1032 KB |
Output is correct |
45 |
Correct |
20 ms |
6732 KB |
Output is correct |
46 |
Correct |
20 ms |
6708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
584 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
2012 KB |
Output is correct |
34 |
Correct |
5 ms |
2012 KB |
Output is correct |
35 |
Correct |
14 ms |
3028 KB |
Output is correct |
36 |
Correct |
2 ms |
776 KB |
Output is correct |
37 |
Correct |
15 ms |
3660 KB |
Output is correct |
38 |
Correct |
18 ms |
4012 KB |
Output is correct |
39 |
Correct |
18 ms |
4052 KB |
Output is correct |
40 |
Correct |
18 ms |
4040 KB |
Output is correct |
41 |
Correct |
18 ms |
4016 KB |
Output is correct |
42 |
Correct |
5 ms |
972 KB |
Output is correct |
43 |
Correct |
4 ms |
976 KB |
Output is correct |
44 |
Correct |
5 ms |
980 KB |
Output is correct |
45 |
Correct |
20 ms |
6700 KB |
Output is correct |
46 |
Correct |
20 ms |
6792 KB |
Output is correct |
47 |
Correct |
41 ms |
11848 KB |
Output is correct |
48 |
Correct |
14 ms |
5880 KB |
Output is correct |
49 |
Correct |
11 ms |
5092 KB |
Output is correct |
50 |
Correct |
9 ms |
4820 KB |
Output is correct |
51 |
Correct |
35 ms |
8408 KB |
Output is correct |
52 |
Correct |
33 ms |
8884 KB |
Output is correct |
53 |
Correct |
16 ms |
5892 KB |
Output is correct |
54 |
Correct |
3 ms |
3028 KB |
Output is correct |
55 |
Correct |
4 ms |
3408 KB |
Output is correct |
56 |
Correct |
11 ms |
5332 KB |
Output is correct |
57 |
Correct |
31 ms |
11652 KB |
Output is correct |
58 |
Correct |
8 ms |
3960 KB |
Output is correct |
59 |
Correct |
9 ms |
4296 KB |
Output is correct |
60 |
Correct |
10 ms |
4944 KB |
Output is correct |
61 |
Correct |
10 ms |
4436 KB |
Output is correct |
62 |
Correct |
19 ms |
9576 KB |
Output is correct |
63 |
Correct |
66 ms |
26856 KB |
Output is correct |
64 |
Correct |
75 ms |
29908 KB |
Output is correct |
65 |
Correct |
95 ms |
39724 KB |
Output is correct |
66 |
Correct |
178 ms |
68080 KB |
Output is correct |
67 |
Correct |
182 ms |
68020 KB |
Output is correct |