#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MAX = 50000;
int k, n, m, q;
int A[MAX], B[MAX];
vector<int> blk[MAX];
int ans[MAX], ID[MAX];
int L[MAX][5], R[MAX][5];
vector<pair<int, int>> go[MAX], back[MAX];
bool minimize(int &x, const int &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
void divi(int l, int r, vector<int> &v) {
if (v.empty()) return;
if (l == r) {
for (int &id: v) {
if (A[id] == B[id]) ans[id] = 0;
else ans[id] = INF;
}
return;
}
int mid = l + r >> 1;
int lef = l * k, rig = min(n, r * k + k) - 1;
for (int i = lef; i <= rig; ++i) {
for (int t = 0; t < k; ++t) {
L[i][t] = R[i][t] = INF;
}
}
for (int i = 0; i < blk[mid].size(); ++i) {
int u = blk[mid][i]; L[u][i] = 0;
for (pair<int, int> &x: go[u]) {
int v = x.first, w = x.second;
R[v][i] = w;
}
}
for (int i = mid; i >= l + 1; --i) {
for (int &u: blk[i]) {
for (int t = 0; t < k; ++t) {
for (pair<int, int> &x: back[u]) {
int v = x.first, w = x.second;
minimize(L[v][t], L[u][t] + w);
}
}
}
}
for (int i = mid + 1; i <= r - 1; ++i) {
for (int &u: blk[i]) {
for (int t = 0; t < k; ++t) {
for (pair<int, int> &x: go[u]) {
int v = x.first, w = x.second;
minimize(R[v][t], R[u][t] + w);
}
}
}
}
vector<int> todo[2];
for (int &id: v) {
if (ID[A[id]] <= mid && mid < ID[B[id]]) {
for (int t = 0; t < k; ++t)
minimize(ans[id], L[A[id]][t] + R[B[id]][t]);
} else {
todo[ID[A[id]] > mid].push_back(id);
}
}
divi(l, mid, todo[0]); divi(mid + 1, r, todo[1]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n >> m >> q;
while (m--) {
int a, b, t; cin >> a >> b >> t;
back[b].push_back({a, t});
go[a].push_back({b, t});
}
for (int i = 0; i < n; ++i) {
ID[i] = i / k;
blk[ID[i]].push_back(i);
}
vector<int> todo;
for (int i = 0; i < q; ++i) {
cin >> A[i] >> B[i];
todo.push_back(i);
}
memset(ans, 0x3f, sizeof ans);
divi(0, (n - 1) / k, todo);
for (int i = 0; i < q; ++i) {
if (ans[i] >= INF) cout << "-1\n";
else cout << ans[i] << '\n';
}
return 0;
}
Compilation message
toll.cpp: In function 'void divi(int, int, std::vector<int>&)':
toll.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = l + r >> 1;
| ~~^~~
toll.cpp:37:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < blk[mid].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
11092 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
23 ms |
11076 KB |
Output is correct |
9 |
Correct |
24 ms |
10968 KB |
Output is correct |
10 |
Correct |
7 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
10288 KB |
Output is correct |
2 |
Correct |
2 ms |
3984 KB |
Output is correct |
3 |
Correct |
3 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
3984 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
4 ms |
4384 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
21 ms |
11992 KB |
Output is correct |
10 |
Correct |
51 ms |
13612 KB |
Output is correct |
11 |
Correct |
35 ms |
11988 KB |
Output is correct |
12 |
Correct |
42 ms |
11140 KB |
Output is correct |
13 |
Correct |
66 ms |
13092 KB |
Output is correct |
14 |
Correct |
32 ms |
10088 KB |
Output is correct |
15 |
Correct |
28 ms |
9428 KB |
Output is correct |
16 |
Correct |
30 ms |
9464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
3988 KB |
Output is correct |
6 |
Correct |
2 ms |
4092 KB |
Output is correct |
7 |
Correct |
3 ms |
4176 KB |
Output is correct |
8 |
Correct |
4 ms |
4308 KB |
Output is correct |
9 |
Correct |
5 ms |
4260 KB |
Output is correct |
10 |
Correct |
20 ms |
11604 KB |
Output is correct |
11 |
Correct |
41 ms |
11560 KB |
Output is correct |
12 |
Correct |
53 ms |
13248 KB |
Output is correct |
13 |
Correct |
59 ms |
14192 KB |
Output is correct |
14 |
Correct |
47 ms |
12292 KB |
Output is correct |
15 |
Correct |
36 ms |
9320 KB |
Output is correct |
16 |
Correct |
33 ms |
9400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
3988 KB |
Output is correct |
6 |
Correct |
2 ms |
4092 KB |
Output is correct |
7 |
Correct |
3 ms |
4176 KB |
Output is correct |
8 |
Correct |
4 ms |
4308 KB |
Output is correct |
9 |
Correct |
5 ms |
4260 KB |
Output is correct |
10 |
Correct |
20 ms |
11604 KB |
Output is correct |
11 |
Correct |
41 ms |
11560 KB |
Output is correct |
12 |
Correct |
53 ms |
13248 KB |
Output is correct |
13 |
Correct |
59 ms |
14192 KB |
Output is correct |
14 |
Correct |
47 ms |
12292 KB |
Output is correct |
15 |
Correct |
36 ms |
9320 KB |
Output is correct |
16 |
Correct |
33 ms |
9400 KB |
Output is correct |
17 |
Correct |
42 ms |
11648 KB |
Output is correct |
18 |
Correct |
2 ms |
4052 KB |
Output is correct |
19 |
Correct |
3 ms |
4052 KB |
Output is correct |
20 |
Correct |
3 ms |
3984 KB |
Output is correct |
21 |
Correct |
2 ms |
4052 KB |
Output is correct |
22 |
Correct |
2 ms |
4052 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
24 |
Correct |
3 ms |
4308 KB |
Output is correct |
25 |
Correct |
4 ms |
4308 KB |
Output is correct |
26 |
Correct |
4 ms |
4256 KB |
Output is correct |
27 |
Correct |
22 ms |
11748 KB |
Output is correct |
28 |
Correct |
60 ms |
13388 KB |
Output is correct |
29 |
Correct |
67 ms |
14280 KB |
Output is correct |
30 |
Correct |
51 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
11092 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
23 ms |
11076 KB |
Output is correct |
9 |
Correct |
24 ms |
10968 KB |
Output is correct |
10 |
Correct |
7 ms |
7892 KB |
Output is correct |
11 |
Correct |
38 ms |
10288 KB |
Output is correct |
12 |
Correct |
2 ms |
3984 KB |
Output is correct |
13 |
Correct |
3 ms |
4052 KB |
Output is correct |
14 |
Correct |
2 ms |
4052 KB |
Output is correct |
15 |
Correct |
2 ms |
3984 KB |
Output is correct |
16 |
Correct |
3 ms |
4052 KB |
Output is correct |
17 |
Correct |
4 ms |
4384 KB |
Output is correct |
18 |
Correct |
4 ms |
4436 KB |
Output is correct |
19 |
Correct |
21 ms |
11992 KB |
Output is correct |
20 |
Correct |
51 ms |
13612 KB |
Output is correct |
21 |
Correct |
35 ms |
11988 KB |
Output is correct |
22 |
Correct |
42 ms |
11140 KB |
Output is correct |
23 |
Correct |
66 ms |
13092 KB |
Output is correct |
24 |
Correct |
32 ms |
10088 KB |
Output is correct |
25 |
Correct |
28 ms |
9428 KB |
Output is correct |
26 |
Correct |
30 ms |
9464 KB |
Output is correct |
27 |
Correct |
2 ms |
4052 KB |
Output is correct |
28 |
Correct |
2 ms |
4052 KB |
Output is correct |
29 |
Correct |
2 ms |
4052 KB |
Output is correct |
30 |
Correct |
2 ms |
4052 KB |
Output is correct |
31 |
Correct |
2 ms |
3988 KB |
Output is correct |
32 |
Correct |
2 ms |
4092 KB |
Output is correct |
33 |
Correct |
3 ms |
4176 KB |
Output is correct |
34 |
Correct |
4 ms |
4308 KB |
Output is correct |
35 |
Correct |
5 ms |
4260 KB |
Output is correct |
36 |
Correct |
20 ms |
11604 KB |
Output is correct |
37 |
Correct |
41 ms |
11560 KB |
Output is correct |
38 |
Correct |
53 ms |
13248 KB |
Output is correct |
39 |
Correct |
59 ms |
14192 KB |
Output is correct |
40 |
Correct |
47 ms |
12292 KB |
Output is correct |
41 |
Correct |
36 ms |
9320 KB |
Output is correct |
42 |
Correct |
33 ms |
9400 KB |
Output is correct |
43 |
Correct |
42 ms |
11648 KB |
Output is correct |
44 |
Correct |
2 ms |
4052 KB |
Output is correct |
45 |
Correct |
3 ms |
4052 KB |
Output is correct |
46 |
Correct |
3 ms |
3984 KB |
Output is correct |
47 |
Correct |
2 ms |
4052 KB |
Output is correct |
48 |
Correct |
2 ms |
4052 KB |
Output is correct |
49 |
Correct |
3 ms |
4308 KB |
Output is correct |
50 |
Correct |
3 ms |
4308 KB |
Output is correct |
51 |
Correct |
4 ms |
4308 KB |
Output is correct |
52 |
Correct |
4 ms |
4256 KB |
Output is correct |
53 |
Correct |
22 ms |
11748 KB |
Output is correct |
54 |
Correct |
60 ms |
13388 KB |
Output is correct |
55 |
Correct |
67 ms |
14280 KB |
Output is correct |
56 |
Correct |
51 ms |
12380 KB |
Output is correct |
57 |
Correct |
83 ms |
15084 KB |
Output is correct |
58 |
Correct |
25 ms |
12028 KB |
Output is correct |
59 |
Correct |
43 ms |
12008 KB |
Output is correct |