#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
int b[m],p[m];
vector<int>adj[n];
for(int i=0;i<m;i++){
cin>>b[i]>>p[i];
adj[b[i]].pb(p[i]);
}
set<pair<int,int>>s;
s.insert({0,b[0]});
vector<int>d(n,1e9);
d[b[0]]=0;
while(!s.empty()){
pair<int,int>q=*s.begin();
s.erase(s.begin());
int C=q.ff,p=q.ss;
for(int i:adj[p]){
int cur=p,cost=0;
while(cur>=0){
if(C+cost<d[cur]){
if(d[cur]<1e9) s.erase({d[cur],cur});
d[cur]=C+cost;
s.insert({d[cur],cur});
}
cur-=i;
cost++;
}
cur=p;
cost=0;
while(cur<n){
if(C+cost<d[cur]){
if(d[cur]<1e9) s.erase({d[cur],cur});
d[cur]=C+cost;
s.insert({d[cur],cur});
}
cur+=i;
cost++;
}
}
}
if(d[b[1]]==1e9){
cout<<"-1";
}
else{
cout<<d[b[1]];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
788 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
896 KB |
Output is correct |
38 |
Correct |
6 ms |
1116 KB |
Output is correct |
39 |
Correct |
6 ms |
1116 KB |
Output is correct |
40 |
Correct |
7 ms |
1116 KB |
Output is correct |
41 |
Correct |
7 ms |
1116 KB |
Output is correct |
42 |
Correct |
58 ms |
1116 KB |
Output is correct |
43 |
Correct |
55 ms |
1116 KB |
Output is correct |
44 |
Correct |
59 ms |
1116 KB |
Output is correct |
45 |
Correct |
6 ms |
1116 KB |
Output is correct |
46 |
Correct |
6 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
452 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
644 KB |
Output is correct |
34 |
Correct |
3 ms |
600 KB |
Output is correct |
35 |
Correct |
5 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
600 KB |
Output is correct |
37 |
Correct |
5 ms |
860 KB |
Output is correct |
38 |
Correct |
7 ms |
1116 KB |
Output is correct |
39 |
Correct |
6 ms |
1112 KB |
Output is correct |
40 |
Correct |
7 ms |
1112 KB |
Output is correct |
41 |
Correct |
6 ms |
1112 KB |
Output is correct |
42 |
Correct |
58 ms |
1112 KB |
Output is correct |
43 |
Correct |
56 ms |
1164 KB |
Output is correct |
44 |
Correct |
58 ms |
1116 KB |
Output is correct |
45 |
Correct |
7 ms |
1116 KB |
Output is correct |
46 |
Correct |
6 ms |
1116 KB |
Output is correct |
47 |
Correct |
20 ms |
1880 KB |
Output is correct |
48 |
Correct |
5 ms |
2136 KB |
Output is correct |
49 |
Correct |
4 ms |
1788 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
34 ms |
3676 KB |
Output is correct |
52 |
Correct |
32 ms |
3420 KB |
Output is correct |
53 |
Correct |
15 ms |
3672 KB |
Output is correct |
54 |
Correct |
3 ms |
2136 KB |
Output is correct |
55 |
Correct |
3 ms |
1884 KB |
Output is correct |
56 |
Correct |
712 ms |
4068 KB |
Output is correct |
57 |
Correct |
69 ms |
2652 KB |
Output is correct |
58 |
Correct |
565 ms |
2432 KB |
Output is correct |
59 |
Correct |
838 ms |
2644 KB |
Output is correct |
60 |
Correct |
618 ms |
2396 KB |
Output is correct |
61 |
Correct |
715 ms |
2568 KB |
Output is correct |
62 |
Correct |
8 ms |
2652 KB |
Output is correct |
63 |
Correct |
45 ms |
3164 KB |
Output is correct |
64 |
Correct |
48 ms |
3272 KB |
Output is correct |
65 |
Correct |
58 ms |
3308 KB |
Output is correct |
66 |
Correct |
83 ms |
3280 KB |
Output is correct |
67 |
Correct |
80 ms |
3160 KB |
Output is correct |