#include<cstdio>
#include<vector>
#include<queue>
#define INF 0x7fffffff
#define f first
#define s second
using namespace std;
vector<int>cg[50005];
priority_queue<pair<int,int> >pq;
int dist[30005],ed[30005][2];
int n,m,a,b,cur,val,tmp;
bool chk[30005][5005];
int main()
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<m;i++) {
scanf("%d%d",&ed[i][0],&ed[i][1]);
if(ed[i][1]>5000 || !chk[ed[i][0]][ed[i][1]])
cg[ed[i][0]].push_back(ed[i][1]);
if(ed[i][1]<=5000) chk[ed[i][0]][ed[i][1]]=1;
}
for(i=0;i<n;i++) dist[i]=INF;
dist[ed[0][0]]=0;
pq.push({0,ed[0][0]});
while(!pq.empty()) {
val=-pq.top().first;
cur=pq.top().second;
pq.pop();
if(cur==ed[1][0])break;
if(dist[cur]!=val) continue;
for(i=0;i<cg[cur].size();i++) {
tmp=cg[cur][i];
for(j=cur+tmp;j<n;j+=tmp) {
if(dist[j]>dist[cur]+(j-cur)/tmp) {
dist[j]=dist[cur]+(j-cur)/tmp;
pq.push({-dist[j],j});
}
if(tmp<=5000 && chk[j][tmp])break;
}
for(j=cur-tmp;j>=0;j-=tmp) {
if(dist[j]>dist[cur]+(cur-j)/tmp) {
dist[j]=dist[cur]+(cur-j)/tmp;
pq.push({-dist[j],j});
}
if(tmp<=5000 && chk[j][tmp])break;
}
}
}
printf("%d",dist[ed[1][0]]==INF?-1:dist[ed[1][0]]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
149760 KB |
Output is correct |
2 |
Correct |
0 ms |
149760 KB |
Output is correct |
3 |
Correct |
0 ms |
149760 KB |
Output is correct |
4 |
Correct |
0 ms |
149760 KB |
Output is correct |
5 |
Correct |
0 ms |
149760 KB |
Output is correct |
6 |
Correct |
0 ms |
149760 KB |
Output is correct |
7 |
Correct |
0 ms |
149760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
149760 KB |
Output is correct |
2 |
Correct |
0 ms |
149760 KB |
Output is correct |
3 |
Correct |
0 ms |
149760 KB |
Output is correct |
4 |
Correct |
0 ms |
149760 KB |
Output is correct |
5 |
Correct |
0 ms |
149760 KB |
Output is correct |
6 |
Correct |
0 ms |
149760 KB |
Output is correct |
7 |
Correct |
0 ms |
149760 KB |
Output is correct |
8 |
Correct |
0 ms |
149760 KB |
Output is correct |
9 |
Correct |
0 ms |
149760 KB |
Output is correct |
10 |
Correct |
0 ms |
149760 KB |
Output is correct |
11 |
Correct |
0 ms |
149760 KB |
Output is correct |
12 |
Correct |
0 ms |
149760 KB |
Output is correct |
13 |
Correct |
0 ms |
149760 KB |
Output is correct |
14 |
Correct |
0 ms |
149760 KB |
Output is correct |
15 |
Correct |
0 ms |
149760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
149760 KB |
Output is correct |
2 |
Correct |
0 ms |
149760 KB |
Output is correct |
3 |
Correct |
0 ms |
149760 KB |
Output is correct |
4 |
Correct |
0 ms |
149760 KB |
Output is correct |
5 |
Correct |
0 ms |
149760 KB |
Output is correct |
6 |
Correct |
0 ms |
149760 KB |
Output is correct |
7 |
Correct |
0 ms |
149760 KB |
Output is correct |
8 |
Correct |
0 ms |
149760 KB |
Output is correct |
9 |
Correct |
0 ms |
149760 KB |
Output is correct |
10 |
Correct |
0 ms |
149760 KB |
Output is correct |
11 |
Correct |
0 ms |
149760 KB |
Output is correct |
12 |
Correct |
0 ms |
149760 KB |
Output is correct |
13 |
Correct |
0 ms |
149760 KB |
Output is correct |
14 |
Correct |
2 ms |
149760 KB |
Output is correct |
15 |
Correct |
0 ms |
149760 KB |
Output is correct |
16 |
Correct |
0 ms |
149760 KB |
Output is correct |
17 |
Correct |
3 ms |
149760 KB |
Output is correct |
18 |
Correct |
0 ms |
149760 KB |
Output is correct |
19 |
Correct |
0 ms |
149760 KB |
Output is correct |
20 |
Correct |
0 ms |
149760 KB |
Output is correct |
21 |
Correct |
0 ms |
149760 KB |
Output is correct |
22 |
Correct |
0 ms |
149760 KB |
Output is correct |
23 |
Correct |
0 ms |
149760 KB |
Output is correct |
24 |
Correct |
0 ms |
149760 KB |
Output is correct |
25 |
Correct |
0 ms |
149760 KB |
Output is correct |
26 |
Correct |
3 ms |
149760 KB |
Output is correct |
27 |
Correct |
0 ms |
149760 KB |
Output is correct |
28 |
Correct |
0 ms |
149760 KB |
Output is correct |
29 |
Correct |
0 ms |
149896 KB |
Output is correct |
30 |
Correct |
0 ms |
149760 KB |
Output is correct |
31 |
Correct |
0 ms |
149896 KB |
Output is correct |
32 |
Correct |
0 ms |
149896 KB |
Output is correct |
33 |
Correct |
0 ms |
149904 KB |
Output is correct |
34 |
Correct |
2 ms |
149904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
149760 KB |
Output is correct |
2 |
Correct |
0 ms |
149760 KB |
Output is correct |
3 |
Correct |
0 ms |
149760 KB |
Output is correct |
4 |
Correct |
0 ms |
149760 KB |
Output is correct |
5 |
Correct |
0 ms |
149760 KB |
Output is correct |
6 |
Correct |
0 ms |
149760 KB |
Output is correct |
7 |
Correct |
0 ms |
149760 KB |
Output is correct |
8 |
Correct |
0 ms |
149760 KB |
Output is correct |
9 |
Correct |
0 ms |
149760 KB |
Output is correct |
10 |
Correct |
0 ms |
149760 KB |
Output is correct |
11 |
Correct |
1 ms |
149760 KB |
Output is correct |
12 |
Correct |
0 ms |
149760 KB |
Output is correct |
13 |
Correct |
1 ms |
149760 KB |
Output is correct |
14 |
Correct |
0 ms |
149760 KB |
Output is correct |
15 |
Correct |
0 ms |
149760 KB |
Output is correct |
16 |
Correct |
0 ms |
149760 KB |
Output is correct |
17 |
Correct |
2 ms |
149760 KB |
Output is correct |
18 |
Correct |
0 ms |
149760 KB |
Output is correct |
19 |
Correct |
0 ms |
149760 KB |
Output is correct |
20 |
Correct |
3 ms |
149760 KB |
Output is correct |
21 |
Correct |
0 ms |
149760 KB |
Output is correct |
22 |
Correct |
0 ms |
149760 KB |
Output is correct |
23 |
Correct |
0 ms |
149760 KB |
Output is correct |
24 |
Correct |
0 ms |
149760 KB |
Output is correct |
25 |
Correct |
0 ms |
149760 KB |
Output is correct |
26 |
Correct |
0 ms |
149760 KB |
Output is correct |
27 |
Correct |
2 ms |
149760 KB |
Output is correct |
28 |
Correct |
0 ms |
149760 KB |
Output is correct |
29 |
Correct |
0 ms |
149896 KB |
Output is correct |
30 |
Correct |
0 ms |
149760 KB |
Output is correct |
31 |
Correct |
2 ms |
149896 KB |
Output is correct |
32 |
Correct |
0 ms |
149896 KB |
Output is correct |
33 |
Correct |
0 ms |
149904 KB |
Output is correct |
34 |
Correct |
2 ms |
149904 KB |
Output is correct |
35 |
Correct |
13 ms |
150024 KB |
Output is correct |
36 |
Correct |
2 ms |
149760 KB |
Output is correct |
37 |
Correct |
3 ms |
150060 KB |
Output is correct |
38 |
Correct |
13 ms |
150024 KB |
Output is correct |
39 |
Correct |
10 ms |
150024 KB |
Output is correct |
40 |
Correct |
13 ms |
150024 KB |
Output is correct |
41 |
Correct |
12 ms |
150024 KB |
Output is correct |
42 |
Correct |
6 ms |
149760 KB |
Output is correct |
43 |
Correct |
3 ms |
149760 KB |
Output is correct |
44 |
Correct |
7 ms |
149760 KB |
Output is correct |
45 |
Correct |
11 ms |
150068 KB |
Output is correct |
46 |
Correct |
4 ms |
150064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
149760 KB |
Output is correct |
2 |
Correct |
0 ms |
149760 KB |
Output is correct |
3 |
Correct |
0 ms |
149760 KB |
Output is correct |
4 |
Correct |
0 ms |
149760 KB |
Output is correct |
5 |
Correct |
1 ms |
149760 KB |
Output is correct |
6 |
Correct |
0 ms |
149760 KB |
Output is correct |
7 |
Correct |
0 ms |
149760 KB |
Output is correct |
8 |
Correct |
0 ms |
149760 KB |
Output is correct |
9 |
Correct |
1 ms |
149760 KB |
Output is correct |
10 |
Correct |
1 ms |
149760 KB |
Output is correct |
11 |
Correct |
0 ms |
149760 KB |
Output is correct |
12 |
Correct |
0 ms |
149760 KB |
Output is correct |
13 |
Correct |
0 ms |
149760 KB |
Output is correct |
14 |
Correct |
2 ms |
149760 KB |
Output is correct |
15 |
Correct |
0 ms |
149760 KB |
Output is correct |
16 |
Correct |
0 ms |
149760 KB |
Output is correct |
17 |
Correct |
0 ms |
149760 KB |
Output is correct |
18 |
Correct |
0 ms |
149760 KB |
Output is correct |
19 |
Correct |
0 ms |
149760 KB |
Output is correct |
20 |
Correct |
3 ms |
149760 KB |
Output is correct |
21 |
Correct |
0 ms |
149760 KB |
Output is correct |
22 |
Correct |
0 ms |
149760 KB |
Output is correct |
23 |
Correct |
0 ms |
149760 KB |
Output is correct |
24 |
Correct |
0 ms |
149760 KB |
Output is correct |
25 |
Correct |
0 ms |
149760 KB |
Output is correct |
26 |
Correct |
3 ms |
149760 KB |
Output is correct |
27 |
Correct |
0 ms |
149760 KB |
Output is correct |
28 |
Correct |
3 ms |
149760 KB |
Output is correct |
29 |
Correct |
3 ms |
149896 KB |
Output is correct |
30 |
Correct |
0 ms |
149760 KB |
Output is correct |
31 |
Correct |
0 ms |
149896 KB |
Output is correct |
32 |
Correct |
0 ms |
149896 KB |
Output is correct |
33 |
Correct |
0 ms |
149904 KB |
Output is correct |
34 |
Correct |
2 ms |
149904 KB |
Output is correct |
35 |
Correct |
13 ms |
150024 KB |
Output is correct |
36 |
Correct |
0 ms |
149760 KB |
Output is correct |
37 |
Correct |
6 ms |
150060 KB |
Output is correct |
38 |
Correct |
14 ms |
150024 KB |
Output is correct |
39 |
Correct |
13 ms |
150024 KB |
Output is correct |
40 |
Correct |
0 ms |
150024 KB |
Output is correct |
41 |
Correct |
11 ms |
150024 KB |
Output is correct |
42 |
Correct |
6 ms |
149760 KB |
Output is correct |
43 |
Correct |
9 ms |
149760 KB |
Output is correct |
44 |
Correct |
9 ms |
149760 KB |
Output is correct |
45 |
Correct |
10 ms |
150068 KB |
Output is correct |
46 |
Correct |
12 ms |
150064 KB |
Output is correct |
47 |
Correct |
20 ms |
150492 KB |
Output is correct |
48 |
Correct |
8 ms |
150156 KB |
Output is correct |
49 |
Correct |
0 ms |
150156 KB |
Output is correct |
50 |
Correct |
0 ms |
150024 KB |
Output is correct |
51 |
Correct |
15 ms |
151124 KB |
Output is correct |
52 |
Correct |
11 ms |
151128 KB |
Output is correct |
53 |
Correct |
15 ms |
150548 KB |
Output is correct |
54 |
Correct |
0 ms |
149892 KB |
Output is correct |
55 |
Correct |
4 ms |
149892 KB |
Output is correct |
56 |
Correct |
0 ms |
150684 KB |
Output is correct |
57 |
Correct |
4 ms |
150220 KB |
Output is correct |
58 |
Correct |
8 ms |
149892 KB |
Output is correct |
59 |
Correct |
7 ms |
149892 KB |
Output is correct |
60 |
Correct |
5 ms |
149896 KB |
Output is correct |
61 |
Correct |
11 ms |
149916 KB |
Output is correct |
62 |
Correct |
15 ms |
150684 KB |
Output is correct |
63 |
Correct |
64 ms |
151540 KB |
Output is correct |
64 |
Correct |
61 ms |
151500 KB |
Output is correct |
65 |
Correct |
80 ms |
151512 KB |
Output is correct |
66 |
Correct |
104 ms |
153012 KB |
Output is correct |
67 |
Correct |
89 ms |
153016 KB |
Output is correct |