#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>>g[1500005];
int n,m;
int s,t;
long long d[1500005];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int b,p;cin>>b>>p;
b++;
if(i==1)s=b;
if(i==2)t=b;
if(p>30){for(int j=b-p;j>=1;j-=p){g[b].push_back({j,(b-j)/p});}
for(int j=b+p;j<=n;j+=p){g[b].push_back({j,(j-b)/p});}
}
else
{
g[b].push_back({n+(b-1)*30+p,0});
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=30;j++)
{
g[n+(i-1)*30+j].push_back({i,0});
}
for(int i=1;i<=n;i++)
for(int j=1;j<=30;j++)
{
if(i+j<=n){
g[n+(i-1)*30+j].push_back({n+(i+j-1)*30+j,1});
g[n+(i+j-1)*30+j].push_back({n+(i-1)*30+j,1});
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
for(int i=1;i<=n*40;i++)d[i]=1e18;
d[s]=0;
pq.push({0,s});
//cout<<s<<endl;
//for(auto v:g[1])cout<<v.first<<endl;
while(pq.size())
{
int u=pq.top().second;
pq.pop();
for(auto v:g[u])
{
if(d[v.first]>d[u]+v.second){d[v.first]=d[u]+v.second;pq.push({d[v.first],v.first});}
}
}
if(d[t]<1e18)
cout<<d[t];
else cout<<-1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35524 KB |
Output is correct |
3 |
Correct |
19 ms |
35464 KB |
Output is correct |
4 |
Correct |
20 ms |
35436 KB |
Output is correct |
5 |
Correct |
19 ms |
35548 KB |
Output is correct |
6 |
Correct |
20 ms |
35540 KB |
Output is correct |
7 |
Correct |
19 ms |
35540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35552 KB |
Output is correct |
3 |
Correct |
19 ms |
35552 KB |
Output is correct |
4 |
Correct |
23 ms |
35552 KB |
Output is correct |
5 |
Correct |
19 ms |
35512 KB |
Output is correct |
6 |
Correct |
20 ms |
35512 KB |
Output is correct |
7 |
Correct |
19 ms |
35516 KB |
Output is correct |
8 |
Correct |
20 ms |
35540 KB |
Output is correct |
9 |
Correct |
20 ms |
35548 KB |
Output is correct |
10 |
Correct |
20 ms |
35696 KB |
Output is correct |
11 |
Correct |
21 ms |
35720 KB |
Output is correct |
12 |
Correct |
20 ms |
35796 KB |
Output is correct |
13 |
Correct |
20 ms |
35788 KB |
Output is correct |
14 |
Correct |
21 ms |
35796 KB |
Output is correct |
15 |
Correct |
23 ms |
35796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35472 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
18 ms |
35524 KB |
Output is correct |
6 |
Correct |
19 ms |
35540 KB |
Output is correct |
7 |
Correct |
20 ms |
35548 KB |
Output is correct |
8 |
Correct |
19 ms |
35532 KB |
Output is correct |
9 |
Correct |
20 ms |
35604 KB |
Output is correct |
10 |
Correct |
18 ms |
35672 KB |
Output is correct |
11 |
Correct |
20 ms |
35788 KB |
Output is correct |
12 |
Correct |
19 ms |
35692 KB |
Output is correct |
13 |
Correct |
20 ms |
35812 KB |
Output is correct |
14 |
Correct |
19 ms |
35816 KB |
Output is correct |
15 |
Correct |
20 ms |
35716 KB |
Output is correct |
16 |
Correct |
23 ms |
35916 KB |
Output is correct |
17 |
Correct |
25 ms |
36908 KB |
Output is correct |
18 |
Correct |
28 ms |
38540 KB |
Output is correct |
19 |
Correct |
27 ms |
38952 KB |
Output is correct |
20 |
Correct |
27 ms |
38988 KB |
Output is correct |
21 |
Correct |
21 ms |
36308 KB |
Output is correct |
22 |
Correct |
27 ms |
38740 KB |
Output is correct |
23 |
Correct |
28 ms |
38356 KB |
Output is correct |
24 |
Correct |
30 ms |
38860 KB |
Output is correct |
25 |
Correct |
30 ms |
39000 KB |
Output is correct |
26 |
Correct |
35 ms |
39004 KB |
Output is correct |
27 |
Correct |
30 ms |
39012 KB |
Output is correct |
28 |
Correct |
30 ms |
39124 KB |
Output is correct |
29 |
Correct |
37 ms |
39372 KB |
Output is correct |
30 |
Correct |
29 ms |
39004 KB |
Output is correct |
31 |
Correct |
28 ms |
39260 KB |
Output is correct |
32 |
Correct |
32 ms |
39144 KB |
Output is correct |
33 |
Correct |
34 ms |
39736 KB |
Output is correct |
34 |
Correct |
38 ms |
39848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35452 KB |
Output is correct |
3 |
Correct |
24 ms |
35524 KB |
Output is correct |
4 |
Correct |
22 ms |
35468 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35544 KB |
Output is correct |
7 |
Correct |
20 ms |
35548 KB |
Output is correct |
8 |
Correct |
19 ms |
35544 KB |
Output is correct |
9 |
Correct |
19 ms |
35604 KB |
Output is correct |
10 |
Correct |
18 ms |
35668 KB |
Output is correct |
11 |
Correct |
20 ms |
35724 KB |
Output is correct |
12 |
Correct |
18 ms |
35784 KB |
Output is correct |
13 |
Correct |
19 ms |
35736 KB |
Output is correct |
14 |
Correct |
19 ms |
35824 KB |
Output is correct |
15 |
Correct |
19 ms |
35796 KB |
Output is correct |
16 |
Correct |
19 ms |
35864 KB |
Output is correct |
17 |
Correct |
25 ms |
36876 KB |
Output is correct |
18 |
Correct |
30 ms |
38604 KB |
Output is correct |
19 |
Correct |
31 ms |
39036 KB |
Output is correct |
20 |
Correct |
33 ms |
39052 KB |
Output is correct |
21 |
Correct |
21 ms |
36308 KB |
Output is correct |
22 |
Correct |
32 ms |
38628 KB |
Output is correct |
23 |
Correct |
32 ms |
38396 KB |
Output is correct |
24 |
Correct |
32 ms |
38932 KB |
Output is correct |
25 |
Correct |
32 ms |
39056 KB |
Output is correct |
26 |
Correct |
33 ms |
39048 KB |
Output is correct |
27 |
Correct |
33 ms |
39056 KB |
Output is correct |
28 |
Correct |
34 ms |
39108 KB |
Output is correct |
29 |
Correct |
48 ms |
39384 KB |
Output is correct |
30 |
Correct |
39 ms |
38948 KB |
Output is correct |
31 |
Correct |
39 ms |
39256 KB |
Output is correct |
32 |
Correct |
33 ms |
39180 KB |
Output is correct |
33 |
Correct |
41 ms |
39816 KB |
Output is correct |
34 |
Correct |
38 ms |
39748 KB |
Output is correct |
35 |
Correct |
42 ms |
38860 KB |
Output is correct |
36 |
Correct |
29 ms |
37588 KB |
Output is correct |
37 |
Correct |
48 ms |
40536 KB |
Output is correct |
38 |
Correct |
56 ms |
40356 KB |
Output is correct |
39 |
Correct |
57 ms |
40404 KB |
Output is correct |
40 |
Correct |
49 ms |
40456 KB |
Output is correct |
41 |
Correct |
46 ms |
40404 KB |
Output is correct |
42 |
Correct |
33 ms |
39504 KB |
Output is correct |
43 |
Correct |
35 ms |
39392 KB |
Output is correct |
44 |
Correct |
42 ms |
39512 KB |
Output is correct |
45 |
Correct |
50 ms |
43020 KB |
Output is correct |
46 |
Correct |
54 ms |
43096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35528 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35524 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
18 ms |
35480 KB |
Output is correct |
7 |
Correct |
19 ms |
35516 KB |
Output is correct |
8 |
Correct |
24 ms |
35552 KB |
Output is correct |
9 |
Correct |
19 ms |
35540 KB |
Output is correct |
10 |
Correct |
19 ms |
35668 KB |
Output is correct |
11 |
Correct |
19 ms |
35924 KB |
Output is correct |
12 |
Correct |
24 ms |
35752 KB |
Output is correct |
13 |
Correct |
21 ms |
35696 KB |
Output is correct |
14 |
Correct |
22 ms |
35812 KB |
Output is correct |
15 |
Correct |
21 ms |
35812 KB |
Output is correct |
16 |
Correct |
21 ms |
35864 KB |
Output is correct |
17 |
Correct |
25 ms |
36948 KB |
Output is correct |
18 |
Correct |
27 ms |
38612 KB |
Output is correct |
19 |
Correct |
28 ms |
38944 KB |
Output is correct |
20 |
Correct |
29 ms |
38996 KB |
Output is correct |
21 |
Correct |
20 ms |
36260 KB |
Output is correct |
22 |
Correct |
30 ms |
38612 KB |
Output is correct |
23 |
Correct |
29 ms |
38376 KB |
Output is correct |
24 |
Correct |
38 ms |
38920 KB |
Output is correct |
25 |
Correct |
29 ms |
38988 KB |
Output is correct |
26 |
Correct |
31 ms |
39072 KB |
Output is correct |
27 |
Correct |
27 ms |
38988 KB |
Output is correct |
28 |
Correct |
29 ms |
39140 KB |
Output is correct |
29 |
Correct |
39 ms |
39372 KB |
Output is correct |
30 |
Correct |
37 ms |
39112 KB |
Output is correct |
31 |
Correct |
32 ms |
39276 KB |
Output is correct |
32 |
Correct |
32 ms |
39104 KB |
Output is correct |
33 |
Correct |
35 ms |
39812 KB |
Output is correct |
34 |
Correct |
35 ms |
39804 KB |
Output is correct |
35 |
Correct |
35 ms |
38876 KB |
Output is correct |
36 |
Correct |
25 ms |
37584 KB |
Output is correct |
37 |
Correct |
46 ms |
40488 KB |
Output is correct |
38 |
Correct |
45 ms |
40432 KB |
Output is correct |
39 |
Correct |
44 ms |
40396 KB |
Output is correct |
40 |
Correct |
43 ms |
40504 KB |
Output is correct |
41 |
Correct |
43 ms |
40444 KB |
Output is correct |
42 |
Correct |
33 ms |
39524 KB |
Output is correct |
43 |
Correct |
33 ms |
39400 KB |
Output is correct |
44 |
Correct |
42 ms |
39540 KB |
Output is correct |
45 |
Correct |
46 ms |
43044 KB |
Output is correct |
46 |
Correct |
45 ms |
43104 KB |
Output is correct |
47 |
Correct |
166 ms |
63428 KB |
Output is correct |
48 |
Correct |
136 ms |
77276 KB |
Output is correct |
49 |
Correct |
149 ms |
80636 KB |
Output is correct |
50 |
Correct |
183 ms |
84844 KB |
Output is correct |
51 |
Correct |
227 ms |
90092 KB |
Output is correct |
52 |
Correct |
218 ms |
90244 KB |
Output is correct |
53 |
Correct |
197 ms |
87660 KB |
Output is correct |
54 |
Correct |
179 ms |
87120 KB |
Output is correct |
55 |
Correct |
166 ms |
87116 KB |
Output is correct |
56 |
Correct |
167 ms |
88268 KB |
Output is correct |
57 |
Correct |
314 ms |
94712 KB |
Output is correct |
58 |
Correct |
192 ms |
87596 KB |
Output is correct |
59 |
Correct |
192 ms |
87900 KB |
Output is correct |
60 |
Correct |
218 ms |
88460 KB |
Output is correct |
61 |
Correct |
225 ms |
88012 KB |
Output is correct |
62 |
Correct |
240 ms |
90952 KB |
Output is correct |
63 |
Correct |
240 ms |
110028 KB |
Output is correct |
64 |
Correct |
249 ms |
115432 KB |
Output is correct |
65 |
Correct |
273 ms |
123428 KB |
Output is correct |
66 |
Correct |
453 ms |
143956 KB |
Output is correct |
67 |
Correct |
453 ms |
143708 KB |
Output is correct |