#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MAXN = 3e4+10;
const int INF = 1e9+10;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int en, n, m;
int dis[MAXN], a[MAXN], b[MAXN];
vector <int> adj[MAXN];
priority_queue <pii,vector<pii>,greater<pii>> pq;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i=1; i<=m; i++){
cin >> a[i] >> b[i]; a[i]++;
adj[a[i]].pb(b[i]);
if(i==1) pq.push({0, a[i]});
if(i==2) en = a[i];
}
for(int i=1; i<=n; i++){
sort(adj[i].begin(), adj[i].end());
adj[i].resize(unique(adj[i].begin(), adj[i].end()) - adj[i].begin());
dis[i] = INF;
}
dis[a[1]] = 0;
while(!pq.empty()){
int dist = pq.top().fi, nw = pq.top().se; pq.pop();
if(nw==en){
cout << dis[nw] << '\n';
exit(0);
}
if(dist > dis[nw]) continue;
for(auto pow : adj[nw]){
int cnt = 1;
for(int i=nw+pow; i<=n; i+=pow){
if(dis[i] > dis[nw]+cnt){
dis[i] = dis[nw]+cnt;
pq.push({dis[i], i});
}
cnt++;
}
cnt = 1;
for(int i=nw-pow; i>=1; i-=pow){
if(dis[i] > dis[nw]+cnt){
dis[i] = dis[nw]+cnt;
pq.push({dis[i], i});
}
cnt++;
}
}
}
cout << "-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1168 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
1 ms |
1164 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 |
1372 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 |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 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 |
1216 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1284 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1572 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1504 KB |
Output is correct |
34 |
Correct |
2 ms |
1504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1172 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 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 |
1176 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 |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1176 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1168 KB |
Output is correct |
33 |
Correct |
4 ms |
1504 KB |
Output is correct |
34 |
Correct |
2 ms |
1432 KB |
Output is correct |
35 |
Correct |
6 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
5 ms |
1628 KB |
Output is correct |
38 |
Correct |
5 ms |
1780 KB |
Output is correct |
39 |
Correct |
5 ms |
1884 KB |
Output is correct |
40 |
Correct |
5 ms |
1884 KB |
Output is correct |
41 |
Correct |
5 ms |
1884 KB |
Output is correct |
42 |
Correct |
4 ms |
1628 KB |
Output is correct |
43 |
Correct |
4 ms |
1628 KB |
Output is correct |
44 |
Correct |
6 ms |
1624 KB |
Output is correct |
45 |
Correct |
7 ms |
2140 KB |
Output is correct |
46 |
Correct |
6 ms |
2092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1168 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1164 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 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 |
1368 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1232 KB |
Output is correct |
16 |
Correct |
1 ms |
1172 KB |
Output is correct |
17 |
Correct |
1 ms |
1176 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1176 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1180 KB |
Output is correct |
29 |
Correct |
2 ms |
1376 KB |
Output is correct |
30 |
Correct |
1 ms |
1112 KB |
Output is correct |
31 |
Correct |
2 ms |
1128 KB |
Output is correct |
32 |
Correct |
1 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1516 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
5 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1112 KB |
Output is correct |
37 |
Correct |
4 ms |
1628 KB |
Output is correct |
38 |
Correct |
5 ms |
1884 KB |
Output is correct |
39 |
Correct |
5 ms |
1884 KB |
Output is correct |
40 |
Correct |
5 ms |
1852 KB |
Output is correct |
41 |
Correct |
5 ms |
1948 KB |
Output is correct |
42 |
Correct |
4 ms |
1624 KB |
Output is correct |
43 |
Correct |
4 ms |
1628 KB |
Output is correct |
44 |
Correct |
7 ms |
1628 KB |
Output is correct |
45 |
Correct |
7 ms |
2140 KB |
Output is correct |
46 |
Correct |
6 ms |
2012 KB |
Output is correct |
47 |
Correct |
9 ms |
2536 KB |
Output is correct |
48 |
Correct |
5 ms |
1880 KB |
Output is correct |
49 |
Correct |
5 ms |
1880 KB |
Output is correct |
50 |
Correct |
4 ms |
1884 KB |
Output is correct |
51 |
Correct |
14 ms |
3436 KB |
Output is correct |
52 |
Correct |
12 ms |
3004 KB |
Output is correct |
53 |
Correct |
8 ms |
2652 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
518 ms |
3040 KB |
Output is correct |
57 |
Correct |
3 ms |
1908 KB |
Output is correct |
58 |
Correct |
6 ms |
1884 KB |
Output is correct |
59 |
Correct |
7 ms |
1948 KB |
Output is correct |
60 |
Correct |
6 ms |
1884 KB |
Output is correct |
61 |
Correct |
6 ms |
1912 KB |
Output is correct |
62 |
Correct |
7 ms |
2652 KB |
Output is correct |
63 |
Correct |
24 ms |
4056 KB |
Output is correct |
64 |
Correct |
25 ms |
4048 KB |
Output is correct |
65 |
Correct |
31 ms |
4056 KB |
Output is correct |
66 |
Correct |
70 ms |
7116 KB |
Output is correct |
67 |
Correct |
42 ms |
7832 KB |
Output is correct |