#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 30001;
bool used[NMAX][151];
bool use1[NMAX][151];
vector <vector <int> > vv(NMAX);
vector <pii> g[NMAX];
int d[NMAX][151];
int main(){
int n; cin >> n;
int m; cin >> m;
int b[m + 1], p[m + 1];
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
if(p[i] > 150){
int x = b[i] - p[i];
int cnt = 1;
while(x >= 0){
g[b[i]].pb({x, cnt++});
x -= p[i];
}
x = b[i] + p[i];
cnt = 1;
while(x < n){
g[b[i]].pb({x, cnt++});
x += p[i];
}
continue;
}
if(!use1[b[i]][p[i]])
use1[b[i]][p[i]] = true, vv[b[i]].pb(p[i]);
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= 150; j++)
d[i][j] = 1e9;
}
d[b[0]][0] = 0;
priority_queue<pair<int, pii> > q;
q.push({0, {b[0], 0}});
while(!q.empty()){
int x = q.top().s.s, v = q.top().s.f;
q.pop();
if(used[v][x]) continue;
used[v][x] = true;
if(x == 0){
for(int to : vv[v]){
if(d[v][to] > d[v][x])
d[v][to] = d[v][x], q.push({-d[v][to], {v, to}});
}
for(auto to : g[v]){
if(d[to.f][0] > d[v][x] + to.s)
d[to.f][0] = d[v][x] + to.s, q.push({-d[to.f][0], {to.f,0}});
}
}
else{
if(d[v][0] > d[v][x])
d[v][0] = d[v][x], q.push({-d[v][0], {v, 0}});
if(v >= x && d[v - x][x] > d[v][x] + 1)
d[v - x][x] = d[v][x] + 1, q.push({-d[v - x][x], {v - x, x}});
if(v + x < n && d[v + x][x] > d[v][x] + 1)
d[v + x][x] = d[v][x] + 1, q.push({-d[v + x][x], {v + x, x}});
}
}
if(d[b[1]][0] == 1e9)
d[b[1]][0] = -1;
cout << d[b[1]][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1732 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
2 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1732 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
2 ms |
1732 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1736 KB |
Output is correct |
6 |
Correct |
1 ms |
1736 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1736 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1872 KB |
Output is correct |
17 |
Correct |
3 ms |
2388 KB |
Output is correct |
18 |
Correct |
3 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
3020 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
2000 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
3 ms |
3028 KB |
Output is correct |
24 |
Correct |
4 ms |
3436 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
4 ms |
3288 KB |
Output is correct |
27 |
Correct |
3 ms |
3288 KB |
Output is correct |
28 |
Correct |
4 ms |
3312 KB |
Output is correct |
29 |
Correct |
9 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
3156 KB |
Output is correct |
31 |
Correct |
5 ms |
3156 KB |
Output is correct |
32 |
Correct |
4 ms |
3156 KB |
Output is correct |
33 |
Correct |
15 ms |
3276 KB |
Output is correct |
34 |
Correct |
16 ms |
3240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1736 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1872 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
3 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
2892 KB |
Output is correct |
19 |
Correct |
3 ms |
3028 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
2000 KB |
Output is correct |
22 |
Correct |
2 ms |
2888 KB |
Output is correct |
23 |
Correct |
3 ms |
3028 KB |
Output is correct |
24 |
Correct |
4 ms |
3408 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
3 ms |
3412 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
4 ms |
3284 KB |
Output is correct |
29 |
Correct |
8 ms |
3152 KB |
Output is correct |
30 |
Correct |
3 ms |
3156 KB |
Output is correct |
31 |
Correct |
6 ms |
3148 KB |
Output is correct |
32 |
Correct |
4 ms |
3156 KB |
Output is correct |
33 |
Correct |
15 ms |
3276 KB |
Output is correct |
34 |
Correct |
15 ms |
3264 KB |
Output is correct |
35 |
Correct |
20 ms |
3796 KB |
Output is correct |
36 |
Correct |
5 ms |
2768 KB |
Output is correct |
37 |
Correct |
25 ms |
4692 KB |
Output is correct |
38 |
Correct |
28 ms |
4852 KB |
Output is correct |
39 |
Correct |
27 ms |
4820 KB |
Output is correct |
40 |
Correct |
27 ms |
4816 KB |
Output is correct |
41 |
Correct |
27 ms |
4836 KB |
Output is correct |
42 |
Correct |
13 ms |
3740 KB |
Output is correct |
43 |
Correct |
13 ms |
3708 KB |
Output is correct |
44 |
Correct |
14 ms |
3928 KB |
Output is correct |
45 |
Correct |
53 ms |
5788 KB |
Output is correct |
46 |
Correct |
52 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1732 KB |
Output is correct |
2 |
Correct |
1 ms |
1736 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1736 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
5 ms |
2392 KB |
Output is correct |
18 |
Correct |
4 ms |
2912 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
3 ms |
3028 KB |
Output is correct |
24 |
Correct |
4 ms |
3412 KB |
Output is correct |
25 |
Correct |
4 ms |
3284 KB |
Output is correct |
26 |
Correct |
4 ms |
3412 KB |
Output is correct |
27 |
Correct |
4 ms |
3284 KB |
Output is correct |
28 |
Correct |
4 ms |
3284 KB |
Output is correct |
29 |
Correct |
8 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
3156 KB |
Output is correct |
31 |
Correct |
6 ms |
3156 KB |
Output is correct |
32 |
Correct |
5 ms |
3156 KB |
Output is correct |
33 |
Correct |
16 ms |
3208 KB |
Output is correct |
34 |
Correct |
16 ms |
3284 KB |
Output is correct |
35 |
Correct |
20 ms |
3796 KB |
Output is correct |
36 |
Correct |
5 ms |
2784 KB |
Output is correct |
37 |
Correct |
25 ms |
4676 KB |
Output is correct |
38 |
Correct |
27 ms |
4832 KB |
Output is correct |
39 |
Correct |
29 ms |
4756 KB |
Output is correct |
40 |
Correct |
28 ms |
4820 KB |
Output is correct |
41 |
Correct |
28 ms |
4848 KB |
Output is correct |
42 |
Correct |
14 ms |
3796 KB |
Output is correct |
43 |
Correct |
13 ms |
3668 KB |
Output is correct |
44 |
Correct |
17 ms |
3912 KB |
Output is correct |
45 |
Correct |
54 ms |
5744 KB |
Output is correct |
46 |
Correct |
54 ms |
5772 KB |
Output is correct |
47 |
Correct |
99 ms |
17284 KB |
Output is correct |
48 |
Correct |
23 ms |
17484 KB |
Output is correct |
49 |
Correct |
25 ms |
18388 KB |
Output is correct |
50 |
Correct |
20 ms |
19540 KB |
Output is correct |
51 |
Correct |
70 ms |
27072 KB |
Output is correct |
52 |
Correct |
74 ms |
27292 KB |
Output is correct |
53 |
Correct |
38 ms |
24572 KB |
Output is correct |
54 |
Correct |
14 ms |
23816 KB |
Output is correct |
55 |
Correct |
15 ms |
23772 KB |
Output is correct |
56 |
Correct |
35 ms |
29612 KB |
Output is correct |
57 |
Correct |
85 ms |
27808 KB |
Output is correct |
58 |
Correct |
25 ms |
24116 KB |
Output is correct |
59 |
Correct |
35 ms |
24400 KB |
Output is correct |
60 |
Correct |
36 ms |
24908 KB |
Output is correct |
61 |
Correct |
34 ms |
24600 KB |
Output is correct |
62 |
Correct |
65 ms |
26904 KB |
Output is correct |
63 |
Correct |
96 ms |
48664 KB |
Output is correct |
64 |
Correct |
97 ms |
56496 KB |
Output is correct |
65 |
Correct |
238 ms |
56428 KB |
Output is correct |
66 |
Correct |
697 ms |
51540 KB |
Output is correct |
67 |
Correct |
693 ms |
51704 KB |
Output is correct |