#include<bits/stdc++.h>
using namespace std;
#define dlwlrma ios::sync_with_stdio(0); cin.tie(0);
#define pii pair<int,int>
#define f first
#define s second
const int mxN = 30002;
int n,m,src,dest,vis[mxN];
set<int>doge[mxN];
vector<pii>chu[mxN];
priority_queue<pii>pq;
int main(){
dlwlrma
cin >> n >> m;
for(int i = 0; i < m; i++){
int b,p;
cin >> b >> p;
if(i==0)
src=b;
if(i==1)
dest=b;
doge[b].insert(p);
}
for(int i = 0; i < n; i++){
for(int next : doge[i]){
for(int j = i + next, cnt = 1; j < n; j+=next,cnt++){
chu[i].push_back({j,cnt});
if(doge[j].find(next)!=doge[j].end())
break;
}
for(int j = i - next, cnt = 1; j >= 0; j-=next, cnt++){
chu[i].push_back({j,cnt});
if(doge[j].find(next)!=doge[j].end())
break;
}
}
}
memset(vis,0x3f,sizeof(vis));
pq.push({0,src});
vis[src]=0;
while(!pq.empty()){
pii cur = pq.top(); pq.pop();
int dist = cur.f, node = cur.s;
if(node == dest){
cout << -dist << "\n";
return 0;
}
for(pii next : chu[node]){
if(next.s - dist < vis[next.f]){
vis[next.f]= next.s - dist;
pq.push({dist-next.s,next.f});
}
}
}
cout << -1 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2560 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
2 ms |
2560 KB |
Output is correct |
6 |
Correct |
2 ms |
2560 KB |
Output is correct |
7 |
Correct |
2 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2592 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
2 ms |
2560 KB |
Output is correct |
6 |
Correct |
2 ms |
2560 KB |
Output is correct |
7 |
Correct |
2 ms |
2560 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2592 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2560 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
2 ms |
2560 KB |
Output is correct |
6 |
Correct |
2 ms |
2560 KB |
Output is correct |
7 |
Correct |
2 ms |
2560 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2592 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2816 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2720 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2720 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2688 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2944 KB |
Output is correct |
29 |
Correct |
5 ms |
3328 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
3 ms |
3072 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
3964 KB |
Output is correct |
34 |
Correct |
6 ms |
3964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2560 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
3 ms |
2560 KB |
Output is correct |
6 |
Correct |
2 ms |
2560 KB |
Output is correct |
7 |
Correct |
2 ms |
2560 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2816 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2944 KB |
Output is correct |
29 |
Correct |
5 ms |
3328 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
4 ms |
3072 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
3964 KB |
Output is correct |
34 |
Correct |
6 ms |
3964 KB |
Output is correct |
35 |
Correct |
19 ms |
4992 KB |
Output is correct |
36 |
Correct |
4 ms |
2944 KB |
Output is correct |
37 |
Correct |
21 ms |
5760 KB |
Output is correct |
38 |
Correct |
26 ms |
6016 KB |
Output is correct |
39 |
Correct |
25 ms |
6016 KB |
Output is correct |
40 |
Correct |
25 ms |
6016 KB |
Output is correct |
41 |
Correct |
26 ms |
6008 KB |
Output is correct |
42 |
Correct |
7 ms |
2944 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2944 KB |
Output is correct |
45 |
Correct |
29 ms |
8576 KB |
Output is correct |
46 |
Correct |
28 ms |
8576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2560 KB |
Output is correct |
2 |
Correct |
2 ms |
2560 KB |
Output is correct |
3 |
Correct |
2 ms |
2560 KB |
Output is correct |
4 |
Correct |
2 ms |
2560 KB |
Output is correct |
5 |
Correct |
2 ms |
2560 KB |
Output is correct |
6 |
Correct |
3 ms |
2560 KB |
Output is correct |
7 |
Correct |
2 ms |
2560 KB |
Output is correct |
8 |
Correct |
2 ms |
2560 KB |
Output is correct |
9 |
Correct |
2 ms |
2560 KB |
Output is correct |
10 |
Correct |
2 ms |
2560 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
3 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
3 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2720 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2944 KB |
Output is correct |
29 |
Correct |
5 ms |
3328 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
4 ms |
3072 KB |
Output is correct |
32 |
Correct |
4 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
3964 KB |
Output is correct |
34 |
Correct |
7 ms |
3964 KB |
Output is correct |
35 |
Correct |
19 ms |
4992 KB |
Output is correct |
36 |
Correct |
4 ms |
2944 KB |
Output is correct |
37 |
Correct |
21 ms |
5760 KB |
Output is correct |
38 |
Correct |
33 ms |
6008 KB |
Output is correct |
39 |
Correct |
25 ms |
6016 KB |
Output is correct |
40 |
Correct |
25 ms |
6016 KB |
Output is correct |
41 |
Correct |
26 ms |
6016 KB |
Output is correct |
42 |
Correct |
9 ms |
2944 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2944 KB |
Output is correct |
45 |
Correct |
30 ms |
8576 KB |
Output is correct |
46 |
Correct |
27 ms |
8568 KB |
Output is correct |
47 |
Correct |
48 ms |
12792 KB |
Output is correct |
48 |
Correct |
20 ms |
6268 KB |
Output is correct |
49 |
Correct |
19 ms |
5376 KB |
Output is correct |
50 |
Correct |
13 ms |
4864 KB |
Output is correct |
51 |
Correct |
36 ms |
8204 KB |
Output is correct |
52 |
Correct |
39 ms |
8696 KB |
Output is correct |
53 |
Correct |
17 ms |
5372 KB |
Output is correct |
54 |
Correct |
4 ms |
3200 KB |
Output is correct |
55 |
Correct |
5 ms |
3452 KB |
Output is correct |
56 |
Correct |
15 ms |
5120 KB |
Output is correct |
57 |
Correct |
20 ms |
11392 KB |
Output is correct |
58 |
Correct |
10 ms |
3704 KB |
Output is correct |
59 |
Correct |
11 ms |
4092 KB |
Output is correct |
60 |
Correct |
12 ms |
4860 KB |
Output is correct |
61 |
Correct |
10 ms |
4352 KB |
Output is correct |
62 |
Correct |
29 ms |
9468 KB |
Output is correct |
63 |
Correct |
80 ms |
25584 KB |
Output is correct |
64 |
Correct |
85 ms |
28912 KB |
Output is correct |
65 |
Correct |
114 ms |
38508 KB |
Output is correct |
66 |
Correct |
217 ms |
65504 KB |
Output is correct |
67 |
Correct |
203 ms |
65504 KB |
Output is correct |