#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define mp make_pair
#define fi first
#define lb lower_bound
#define se second
#define endl "\n"
using namespace std;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
int blk = 50;
vector<int> weights[n];
int init = -1, initp = -1, target = -1;
for(int i = 1; i <= m; ++i) {
int b, p;
cin >> b >> p;
if(i == 1)
init = b, initp = p;
else if(i == 2)
target = b;
// b -> pos
// p -> multiple
weights[b].push_back(p);
}
// gaperlu cek semua cukup cek yg ada weights
// kalo udh proc weight jg gperlu
// kalo <= blk maka memo tiap pair int modulo dalam vector
// kalo > blk maka cek manual
// doge cmn boleh gerak dr ori node (gaboleh meet di tengah)
// hence cukup dijkstra dengan 1 origin, terus klo intersect lainnya bs langsung dijkstra aja
// tiap node ada theoretically n^2 dijkstra state yg mgkn?
// tp yg high modulo itu sparse, jadi bs simpan manual pakai sorted vector/semacamnya
priority_queue<pair<int, pair<int, pair<int, int>>>, vector<pair<int, pair<int, pair<int, int>>>>, greater<pair<int, pair<int, pair<int, int>>>>> pq;
// fi -> dist
// se.fi -> idx
// se.se.fi -> current p
// se.se.se -> tanda dr atas/bawah
// guna -> reduce double counting
//cout << init << " " << initp << endl;
pq.push(mp(0, mp(init, mp(initp, -1))));
int d[n + 1];
map<int, bool> vis[n];
memset(d, -1, sizeof(d));
// cek ada yg bs sampai ke target atau tidak, kalo tidak langsung output false
// kalo iya, maka ada pembuktian min path tidak sepanjang itu?
while(pq.size()) {
int dist = pq.top().fi, idx = pq.top().se.fi, curp = pq.top().se.se.fi, tanda = pq.top().se.se.se;
pq.pop();
//cout << dist << " " << idx << " " << curp << endl;
if(vis[idx][curp])
continue;
if(d[idx] == -1)
d[idx] = dist;
d[idx] = min(d[idx], dist);
for(auto i : weights[idx]) {
if(!vis[idx][i] && i != curp) {
if(i > blk) {
int tmp = idx + i;
while(tmp < n && !weights[tmp].size())
tmp += i;
if(tmp < n && !vis[tmp][i])
pq.push(mp(dist + (tmp - idx) / i, mp(tmp, mp(i, 0))));
tmp = idx - i;
while(tmp >= 0 && !weights[tmp].size())
tmp -= i;
if(tmp >= 0 && !vis[tmp][i])
pq.push(mp(dist + (idx - tmp) / i, mp(tmp, mp(i, 1))));
}
else {
if(idx + i < n && !vis[idx + i][i])
pq.push(mp(dist + 1, mp(idx + i, mp(i, 0))));
if(idx - i >= 0 && !vis[idx - i][i])
pq.push(mp(dist + 1, mp(idx - i, mp(i, 1))));
}
vis[idx][i] = 1;
}
}
weights[idx].clear();
//cout << idx << " " << curp << endl;
if(!vis[idx][curp]) {
vis[idx][curp] = 1;
//cout << dist << " " << idx << " " << curp << endl;
if(curp > blk) {
int tmp = idx + curp;
if(tanda != 1) {
while(tmp < n && !weights[tmp].size())
tmp += curp;
if(tmp < n && !vis[tmp][curp])
pq.push(mp(dist + (tmp - idx) / curp, mp(tmp, mp(curp, 0))));
}
if(tanda != 0) {
tmp = idx - curp;
while(tmp >= 0 && !weights[tmp].size())
tmp -= curp;
if(tmp >= 0 && !vis[tmp][curp])
pq.push(mp(dist + (idx - tmp) / curp, mp(tmp, mp(curp, 1))));
}
}
else {
if(idx + curp < n && !vis[idx + curp][curp])
pq.push(mp(dist + 1, mp(idx + curp, mp(curp, 0))));
if(idx - curp >= 0 && !vis[idx - curp][curp])
pq.push(mp(dist + 1, mp(idx - curp, mp(curp, 1))));
}
}
if(idx == target)
break;
}
cout << d[target] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
24 ms |
3284 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
31 |
Correct |
13 ms |
1964 KB |
Output is correct |
32 |
Correct |
6 ms |
1492 KB |
Output is correct |
33 |
Correct |
37 ms |
4828 KB |
Output is correct |
34 |
Correct |
17 ms |
2448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
18 ms |
3248 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
31 |
Correct |
11 ms |
1960 KB |
Output is correct |
32 |
Correct |
6 ms |
1492 KB |
Output is correct |
33 |
Correct |
36 ms |
4816 KB |
Output is correct |
34 |
Correct |
16 ms |
2388 KB |
Output is correct |
35 |
Correct |
12 ms |
2384 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
9 ms |
2048 KB |
Output is correct |
38 |
Correct |
9 ms |
1492 KB |
Output is correct |
39 |
Correct |
5 ms |
596 KB |
Output is correct |
40 |
Correct |
5 ms |
596 KB |
Output is correct |
41 |
Correct |
7 ms |
1004 KB |
Output is correct |
42 |
Correct |
4 ms |
724 KB |
Output is correct |
43 |
Correct |
5 ms |
724 KB |
Output is correct |
44 |
Correct |
4 ms |
680 KB |
Output is correct |
45 |
Correct |
55 ms |
7108 KB |
Output is correct |
46 |
Correct |
21 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
20 ms |
3284 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
31 |
Correct |
9 ms |
2004 KB |
Output is correct |
32 |
Correct |
6 ms |
1492 KB |
Output is correct |
33 |
Correct |
28 ms |
4844 KB |
Output is correct |
34 |
Correct |
12 ms |
2352 KB |
Output is correct |
35 |
Correct |
12 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
10 ms |
2000 KB |
Output is correct |
38 |
Correct |
8 ms |
1492 KB |
Output is correct |
39 |
Correct |
6 ms |
596 KB |
Output is correct |
40 |
Correct |
6 ms |
596 KB |
Output is correct |
41 |
Correct |
9 ms |
980 KB |
Output is correct |
42 |
Correct |
4 ms |
724 KB |
Output is correct |
43 |
Correct |
6 ms |
724 KB |
Output is correct |
44 |
Correct |
5 ms |
724 KB |
Output is correct |
45 |
Correct |
52 ms |
7084 KB |
Output is correct |
46 |
Correct |
26 ms |
3348 KB |
Output is correct |
47 |
Correct |
11 ms |
3536 KB |
Output is correct |
48 |
Correct |
6 ms |
2388 KB |
Output is correct |
49 |
Correct |
5 ms |
2648 KB |
Output is correct |
50 |
Correct |
7 ms |
2644 KB |
Output is correct |
51 |
Correct |
16 ms |
4892 KB |
Output is correct |
52 |
Correct |
16 ms |
4988 KB |
Output is correct |
53 |
Correct |
10 ms |
3352 KB |
Output is correct |
54 |
Correct |
4 ms |
3156 KB |
Output is correct |
55 |
Correct |
4 ms |
3668 KB |
Output is correct |
56 |
Correct |
11 ms |
4796 KB |
Output is correct |
57 |
Correct |
3 ms |
2644 KB |
Output is correct |
58 |
Correct |
22 ms |
5656 KB |
Output is correct |
59 |
Correct |
14 ms |
5372 KB |
Output is correct |
60 |
Correct |
14 ms |
5336 KB |
Output is correct |
61 |
Correct |
12 ms |
4884 KB |
Output is correct |
62 |
Correct |
62 ms |
13412 KB |
Output is correct |
63 |
Correct |
16 ms |
5532 KB |
Output is correct |
64 |
Correct |
17 ms |
5076 KB |
Output is correct |
65 |
Correct |
18 ms |
5196 KB |
Output is correct |
66 |
Correct |
794 ms |
70352 KB |
Output is correct |
67 |
Correct |
83 ms |
12296 KB |
Output is correct |