// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
int main() {
ll N,M; cin >> N >> M;
//ll K = 5*floor(sqrt(N+0.001)); //MODIFY
ll K = 10;
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++) {
//val[i]=1e18;
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;
for (ll i=0;i<N;i++) {
if (val[i]<mv && !found[i]) {
mv = val[i]; ctr = i;
}
}
if (ctr==-1) {
cout << "-1"; exit(0);
} else if (ctr==B[1]) {
cout << val[B[1]]; exit(0);
} else {
for (pii x0: doges[ctr]) {
ll i = x0.first;
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) {
val[(B[i]+t*P[i])]=min(val[(B[i]+t*P[i])],mv+(ll)abs(t));
}
}
}
}
found[ctr]=1;
}*/
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];
//cout << "process2: "<<v<<", "<<x<<", "<<k<<"\n";
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();
}
}
//cout << "mv,ctr,lpop="<<mv<<","<<ctr<<","<<lpop<<"\n";
/*for (ll k=0;k<K;k++) {
while (!v2q[k].empty()) {
ll x = v2q[k].top();
if (!found2[k][x] && !found[x]) {
if (val2[k][x]<mv) {
mv = val2[k][x]; ctr = x; lpop = k;
}
break;
} else 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];
v2q[k].push(x-k);
}
if (((x+k)<N)&&(val2[k][x+k]>(1+val2[k][x]))) {
val2[k][x+k]=1+val2[k][x];
v2q[k].push(x+k);
}
}
}
}*/
if (lpop==-1) {
cout << "-1"; exit(0);
} else if (ctr==B[1]) {
//cout << val[B[1]]; exit(0);
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;
}
}
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
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 |
344 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 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 |
1 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 |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
344 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 |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 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 |
1 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 |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
600 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 |
860 KB |
Output is correct |
35 |
Correct |
8 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
636 KB |
Output is correct |
37 |
Correct |
6 ms |
1628 KB |
Output is correct |
38 |
Correct |
9 ms |
2140 KB |
Output is correct |
39 |
Correct |
8 ms |
1984 KB |
Output is correct |
40 |
Correct |
8 ms |
1884 KB |
Output is correct |
41 |
Correct |
10 ms |
1884 KB |
Output is correct |
42 |
Correct |
10 ms |
1752 KB |
Output is correct |
43 |
Correct |
8 ms |
1752 KB |
Output is correct |
44 |
Correct |
11 ms |
2688 KB |
Output is correct |
45 |
Correct |
11 ms |
1884 KB |
Output is correct |
46 |
Correct |
10 ms |
2024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 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 |
1 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 |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 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 |
1 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 |
608 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 |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
7 ms |
1652 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
6 ms |
1628 KB |
Output is correct |
38 |
Correct |
8 ms |
1968 KB |
Output is correct |
39 |
Correct |
8 ms |
1984 KB |
Output is correct |
40 |
Correct |
8 ms |
1888 KB |
Output is correct |
41 |
Correct |
9 ms |
2028 KB |
Output is correct |
42 |
Correct |
6 ms |
1752 KB |
Output is correct |
43 |
Correct |
6 ms |
1752 KB |
Output is correct |
44 |
Correct |
9 ms |
2676 KB |
Output is correct |
45 |
Correct |
9 ms |
1884 KB |
Output is correct |
46 |
Correct |
8 ms |
1904 KB |
Output is correct |
47 |
Correct |
11 ms |
3564 KB |
Output is correct |
48 |
Correct |
8 ms |
3932 KB |
Output is correct |
49 |
Correct |
8 ms |
4188 KB |
Output is correct |
50 |
Correct |
6 ms |
4444 KB |
Output is correct |
51 |
Correct |
22 ms |
6348 KB |
Output is correct |
52 |
Correct |
16 ms |
6356 KB |
Output is correct |
53 |
Correct |
11 ms |
5136 KB |
Output is correct |
54 |
Correct |
3 ms |
3932 KB |
Output is correct |
55 |
Correct |
3 ms |
4032 KB |
Output is correct |
56 |
Correct |
12 ms |
5212 KB |
Output is correct |
57 |
Correct |
3 ms |
4828 KB |
Output is correct |
58 |
Correct |
12 ms |
6388 KB |
Output is correct |
59 |
Correct |
12 ms |
5336 KB |
Output is correct |
60 |
Correct |
10 ms |
5320 KB |
Output is correct |
61 |
Correct |
13 ms |
5336 KB |
Output is correct |
62 |
Correct |
17 ms |
5776 KB |
Output is correct |
63 |
Correct |
23 ms |
6872 KB |
Output is correct |
64 |
Correct |
22 ms |
6664 KB |
Output is correct |
65 |
Correct |
27 ms |
7640 KB |
Output is correct |
66 |
Correct |
42 ms |
7644 KB |
Output is correct |
67 |
Correct |
31 ms |
7640 KB |
Output is correct |