#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,m,st,ed,dis[30001];
bool vis[30001];
set<int> s[30001];
vector<pii> adj[30001];
priority_queue<pii, vector<pii>, greater<pii>> q;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for (int i=0; i<m; ++i) {
int a,b; cin>>a>>b;
s[a].insert(b);
if (i==0) st=a;
if (i==1) ed=a;
}
for (int i=0; i<n; ++i) {
for (auto it=s[i].begin(); it!=s[i].end(); ++it) {
int power=*it;
//forward
for (int j=1; j<=n; ++j) {
int nxt=i+power*j;
if (nxt>n-1) break;
adj[i].push_back(pii(nxt,j));
if (s[nxt].find(power)!=s[nxt].end()) break;
}
//backward
for (int j=1; j<=n; ++j) {
int nxt=i+power*(-j);
if (nxt<0) break;
adj[i].push_back(pii(nxt,j));
if (s[nxt].find(power)!=s[nxt].end()) break;
}
}
}
memset(dis,0x3f,sizeof(dis));
dis[st]=0;
q.push(pii(0,st));
while (!q.empty()) {
int node=q.top().second;
q.pop();
if (vis[node]) continue;
vis[node]=true;
if (node==ed) {
cout<<dis[ed];
return 0;
}
for (auto k : adj[node]) {
int des=k.first, w=k.second;
if (dis[des]>dis[node]+w) {
dis[des]=dis[node]+w;
q.push(pii(dis[des],des));
}
}
}
cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2516 KB |
Output is correct |
2 |
Correct |
2 ms |
2544 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2440 KB |
Output is correct |
6 |
Correct |
2 ms |
2440 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2516 KB |
Output is correct |
2 |
Correct |
2 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2516 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2444 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2436 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
3 ms |
2700 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2516 KB |
Output is correct |
2 |
Correct |
1 ms |
2516 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2516 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2444 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2572 KB |
Output is correct |
12 |
Correct |
2 ms |
2568 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2708 KB |
Output is correct |
21 |
Correct |
2 ms |
2576 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2756 KB |
Output is correct |
26 |
Correct |
2 ms |
2576 KB |
Output is correct |
27 |
Correct |
2 ms |
2576 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
3 ms |
2900 KB |
Output is correct |
33 |
Correct |
6 ms |
4116 KB |
Output is correct |
34 |
Correct |
5 ms |
4052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2516 KB |
Output is correct |
2 |
Correct |
1 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2516 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2516 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2448 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2700 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2572 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2568 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2756 KB |
Output is correct |
26 |
Correct |
2 ms |
2584 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
3024 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
3 ms |
2824 KB |
Output is correct |
31 |
Correct |
4 ms |
3028 KB |
Output is correct |
32 |
Correct |
3 ms |
2824 KB |
Output is correct |
33 |
Correct |
6 ms |
4052 KB |
Output is correct |
34 |
Correct |
7 ms |
4052 KB |
Output is correct |
35 |
Correct |
13 ms |
4948 KB |
Output is correct |
36 |
Correct |
3 ms |
2900 KB |
Output is correct |
37 |
Correct |
17 ms |
5632 KB |
Output is correct |
38 |
Correct |
19 ms |
5904 KB |
Output is correct |
39 |
Correct |
18 ms |
5912 KB |
Output is correct |
40 |
Correct |
18 ms |
5852 KB |
Output is correct |
41 |
Correct |
21 ms |
5972 KB |
Output is correct |
42 |
Correct |
5 ms |
2772 KB |
Output is correct |
43 |
Correct |
6 ms |
2784 KB |
Output is correct |
44 |
Correct |
5 ms |
2832 KB |
Output is correct |
45 |
Correct |
22 ms |
8584 KB |
Output is correct |
46 |
Correct |
22 ms |
8572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2516 KB |
Output is correct |
2 |
Correct |
1 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2516 KB |
Output is correct |
6 |
Correct |
2 ms |
2436 KB |
Output is correct |
7 |
Correct |
2 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2444 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2572 KB |
Output is correct |
17 |
Correct |
3 ms |
2832 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2568 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
3 ms |
2644 KB |
Output is correct |
24 |
Correct |
3 ms |
2704 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
3 ms |
2800 KB |
Output is correct |
31 |
Correct |
3 ms |
2952 KB |
Output is correct |
32 |
Correct |
3 ms |
2820 KB |
Output is correct |
33 |
Correct |
8 ms |
4052 KB |
Output is correct |
34 |
Correct |
5 ms |
4052 KB |
Output is correct |
35 |
Correct |
14 ms |
4948 KB |
Output is correct |
36 |
Correct |
4 ms |
2900 KB |
Output is correct |
37 |
Correct |
16 ms |
5664 KB |
Output is correct |
38 |
Correct |
18 ms |
5964 KB |
Output is correct |
39 |
Correct |
19 ms |
5904 KB |
Output is correct |
40 |
Correct |
21 ms |
5784 KB |
Output is correct |
41 |
Correct |
18 ms |
5972 KB |
Output is correct |
42 |
Correct |
6 ms |
2772 KB |
Output is correct |
43 |
Correct |
6 ms |
2772 KB |
Output is correct |
44 |
Correct |
6 ms |
2836 KB |
Output is correct |
45 |
Correct |
23 ms |
8576 KB |
Output is correct |
46 |
Correct |
20 ms |
8552 KB |
Output is correct |
47 |
Correct |
35 ms |
12972 KB |
Output is correct |
48 |
Correct |
15 ms |
6216 KB |
Output is correct |
49 |
Correct |
12 ms |
5204 KB |
Output is correct |
50 |
Correct |
11 ms |
4820 KB |
Output is correct |
51 |
Correct |
28 ms |
8332 KB |
Output is correct |
52 |
Correct |
26 ms |
8788 KB |
Output is correct |
53 |
Correct |
11 ms |
5328 KB |
Output is correct |
54 |
Correct |
3 ms |
3028 KB |
Output is correct |
55 |
Correct |
5 ms |
3408 KB |
Output is correct |
56 |
Correct |
12 ms |
5144 KB |
Output is correct |
57 |
Correct |
15 ms |
11344 KB |
Output is correct |
58 |
Correct |
9 ms |
3668 KB |
Output is correct |
59 |
Correct |
9 ms |
4096 KB |
Output is correct |
60 |
Correct |
10 ms |
4760 KB |
Output is correct |
61 |
Correct |
9 ms |
4128 KB |
Output is correct |
62 |
Correct |
22 ms |
9436 KB |
Output is correct |
63 |
Correct |
66 ms |
26664 KB |
Output is correct |
64 |
Correct |
72 ms |
29776 KB |
Output is correct |
65 |
Correct |
96 ms |
39436 KB |
Output is correct |
66 |
Correct |
181 ms |
67756 KB |
Output is correct |
67 |
Correct |
146 ms |
67832 KB |
Output is correct |