#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
int n, m, s[30030], p[30030];
ll d[30030];
vector<array<int, 2>> v[30030];
set<int> st[30030];
priority_queue<array<ll, 2>> pq;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=1;i<=m;i++) {
cin >> s[i] >> p[i];
st[s[i]].insert(p[i]);
}
for(int i=1;i<=m;i++) {
for(int j=1;;j++) {
int x = s[i]-p[i]*j;
if(x < 0) break;
v[s[i]].push_back({x, j});
if(st[x].count(p[i])) break;
}
for(int j=1;;j++) {
int x = s[i]+p[i]*j;
if(x >= n) break;
v[s[i]].push_back({x, j});
if(st[x].count(p[i])) break;
}
}
fill(d, d+n, INF);
d[s[1]] = 0, pq.push({0, s[1]});
while(!pq.empty()) {
auto [c, u] = pq.top(); pq.pop();
if(-c > d[u]) continue;
for(auto [x, y] : v[u]) if(y-c < d[x]) d[x] = y-c, pq.push({c-y, x});
}
cout << (d[s[2]] == INF ? -1 : d[s[2]]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2660 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
4 ms |
4820 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2680 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2656 KB |
Output is correct |
7 |
Correct |
1 ms |
2656 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2656 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
4868 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2860 KB |
Output is correct |
19 |
Correct |
1 ms |
2656 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2656 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2916 KB |
Output is correct |
25 |
Correct |
2 ms |
2912 KB |
Output is correct |
26 |
Correct |
42 ms |
36944 KB |
Output is correct |
27 |
Correct |
45 ms |
35512 KB |
Output is correct |
28 |
Correct |
2 ms |
2912 KB |
Output is correct |
29 |
Correct |
2 ms |
3680 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
3176 KB |
Output is correct |
32 |
Correct |
2 ms |
2988 KB |
Output is correct |
33 |
Correct |
4 ms |
4328 KB |
Output is correct |
34 |
Correct |
4 ms |
4448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2656 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2656 KB |
Output is correct |
4 |
Correct |
1 ms |
2664 KB |
Output is correct |
5 |
Correct |
1 ms |
2660 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2660 KB |
Output is correct |
8 |
Correct |
1 ms |
2660 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
4820 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
1 ms |
2656 KB |
Output is correct |
15 |
Correct |
1 ms |
2664 KB |
Output is correct |
16 |
Correct |
1 ms |
2660 KB |
Output is correct |
17 |
Correct |
2 ms |
2912 KB |
Output is correct |
18 |
Correct |
1 ms |
2656 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
43 ms |
35824 KB |
Output is correct |
27 |
Correct |
42 ms |
36020 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
4188 KB |
Output is correct |
34 |
Correct |
4 ms |
4444 KB |
Output is correct |
35 |
Correct |
12 ms |
5084 KB |
Output is correct |
36 |
Correct |
2 ms |
3164 KB |
Output is correct |
37 |
Correct |
14 ms |
5980 KB |
Output is correct |
38 |
Correct |
18 ms |
6248 KB |
Output is correct |
39 |
Correct |
16 ms |
6256 KB |
Output is correct |
40 |
Correct |
17 ms |
6252 KB |
Output is correct |
41 |
Correct |
18 ms |
6236 KB |
Output is correct |
42 |
Runtime error |
335 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2400 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
4 ms |
4564 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2904 KB |
Output is correct |
26 |
Correct |
54 ms |
35624 KB |
Output is correct |
27 |
Correct |
48 ms |
35492 KB |
Output is correct |
28 |
Correct |
2 ms |
2912 KB |
Output is correct |
29 |
Correct |
3 ms |
3424 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
2940 KB |
Output is correct |
33 |
Correct |
4 ms |
4188 KB |
Output is correct |
34 |
Correct |
4 ms |
4188 KB |
Output is correct |
35 |
Correct |
13 ms |
5480 KB |
Output is correct |
36 |
Correct |
4 ms |
2904 KB |
Output is correct |
37 |
Correct |
14 ms |
6128 KB |
Output is correct |
38 |
Correct |
21 ms |
6492 KB |
Output is correct |
39 |
Correct |
17 ms |
6492 KB |
Output is correct |
40 |
Correct |
22 ms |
6492 KB |
Output is correct |
41 |
Correct |
19 ms |
6224 KB |
Output is correct |
42 |
Runtime error |
264 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |