#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 1e6 + 7;
const int mod = 1e9 + 7;
int n,m;
int a[N];
int b[N];
int d[N];
int h[N];
int t[N];
set < int > s[N];
vector < pair < int , int > > v[N];
void solve()
{
cin >> n >> m;
for( int i = 0; i < m; i++ ){
cin >> a[i] >> b[i];
s[a[i]].insert(b[i]);
}
for( int i = 0; i <= n; i++ )d[i] = 1e9;
for( int i = 0; i < n; i++ ){
auto it = s[i].begin();
while( it != s[i].end() ){
int z = i , cnt = 0;
while( z + *it < n ){
z += *it , cnt++;
v[i].push_back({z , cnt});
if( s[z].find(*it) != s[z].end() )break;
}
z = i , cnt = 0;
while( z - *it >= 0 ){
z -= *it , cnt++;
v[i].push_back({z , cnt});
if( s[z].find(*it) != s[z].end() )break;
}
it++;
}
}
d[a[0]] = 0;
set < pair < int , int > > q;
q.insert({0 , a[0]});
while( !q.empty() ){
int x = q.begin()->se;
q.erase(q.begin());
for( auto y : v[x] ){
if( d[x] + y.se < d[y.fi] ){
q.erase({d[y.fi] , y.fi});
d[y.fi] = d[x] + y.se;
q.insert({ d[y.fi] , y.fi });
}
}
}
if( d[a[1]] == 1e9 )d[a[1]] = -1;
cout << d[a[1]];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int t = 1;//cin >> t;
while( t-- ){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70776 KB |
Output is correct |
2 |
Correct |
73 ms |
70776 KB |
Output is correct |
3 |
Correct |
74 ms |
70800 KB |
Output is correct |
4 |
Correct |
73 ms |
70776 KB |
Output is correct |
5 |
Correct |
73 ms |
70868 KB |
Output is correct |
6 |
Correct |
73 ms |
70820 KB |
Output is correct |
7 |
Correct |
72 ms |
70904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70776 KB |
Output is correct |
2 |
Correct |
73 ms |
70776 KB |
Output is correct |
3 |
Correct |
71 ms |
70820 KB |
Output is correct |
4 |
Correct |
74 ms |
70780 KB |
Output is correct |
5 |
Correct |
72 ms |
70752 KB |
Output is correct |
6 |
Correct |
72 ms |
70776 KB |
Output is correct |
7 |
Correct |
73 ms |
70876 KB |
Output is correct |
8 |
Correct |
73 ms |
70876 KB |
Output is correct |
9 |
Correct |
74 ms |
70876 KB |
Output is correct |
10 |
Correct |
74 ms |
70908 KB |
Output is correct |
11 |
Correct |
73 ms |
71032 KB |
Output is correct |
12 |
Correct |
74 ms |
70780 KB |
Output is correct |
13 |
Correct |
74 ms |
70884 KB |
Output is correct |
14 |
Correct |
75 ms |
71032 KB |
Output is correct |
15 |
Correct |
74 ms |
70904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
70776 KB |
Output is correct |
2 |
Correct |
71 ms |
70868 KB |
Output is correct |
3 |
Correct |
70 ms |
70776 KB |
Output is correct |
4 |
Correct |
71 ms |
70776 KB |
Output is correct |
5 |
Correct |
71 ms |
70816 KB |
Output is correct |
6 |
Correct |
70 ms |
70780 KB |
Output is correct |
7 |
Correct |
70 ms |
70776 KB |
Output is correct |
8 |
Correct |
78 ms |
70904 KB |
Output is correct |
9 |
Correct |
71 ms |
70864 KB |
Output is correct |
10 |
Correct |
74 ms |
70904 KB |
Output is correct |
11 |
Correct |
72 ms |
70988 KB |
Output is correct |
12 |
Correct |
71 ms |
70904 KB |
Output is correct |
13 |
Correct |
72 ms |
70776 KB |
Output is correct |
14 |
Correct |
74 ms |
71132 KB |
Output is correct |
15 |
Correct |
74 ms |
71032 KB |
Output is correct |
16 |
Correct |
73 ms |
70904 KB |
Output is correct |
17 |
Correct |
72 ms |
71160 KB |
Output is correct |
18 |
Correct |
74 ms |
71020 KB |
Output is correct |
19 |
Correct |
73 ms |
70904 KB |
Output is correct |
20 |
Correct |
72 ms |
71060 KB |
Output is correct |
21 |
Correct |
75 ms |
70944 KB |
Output is correct |
22 |
Correct |
74 ms |
70848 KB |
Output is correct |
23 |
Correct |
74 ms |
71032 KB |
Output is correct |
24 |
Correct |
75 ms |
71164 KB |
Output is correct |
25 |
Correct |
76 ms |
71172 KB |
Output is correct |
26 |
Correct |
74 ms |
71032 KB |
Output is correct |
27 |
Correct |
82 ms |
71016 KB |
Output is correct |
28 |
Correct |
75 ms |
71160 KB |
Output is correct |
29 |
Correct |
79 ms |
71672 KB |
Output is correct |
30 |
Correct |
73 ms |
71164 KB |
Output is correct |
31 |
Correct |
77 ms |
71416 KB |
Output is correct |
32 |
Correct |
75 ms |
71164 KB |
Output is correct |
33 |
Correct |
82 ms |
72052 KB |
Output is correct |
34 |
Correct |
83 ms |
72316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70776 KB |
Output is correct |
2 |
Correct |
72 ms |
70776 KB |
Output is correct |
3 |
Correct |
74 ms |
70868 KB |
Output is correct |
4 |
Correct |
73 ms |
70904 KB |
Output is correct |
5 |
Correct |
74 ms |
70904 KB |
Output is correct |
6 |
Correct |
73 ms |
70780 KB |
Output is correct |
7 |
Correct |
73 ms |
70776 KB |
Output is correct |
8 |
Correct |
73 ms |
70880 KB |
Output is correct |
9 |
Correct |
73 ms |
70776 KB |
Output is correct |
10 |
Correct |
73 ms |
70904 KB |
Output is correct |
11 |
Correct |
74 ms |
71032 KB |
Output is correct |
12 |
Correct |
74 ms |
70904 KB |
Output is correct |
13 |
Correct |
72 ms |
70864 KB |
Output is correct |
14 |
Correct |
72 ms |
70904 KB |
Output is correct |
15 |
Correct |
72 ms |
71012 KB |
Output is correct |
16 |
Correct |
74 ms |
70908 KB |
Output is correct |
17 |
Correct |
75 ms |
71160 KB |
Output is correct |
18 |
Correct |
73 ms |
71032 KB |
Output is correct |
19 |
Correct |
72 ms |
70876 KB |
Output is correct |
20 |
Correct |
73 ms |
71032 KB |
Output is correct |
21 |
Correct |
74 ms |
70904 KB |
Output is correct |
22 |
Correct |
74 ms |
70904 KB |
Output is correct |
23 |
Correct |
73 ms |
71032 KB |
Output is correct |
24 |
Correct |
74 ms |
71088 KB |
Output is correct |
25 |
Correct |
75 ms |
71032 KB |
Output is correct |
26 |
Correct |
74 ms |
71032 KB |
Output is correct |
27 |
Correct |
74 ms |
71032 KB |
Output is correct |
28 |
Correct |
76 ms |
71160 KB |
Output is correct |
29 |
Correct |
77 ms |
71544 KB |
Output is correct |
30 |
Correct |
81 ms |
71108 KB |
Output is correct |
31 |
Correct |
76 ms |
71292 KB |
Output is correct |
32 |
Correct |
74 ms |
71160 KB |
Output is correct |
33 |
Correct |
82 ms |
72180 KB |
Output is correct |
34 |
Correct |
81 ms |
72180 KB |
Output is correct |
35 |
Correct |
94 ms |
73464 KB |
Output is correct |
36 |
Correct |
77 ms |
71412 KB |
Output is correct |
37 |
Correct |
98 ms |
74104 KB |
Output is correct |
38 |
Correct |
104 ms |
74232 KB |
Output is correct |
39 |
Correct |
105 ms |
74488 KB |
Output is correct |
40 |
Correct |
104 ms |
74360 KB |
Output is correct |
41 |
Correct |
103 ms |
74232 KB |
Output is correct |
42 |
Correct |
78 ms |
71416 KB |
Output is correct |
43 |
Correct |
78 ms |
71160 KB |
Output is correct |
44 |
Correct |
78 ms |
71160 KB |
Output is correct |
45 |
Correct |
109 ms |
76920 KB |
Output is correct |
46 |
Correct |
108 ms |
76920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70840 KB |
Output is correct |
2 |
Correct |
72 ms |
70872 KB |
Output is correct |
3 |
Correct |
73 ms |
70864 KB |
Output is correct |
4 |
Correct |
72 ms |
70904 KB |
Output is correct |
5 |
Correct |
74 ms |
70776 KB |
Output is correct |
6 |
Correct |
72 ms |
70776 KB |
Output is correct |
7 |
Correct |
72 ms |
70776 KB |
Output is correct |
8 |
Correct |
71 ms |
70776 KB |
Output is correct |
9 |
Correct |
73 ms |
70904 KB |
Output is correct |
10 |
Correct |
74 ms |
70840 KB |
Output is correct |
11 |
Correct |
74 ms |
70904 KB |
Output is correct |
12 |
Correct |
73 ms |
70904 KB |
Output is correct |
13 |
Correct |
74 ms |
70780 KB |
Output is correct |
14 |
Correct |
74 ms |
71032 KB |
Output is correct |
15 |
Correct |
75 ms |
71032 KB |
Output is correct |
16 |
Correct |
74 ms |
70904 KB |
Output is correct |
17 |
Correct |
76 ms |
71188 KB |
Output is correct |
18 |
Correct |
74 ms |
71112 KB |
Output is correct |
19 |
Correct |
74 ms |
70904 KB |
Output is correct |
20 |
Correct |
75 ms |
71112 KB |
Output is correct |
21 |
Correct |
73 ms |
70904 KB |
Output is correct |
22 |
Correct |
74 ms |
71032 KB |
Output is correct |
23 |
Correct |
75 ms |
71032 KB |
Output is correct |
24 |
Correct |
75 ms |
71080 KB |
Output is correct |
25 |
Correct |
76 ms |
71216 KB |
Output is correct |
26 |
Correct |
73 ms |
71028 KB |
Output is correct |
27 |
Correct |
74 ms |
71032 KB |
Output is correct |
28 |
Correct |
75 ms |
71160 KB |
Output is correct |
29 |
Correct |
79 ms |
71544 KB |
Output is correct |
30 |
Correct |
74 ms |
71160 KB |
Output is correct |
31 |
Correct |
76 ms |
71288 KB |
Output is correct |
32 |
Correct |
79 ms |
71160 KB |
Output is correct |
33 |
Correct |
80 ms |
72180 KB |
Output is correct |
34 |
Correct |
82 ms |
72180 KB |
Output is correct |
35 |
Correct |
93 ms |
73464 KB |
Output is correct |
36 |
Correct |
77 ms |
71288 KB |
Output is correct |
37 |
Correct |
98 ms |
73976 KB |
Output is correct |
38 |
Correct |
104 ms |
74476 KB |
Output is correct |
39 |
Correct |
103 ms |
74488 KB |
Output is correct |
40 |
Correct |
106 ms |
74232 KB |
Output is correct |
41 |
Correct |
104 ms |
74464 KB |
Output is correct |
42 |
Correct |
79 ms |
71396 KB |
Output is correct |
43 |
Correct |
79 ms |
71160 KB |
Output is correct |
44 |
Correct |
80 ms |
71308 KB |
Output is correct |
45 |
Correct |
108 ms |
76920 KB |
Output is correct |
46 |
Correct |
107 ms |
76956 KB |
Output is correct |
47 |
Correct |
150 ms |
81528 KB |
Output is correct |
48 |
Correct |
96 ms |
74740 KB |
Output is correct |
49 |
Correct |
94 ms |
73848 KB |
Output is correct |
50 |
Correct |
88 ms |
73464 KB |
Output is correct |
51 |
Correct |
157 ms |
77468 KB |
Output is correct |
52 |
Correct |
156 ms |
77696 KB |
Output is correct |
53 |
Correct |
123 ms |
75004 KB |
Output is correct |
54 |
Correct |
89 ms |
71848 KB |
Output is correct |
55 |
Correct |
80 ms |
72384 KB |
Output is correct |
56 |
Correct |
93 ms |
73720 KB |
Output is correct |
57 |
Correct |
156 ms |
80120 KB |
Output is correct |
58 |
Correct |
87 ms |
72816 KB |
Output is correct |
59 |
Correct |
88 ms |
73128 KB |
Output is correct |
60 |
Correct |
90 ms |
73844 KB |
Output is correct |
61 |
Correct |
90 ms |
73336 KB |
Output is correct |
62 |
Correct |
109 ms |
78068 KB |
Output is correct |
63 |
Correct |
222 ms |
94072 KB |
Output is correct |
64 |
Correct |
230 ms |
97648 KB |
Output is correct |
65 |
Correct |
283 ms |
107040 KB |
Output is correct |
66 |
Correct |
461 ms |
131756 KB |
Output is correct |
67 |
Correct |
456 ms |
131680 KB |
Output is correct |