#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define inf 1e9
int n,m;
int st,en;
int d[30010];
vector<int> v[30010];
void go()
{
fill(d,d+n+1,inf);
d[st]=0;
priority_queue<pair<int,int>> pq;
pq.push({0,st});
while(!pq.empty())
{
pair<int,int> a=pq.top();
int current=a.second;
int distance=-a.first;
//cout<<"| "<<current<<" "<<distance<<'\n';
pq.pop();
if(d[current]<distance) continue;
for(auto j:v[current])
{
int num=1;
while(1)
{
int temp=current+j*num;
//cout<<d[current];
if(temp>=n) break;
if(distance+num<d[temp])
{
d[temp]=distance+num;
pq.push({-d[temp],temp});
if(v[temp].size())
{
int idx=upper_bound(v[temp].begin(),v[temp].end(),j)-v[temp].begin();
if(v[temp][idx-1]==j) break;
}
}
num++;
}
num=1;
while(1)
{
int temp=current-j*num;
if(temp<0) break;
if(distance+num<d[temp])
{
d[temp]=distance+num;
pq.push({-d[temp],temp});
if(v[temp].size())
{
int idx=upper_bound(v[temp].begin(),v[temp].end(),j)-v[temp].begin();
if(v[temp][idx-1]==j) break;
}
}
num++;
}
}
}
}
int main()
{
fastio;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
if(i==0) st=a;
else if(i==1) en=a;
v[a].push_back(b);
}
for(int i=0;i<n;i++)
sort(v[i].begin(),v[i].end());
go();
if(d[en]==inf) cout<<-1;
else cout<<d[en];
}
# |
Verdict |
Execution time |
Memory |
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 |
0 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 |
0 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
0 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 |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 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 |
0 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 |
1116 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 |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 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 |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
5 ms |
1112 KB |
Output is correct |
28 |
Correct |
2 ms |
1120 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 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 |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1156 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 |
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 |
2 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
2 ms |
1120 KB |
Output is correct |
21 |
Correct |
1 ms |
1076 KB |
Output is correct |
22 |
Correct |
1 ms |
1124 KB |
Output is correct |
23 |
Correct |
2 ms |
1120 KB |
Output is correct |
24 |
Correct |
2 ms |
1128 KB |
Output is correct |
25 |
Correct |
1 ms |
1128 KB |
Output is correct |
26 |
Correct |
4 ms |
1216 KB |
Output is correct |
27 |
Correct |
4 ms |
1128 KB |
Output is correct |
28 |
Correct |
2 ms |
1128 KB |
Output is correct |
29 |
Correct |
1 ms |
1128 KB |
Output is correct |
30 |
Correct |
1 ms |
1284 KB |
Output is correct |
31 |
Correct |
1 ms |
1124 KB |
Output is correct |
32 |
Correct |
1 ms |
1128 KB |
Output is correct |
33 |
Correct |
2 ms |
1384 KB |
Output is correct |
34 |
Correct |
2 ms |
1380 KB |
Output is correct |
35 |
Correct |
4 ms |
1384 KB |
Output is correct |
36 |
Correct |
1 ms |
1128 KB |
Output is correct |
37 |
Correct |
4 ms |
1384 KB |
Output is correct |
38 |
Correct |
7 ms |
1384 KB |
Output is correct |
39 |
Correct |
6 ms |
1384 KB |
Output is correct |
40 |
Correct |
9 ms |
1396 KB |
Output is correct |
41 |
Correct |
6 ms |
1384 KB |
Output is correct |
42 |
Correct |
41 ms |
1380 KB |
Output is correct |
43 |
Correct |
35 ms |
1384 KB |
Output is correct |
44 |
Correct |
34 ms |
1384 KB |
Output is correct |
45 |
Correct |
6 ms |
1536 KB |
Output is correct |
46 |
Correct |
6 ms |
1624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1128 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 |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
0 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 |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1128 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 |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 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 |
1112 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
1112 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 |
1112 KB |
Output is correct |
33 |
Correct |
3 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
4 ms |
1400 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
6 ms |
1372 KB |
Output is correct |
38 |
Correct |
6 ms |
1372 KB |
Output is correct |
39 |
Correct |
6 ms |
1456 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
5 ms |
1464 KB |
Output is correct |
42 |
Correct |
33 ms |
1372 KB |
Output is correct |
43 |
Correct |
44 ms |
1372 KB |
Output is correct |
44 |
Correct |
30 ms |
1368 KB |
Output is correct |
45 |
Correct |
6 ms |
1524 KB |
Output is correct |
46 |
Correct |
5 ms |
1628 KB |
Output is correct |
47 |
Correct |
11 ms |
2200 KB |
Output is correct |
48 |
Correct |
4 ms |
1628 KB |
Output is correct |
49 |
Correct |
4 ms |
1624 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
18 ms |
2520 KB |
Output is correct |
52 |
Correct |
18 ms |
2520 KB |
Output is correct |
53 |
Correct |
10 ms |
2520 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
178 ms |
2140 KB |
Output is correct |
57 |
Correct |
27 ms |
2516 KB |
Output is correct |
58 |
Correct |
519 ms |
1624 KB |
Output is correct |
59 |
Correct |
418 ms |
1628 KB |
Output is correct |
60 |
Correct |
499 ms |
1628 KB |
Output is correct |
61 |
Correct |
398 ms |
1628 KB |
Output is correct |
62 |
Correct |
6 ms |
2140 KB |
Output is correct |
63 |
Correct |
17 ms |
2688 KB |
Output is correct |
64 |
Correct |
16 ms |
2516 KB |
Output is correct |
65 |
Correct |
19 ms |
2524 KB |
Output is correct |
66 |
Correct |
36 ms |
3532 KB |
Output is correct |
67 |
Correct |
37 ms |
3532 KB |
Output is correct |