#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ll N,M; cin >> N >> M;
ll K = 9;
ll B[M],P[M];
ll val[N];
ll val2[K][N];
bool found[N];
bool found2[K][N];
vector<pii> doges[N];
for (ll i=0;i<M;i++) {
cin >> B[i] >> P[i];
doges[B[i]].push_back({i,P[i]});
}
for (ll i=0;i<N;i++) {
for (ll k=0;k<K;k++) {
val2[k][i]=1e18;
found2[k][i]=0;
}
val[i]=1e18;
found[i]=0;
}
val[B[0]]=0;
priority_queue<pair<ll,ll>> PQ; //-val, position
priority_queue<array<ll,3>> PQ2; //-val, position, k
PQ.push({0,B[0]});
while(1) {
ll mv = 1e17; ll ctr = -1;
ll lpop = -1;
while (!PQ.empty()) {
pii q0 = PQ.top(); ll v = -q0.first; ll x = q0.second;
if (!found[x]) {
ctr = x; mv = v; lpop = 0;
break;
} else {
PQ.pop();
}
}
while (!PQ2.empty()) {
array<ll,3> a0 = PQ2.top(); ll v = -a0[0]; ll x = a0[1]; ll k = a0[2];
if (v>=mv) {break;}
if (!found2[k][x]) {
found2[k][x]=1;
if (((x-k)>=0)&&(val2[k][x-k]>(1+val2[k][x]))) {
val2[k][x-k]=1+val2[k][x];
PQ2.push({-val2[k][x-k],(x-k),k});
}
if (((x+k)<N)&&(val2[k][x+k]>(1+val2[k][x]))) {
val2[k][x+k]=1+val2[k][x];
PQ2.push({-val2[k][x+k],(x+k),k});
}
if (!found[x]) {
if (val2[k][x]<mv) {
mv = val2[k][x]; ctr = x; lpop = 1;
}
break;
} else {
PQ2.pop();
}
} else {
PQ2.pop();
}
}
if (lpop==-1) {
cout << "-1"; exit(0);
} else if (ctr==B[1]) {
ll ans = val[B[1]];
for (ll k=0;k<K;k++) {
ans = min(ans,val2[k][B[1]]);
}
cout << ans; exit(0);
} else {
if (lpop) {
PQ2.pop();
} else {
PQ.pop();
}
for (pii x0: doges[ctr]) {
ll i = x0.first;
if (P[i]>=K) {
for (ll t=(-(B[i]/P[i]+2));t<(((N-B[i])/P[i])+2);t++) {
if ((B[i]+t*P[i])>=0 && (B[i]+t*P[i])<N) {
if ((mv+(ll)abs(t))<val[(B[i]+t*P[i])]) {
val[(B[i]+t*P[i])]=mv+(ll)abs(t);
PQ.push({-val[(B[i]+t*P[i])],(B[i]+t*P[i])});
}
}
}
} else {
val2[P[i]][B[i]]=mv;
PQ2.push({-mv,B[i],P[i]});
}
}
}
found[ctr]=1;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
764 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
604 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 |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
608 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
1628 KB |
Output is correct |
38 |
Correct |
4 ms |
2160 KB |
Output is correct |
39 |
Correct |
4 ms |
2008 KB |
Output is correct |
40 |
Correct |
4 ms |
1884 KB |
Output is correct |
41 |
Correct |
4 ms |
1884 KB |
Output is correct |
42 |
Correct |
3 ms |
1760 KB |
Output is correct |
43 |
Correct |
3 ms |
1756 KB |
Output is correct |
44 |
Correct |
5 ms |
2796 KB |
Output is correct |
45 |
Correct |
5 ms |
2032 KB |
Output is correct |
46 |
Correct |
5 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
968 KB |
Output is correct |
35 |
Correct |
3 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
680 KB |
Output is correct |
37 |
Correct |
3 ms |
1628 KB |
Output is correct |
38 |
Correct |
4 ms |
2136 KB |
Output is correct |
39 |
Correct |
3 ms |
1796 KB |
Output is correct |
40 |
Correct |
4 ms |
1884 KB |
Output is correct |
41 |
Correct |
4 ms |
1884 KB |
Output is correct |
42 |
Correct |
3 ms |
1760 KB |
Output is correct |
43 |
Correct |
3 ms |
1756 KB |
Output is correct |
44 |
Correct |
5 ms |
2796 KB |
Output is correct |
45 |
Correct |
5 ms |
1812 KB |
Output is correct |
46 |
Correct |
4 ms |
1884 KB |
Output is correct |
47 |
Correct |
6 ms |
3292 KB |
Output is correct |
48 |
Correct |
4 ms |
3932 KB |
Output is correct |
49 |
Correct |
4 ms |
4184 KB |
Output is correct |
50 |
Correct |
3 ms |
4188 KB |
Output is correct |
51 |
Correct |
9 ms |
6236 KB |
Output is correct |
52 |
Correct |
11 ms |
6104 KB |
Output is correct |
53 |
Correct |
6 ms |
4956 KB |
Output is correct |
54 |
Correct |
2 ms |
3676 KB |
Output is correct |
55 |
Correct |
3 ms |
3676 KB |
Output is correct |
56 |
Correct |
9 ms |
4956 KB |
Output is correct |
57 |
Correct |
3 ms |
4568 KB |
Output is correct |
58 |
Correct |
9 ms |
6108 KB |
Output is correct |
59 |
Correct |
6 ms |
4832 KB |
Output is correct |
60 |
Correct |
7 ms |
4924 KB |
Output is correct |
61 |
Correct |
6 ms |
4824 KB |
Output is correct |
62 |
Correct |
9 ms |
4988 KB |
Output is correct |
63 |
Correct |
19 ms |
6104 KB |
Output is correct |
64 |
Correct |
18 ms |
6104 KB |
Output is correct |
65 |
Correct |
22 ms |
7120 KB |
Output is correct |
66 |
Correct |
35 ms |
7120 KB |
Output is correct |
67 |
Correct |
27 ms |
7132 KB |
Output is correct |