#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define sp ' '
#define nl '\n'
void sc(){}template<class T,class...A>void sc(T&t,A&...a){cin>>t,sc(a...);}
void pr(){}template<class T,class...A>void pr(T t,A...a){cout<<t,pr(a...);}
#define ms(x, y) memset(x, y, sizeof(x))
#define sz(x) (int)(x.size())
#define af(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
const int mod = 1e9 + 7, base = 131;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MM = 3e4 + 5;
int N, M, st, ed, dist[MM];
unordered_set<int> s[MM];
priority_queue<pii, vector<pii>, greater<pii>> q;
int main(){
cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
sc(N, M);
for (int i = 0, a, b; i < M; i++){
sc(a, b);
if (i == 0) st = a;
if (i == 1) ed = a;
s[a].insert(b);
}
ms(dist, INF); dist[st] = 0;
q.push({0, st});
while(sz(q)){
int p = q.top().s, d = q.top().f; q.pop();
if (p == ed){pr(d); return 0;}
if (d > dist[p]) continue;
for (auto &i : s[p]){
for (int j = 1; p + i * j < N; j++){
if (dist[p + i * j] > dist[p] + j){
dist[p + i * j] = dist[p] + j;
q.push({dist[p + i * j], p + i * j});
}
if (s[p + i * j].count(i)) break;
}
for (int j = 1; p - i * j >= 0; j++){
if (dist[p - i * j] > dist[p] + j){
dist[p - i * j] = dist[p] + j;
q.push({dist[p - i * j], p - i * j});
}
if (s[p - i * j].count(i)) break;
}
}
}
pr(-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2168 KB |
Output is correct |
2 |
Correct |
8 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2168 KB |
Output is correct |
11 |
Correct |
6 ms |
2168 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2040 KB |
Output is correct |
14 |
Correct |
6 ms |
2168 KB |
Output is correct |
15 |
Correct |
7 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2044 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
7 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2044 KB |
Output is correct |
11 |
Correct |
6 ms |
2168 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Correct |
6 ms |
2040 KB |
Output is correct |
14 |
Correct |
6 ms |
2168 KB |
Output is correct |
15 |
Correct |
6 ms |
2168 KB |
Output is correct |
16 |
Correct |
6 ms |
2168 KB |
Output is correct |
17 |
Correct |
7 ms |
2168 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
6 ms |
2168 KB |
Output is correct |
21 |
Correct |
6 ms |
2168 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
7 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2168 KB |
Output is correct |
25 |
Correct |
7 ms |
2168 KB |
Output is correct |
26 |
Correct |
6 ms |
2168 KB |
Output is correct |
27 |
Correct |
6 ms |
2168 KB |
Output is correct |
28 |
Correct |
7 ms |
2296 KB |
Output is correct |
29 |
Correct |
8 ms |
2296 KB |
Output is correct |
30 |
Correct |
6 ms |
2168 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2168 KB |
Output is correct |
33 |
Correct |
8 ms |
2424 KB |
Output is correct |
34 |
Correct |
8 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2172 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2168 KB |
Output is correct |
11 |
Correct |
7 ms |
2168 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2040 KB |
Output is correct |
14 |
Correct |
6 ms |
2172 KB |
Output is correct |
15 |
Correct |
7 ms |
2168 KB |
Output is correct |
16 |
Correct |
7 ms |
2168 KB |
Output is correct |
17 |
Correct |
7 ms |
2168 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
6 ms |
2168 KB |
Output is correct |
21 |
Correct |
6 ms |
2168 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
7 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2296 KB |
Output is correct |
25 |
Correct |
7 ms |
2168 KB |
Output is correct |
26 |
Correct |
6 ms |
2168 KB |
Output is correct |
27 |
Correct |
6 ms |
2168 KB |
Output is correct |
28 |
Correct |
7 ms |
2168 KB |
Output is correct |
29 |
Correct |
8 ms |
2296 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
6 ms |
2168 KB |
Output is correct |
33 |
Correct |
9 ms |
2424 KB |
Output is correct |
34 |
Correct |
8 ms |
2424 KB |
Output is correct |
35 |
Correct |
19 ms |
3576 KB |
Output is correct |
36 |
Correct |
8 ms |
2424 KB |
Output is correct |
37 |
Correct |
18 ms |
3192 KB |
Output is correct |
38 |
Correct |
20 ms |
3832 KB |
Output is correct |
39 |
Correct |
17 ms |
3704 KB |
Output is correct |
40 |
Correct |
17 ms |
3704 KB |
Output is correct |
41 |
Correct |
18 ms |
3832 KB |
Output is correct |
42 |
Correct |
10 ms |
2296 KB |
Output is correct |
43 |
Correct |
10 ms |
2296 KB |
Output is correct |
44 |
Correct |
11 ms |
2424 KB |
Output is correct |
45 |
Correct |
21 ms |
3752 KB |
Output is correct |
46 |
Correct |
20 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
7 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2168 KB |
Output is correct |
11 |
Correct |
6 ms |
2168 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2040 KB |
Output is correct |
14 |
Correct |
6 ms |
2168 KB |
Output is correct |
15 |
Correct |
6 ms |
2168 KB |
Output is correct |
16 |
Correct |
6 ms |
2168 KB |
Output is correct |
17 |
Correct |
7 ms |
2168 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
7 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
2168 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
7 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2296 KB |
Output is correct |
25 |
Correct |
7 ms |
2168 KB |
Output is correct |
26 |
Correct |
6 ms |
2168 KB |
Output is correct |
27 |
Correct |
6 ms |
2172 KB |
Output is correct |
28 |
Correct |
7 ms |
2296 KB |
Output is correct |
29 |
Correct |
9 ms |
2296 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2168 KB |
Output is correct |
33 |
Correct |
9 ms |
2424 KB |
Output is correct |
34 |
Correct |
8 ms |
2424 KB |
Output is correct |
35 |
Correct |
18 ms |
3480 KB |
Output is correct |
36 |
Correct |
8 ms |
2424 KB |
Output is correct |
37 |
Correct |
16 ms |
3192 KB |
Output is correct |
38 |
Correct |
19 ms |
3704 KB |
Output is correct |
39 |
Correct |
16 ms |
3704 KB |
Output is correct |
40 |
Correct |
17 ms |
3704 KB |
Output is correct |
41 |
Correct |
17 ms |
3832 KB |
Output is correct |
42 |
Correct |
10 ms |
2296 KB |
Output is correct |
43 |
Correct |
11 ms |
2296 KB |
Output is correct |
44 |
Correct |
12 ms |
2424 KB |
Output is correct |
45 |
Correct |
20 ms |
3832 KB |
Output is correct |
46 |
Correct |
20 ms |
3832 KB |
Output is correct |
47 |
Correct |
26 ms |
4216 KB |
Output is correct |
48 |
Correct |
16 ms |
3576 KB |
Output is correct |
49 |
Correct |
15 ms |
3452 KB |
Output is correct |
50 |
Correct |
12 ms |
3064 KB |
Output is correct |
51 |
Correct |
31 ms |
4728 KB |
Output is correct |
52 |
Correct |
46 ms |
4728 KB |
Output is correct |
53 |
Correct |
21 ms |
4344 KB |
Output is correct |
54 |
Correct |
7 ms |
2296 KB |
Output is correct |
55 |
Correct |
8 ms |
2296 KB |
Output is correct |
56 |
Correct |
20 ms |
4216 KB |
Output is correct |
57 |
Correct |
8 ms |
2680 KB |
Output is correct |
58 |
Correct |
12 ms |
2424 KB |
Output is correct |
59 |
Correct |
14 ms |
2552 KB |
Output is correct |
60 |
Correct |
13 ms |
2552 KB |
Output is correct |
61 |
Correct |
14 ms |
2680 KB |
Output is correct |
62 |
Correct |
27 ms |
4344 KB |
Output is correct |
63 |
Correct |
60 ms |
4852 KB |
Output is correct |
64 |
Correct |
70 ms |
4848 KB |
Output is correct |
65 |
Correct |
77 ms |
5744 KB |
Output is correct |
66 |
Correct |
110 ms |
5744 KB |
Output is correct |
67 |
Correct |
98 ms |
5744 KB |
Output is correct |