#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<ll,ll>
#define ff first
#define ss second
#define sz size()
const int N = 3e4 + 5;
using namespace std;
ll n, m, b[N], p[N];
vector <pii> v[N];
priority_queue <pii> pq;
vector <ll> dis(N,1e9);
map <pii,ll> mp;
void f(){
pq.push({0,b[1]});
dis[b[1]] = 0;
while(!pq.empty()){
int a = pq.top().ss;
pq.pop();
for(auto i: v[a]){
if(dis[a] + i.ss < dis[i.ff]){
dis[i.ff] = dis[a] + i.ss;
pq.push({-dis[i.ff],i.ff});
}
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
mp[{b[i],p[i]}]++;
}
for(int i = 1; i <= m; i++){
ll nd = b[i], cnt = 0;
while(1){
nd -= p[i];
if(nd < 0) break;
cnt++;
v[b[i]].pb({nd,cnt});
if(mp.find({nd,p[i]}) != mp.end()) break;
}
nd = b[i], cnt = 0;
while(1){
nd += p[i];
if(nd > n-1) break;
cnt++;
v[b[i]].pb({nd,cnt});
if(mp.find({nd,p[i]}) != mp.end()) break;
}
if(mp[{b[i],p[i]}] > 1) mp[{b[i],p[i]}]--;
}
f();
cout << (dis[b[2]] == 1e9 ? -1 : dis[b[2]]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1520 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
7 ms |
5844 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1412 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
5 ms |
6096 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1436 KB |
Output is correct |
17 |
Correct |
3 ms |
1936 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1420 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1464 KB |
Output is correct |
22 |
Correct |
2 ms |
1376 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1896 KB |
Output is correct |
25 |
Correct |
3 ms |
1636 KB |
Output is correct |
26 |
Correct |
80 ms |
67208 KB |
Output is correct |
27 |
Correct |
99 ms |
68596 KB |
Output is correct |
28 |
Correct |
4 ms |
1896 KB |
Output is correct |
29 |
Correct |
3 ms |
2920 KB |
Output is correct |
30 |
Correct |
2 ms |
1896 KB |
Output is correct |
31 |
Correct |
2 ms |
2580 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
6 ms |
4216 KB |
Output is correct |
34 |
Correct |
6 ms |
4576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1384 KB |
Output is correct |
2 |
Correct |
1 ms |
1384 KB |
Output is correct |
3 |
Correct |
1 ms |
1384 KB |
Output is correct |
4 |
Correct |
1 ms |
1384 KB |
Output is correct |
5 |
Correct |
1 ms |
1384 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
5 ms |
6100 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1628 KB |
Output is correct |
16 |
Correct |
2 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
1884 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
2 ms |
1412 KB |
Output is correct |
20 |
Correct |
2 ms |
1692 KB |
Output is correct |
21 |
Correct |
2 ms |
1432 KB |
Output is correct |
22 |
Correct |
2 ms |
1420 KB |
Output is correct |
23 |
Correct |
2 ms |
1624 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1624 KB |
Output is correct |
26 |
Correct |
74 ms |
67368 KB |
Output is correct |
27 |
Correct |
80 ms |
67244 KB |
Output is correct |
28 |
Correct |
3 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2904 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
2 ms |
2208 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
7 ms |
4188 KB |
Output is correct |
34 |
Correct |
6 ms |
4312 KB |
Output is correct |
35 |
Correct |
30 ms |
5744 KB |
Output is correct |
36 |
Correct |
4 ms |
2140 KB |
Output is correct |
37 |
Correct |
38 ms |
7528 KB |
Output is correct |
38 |
Correct |
42 ms |
7532 KB |
Output is correct |
39 |
Correct |
45 ms |
7684 KB |
Output is correct |
40 |
Correct |
44 ms |
7764 KB |
Output is correct |
41 |
Correct |
47 ms |
7520 KB |
Output is correct |
42 |
Runtime error |
248 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1420 KB |
Output is correct |
4 |
Correct |
1 ms |
1376 KB |
Output is correct |
5 |
Correct |
1 ms |
1376 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1376 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
5 ms |
5844 KB |
Output is correct |
13 |
Correct |
2 ms |
1376 KB |
Output is correct |
14 |
Correct |
2 ms |
1640 KB |
Output is correct |
15 |
Correct |
2 ms |
1632 KB |
Output is correct |
16 |
Correct |
1 ms |
1576 KB |
Output is correct |
17 |
Correct |
3 ms |
1884 KB |
Output is correct |
18 |
Correct |
2 ms |
1632 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1888 KB |
Output is correct |
25 |
Correct |
3 ms |
1628 KB |
Output is correct |
26 |
Correct |
76 ms |
67440 KB |
Output is correct |
27 |
Correct |
79 ms |
67244 KB |
Output is correct |
28 |
Correct |
3 ms |
1880 KB |
Output is correct |
29 |
Correct |
5 ms |
2904 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
3 ms |
2140 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
5 ms |
4188 KB |
Output is correct |
34 |
Correct |
6 ms |
4312 KB |
Output is correct |
35 |
Correct |
29 ms |
5792 KB |
Output is correct |
36 |
Correct |
5 ms |
2140 KB |
Output is correct |
37 |
Correct |
38 ms |
7312 KB |
Output is correct |
38 |
Correct |
46 ms |
7760 KB |
Output is correct |
39 |
Correct |
45 ms |
7672 KB |
Output is correct |
40 |
Correct |
42 ms |
7756 KB |
Output is correct |
41 |
Correct |
43 ms |
7504 KB |
Output is correct |
42 |
Runtime error |
230 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |