#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
const int O = 1e4 + 5;
const int K = 6;
const int INF = 1e9;
int k, n, m, o;
vector<array<int, 3>> g[K][N], rg[K][N];
int ans[O];
int d[K][N], rd[K][N];
void bfs(int row, int col, int r) {
for (int i = 0; i < k; i++) {
for (int j = col; j <= r; j++) {
d[i][j] = INF;
}
}
priority_queue<pair<int, pair<int, int>>> pq;
d[row][col] = 0;
pq.push({-d[row][col], {row, col}});
while (!pq.empty()) {
int cd = -pq.top().first;
int x = pq.top().second.first;
int y = pq.top().second.second;
pq.pop();
if (cd != d[x][y]) {
continue;
}
for (auto &nxt : g[x][y]) {
if (nxt[1] > r) {
continue;
}
if (d[nxt[0]][nxt[1]] > d[x][y] + nxt[2]) {
d[nxt[0]][nxt[1]] = d[x][y] + nxt[2];
pq.push({-d[nxt[0]][nxt[1]], {nxt[0], nxt[1]}});
}
}
}
}
void sfb(int row, int col, int l) {
for (int i = 0; i < k; i++) {
for (int j = col; j >= l; j--) {
rd[i][j] = INF;
}
}
priority_queue<pair<int, pair<int, int>>> pq;
rd[row][col] = 0;
pq.push({-rd[row][col], {row, col}});
while (!pq.empty()) {
int cd = -pq.top().first;
int x = pq.top().second.first;
int y = pq.top().second.second;
pq.pop();
if (cd != rd[x][y]) {
continue;
}
for (auto &nxt : rg[x][y]) {
if (nxt[1] < l) {
continue;
}
if (rd[nxt[0]][nxt[1]] > rd[x][y] + nxt[2]) {
rd[nxt[0]][nxt[1]] = rd[x][y] + nxt[2];
pq.push({-rd[nxt[0]][nxt[1]], {nxt[0], nxt[1]}});
}
}
}
}
void solve(int l, int r, vector<array<int, 5>> &q) {
if (q.empty()) {
return;
}
int m = (l + r) >> 1;
vector<array<int, 5>> ql, qr, qc;
for (auto &x : q) {
if (x[3] < m) {
ql.emplace_back(x);
} else if (x[1] > m) {
qr.emplace_back(x);
} else {
qc.emplace_back(x);
}
}
for (int row = 0; row < k; row++) {
bfs(row, m, r);
sfb(row, m, l);
for (auto &x : qc) {
ans[x[4]] = min(ans[x[4]], rd[x[0]][x[1]] + d[x[2]][x[3]]);
}
}
solve(l, m - 1, ql);
solve(m + 1, r, qr);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> k >> n >> m >> o;
for (int i = 0; i < m; i++) {
int a, b, t;
cin >> a >> b >> t;
g[a % k][a / k].push_back({b % k, b / k, t});
rg[b % k][b / k].push_back({a % k, a / k, t});
}
vector<array<int, 5>> que(o);
for (int i = 0; i < o; i++) {
int a, b;
cin >> a >> b;
que[i] = {a % k, a / k, b % k, b / k, i};
}
for (int i = 0; i < o; i++) {
ans[i] = INF;
}
solve(0, n - 1, que);
for (int i = 0; i < o; i++) {
if (ans[i] == INF) {
ans[i] = -1;
}
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
21488 KB |
Output is correct |
2 |
Correct |
5 ms |
16728 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16820 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
5 ms |
16732 KB |
Output is correct |
8 |
Correct |
27 ms |
21336 KB |
Output is correct |
9 |
Correct |
22 ms |
21300 KB |
Output is correct |
10 |
Correct |
6 ms |
17500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
22268 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
5 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
7 ms |
17340 KB |
Output is correct |
8 |
Correct |
8 ms |
17556 KB |
Output is correct |
9 |
Correct |
23 ms |
21424 KB |
Output is correct |
10 |
Correct |
105 ms |
25744 KB |
Output is correct |
11 |
Correct |
50 ms |
22416 KB |
Output is correct |
12 |
Correct |
37 ms |
22112 KB |
Output is correct |
13 |
Correct |
109 ms |
26708 KB |
Output is correct |
14 |
Correct |
54 ms |
22580 KB |
Output is correct |
15 |
Correct |
56 ms |
21328 KB |
Output is correct |
16 |
Correct |
55 ms |
21540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16764 KB |
Output is correct |
4 |
Correct |
4 ms |
16760 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
5 ms |
16772 KB |
Output is correct |
7 |
Correct |
6 ms |
16732 KB |
Output is correct |
8 |
Correct |
9 ms |
16976 KB |
Output is correct |
9 |
Correct |
9 ms |
16976 KB |
Output is correct |
10 |
Correct |
28 ms |
20760 KB |
Output is correct |
11 |
Correct |
60 ms |
21328 KB |
Output is correct |
12 |
Correct |
124 ms |
24936 KB |
Output is correct |
13 |
Correct |
127 ms |
25916 KB |
Output is correct |
14 |
Correct |
94 ms |
23452 KB |
Output is correct |
15 |
Correct |
80 ms |
21084 KB |
Output is correct |
16 |
Correct |
93 ms |
21092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16764 KB |
Output is correct |
4 |
Correct |
4 ms |
16760 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
5 ms |
16772 KB |
Output is correct |
7 |
Correct |
6 ms |
16732 KB |
Output is correct |
8 |
Correct |
9 ms |
16976 KB |
Output is correct |
9 |
Correct |
9 ms |
16976 KB |
Output is correct |
10 |
Correct |
28 ms |
20760 KB |
Output is correct |
11 |
Correct |
60 ms |
21328 KB |
Output is correct |
12 |
Correct |
124 ms |
24936 KB |
Output is correct |
13 |
Correct |
127 ms |
25916 KB |
Output is correct |
14 |
Correct |
94 ms |
23452 KB |
Output is correct |
15 |
Correct |
80 ms |
21084 KB |
Output is correct |
16 |
Correct |
93 ms |
21092 KB |
Output is correct |
17 |
Correct |
75 ms |
21828 KB |
Output is correct |
18 |
Correct |
4 ms |
16728 KB |
Output is correct |
19 |
Correct |
4 ms |
16728 KB |
Output is correct |
20 |
Correct |
4 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16728 KB |
Output is correct |
22 |
Correct |
5 ms |
16732 KB |
Output is correct |
23 |
Correct |
6 ms |
16988 KB |
Output is correct |
24 |
Correct |
6 ms |
17136 KB |
Output is correct |
25 |
Correct |
9 ms |
17244 KB |
Output is correct |
26 |
Correct |
8 ms |
17240 KB |
Output is correct |
27 |
Correct |
24 ms |
20816 KB |
Output is correct |
28 |
Correct |
136 ms |
25196 KB |
Output is correct |
29 |
Correct |
157 ms |
26188 KB |
Output is correct |
30 |
Correct |
107 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
21488 KB |
Output is correct |
2 |
Correct |
5 ms |
16728 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16820 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
5 ms |
16732 KB |
Output is correct |
8 |
Correct |
27 ms |
21336 KB |
Output is correct |
9 |
Correct |
22 ms |
21300 KB |
Output is correct |
10 |
Correct |
6 ms |
17500 KB |
Output is correct |
11 |
Correct |
49 ms |
22268 KB |
Output is correct |
12 |
Correct |
4 ms |
16732 KB |
Output is correct |
13 |
Correct |
4 ms |
16732 KB |
Output is correct |
14 |
Correct |
5 ms |
16732 KB |
Output is correct |
15 |
Correct |
5 ms |
16732 KB |
Output is correct |
16 |
Correct |
4 ms |
16732 KB |
Output is correct |
17 |
Correct |
7 ms |
17340 KB |
Output is correct |
18 |
Correct |
8 ms |
17556 KB |
Output is correct |
19 |
Correct |
23 ms |
21424 KB |
Output is correct |
20 |
Correct |
105 ms |
25744 KB |
Output is correct |
21 |
Correct |
50 ms |
22416 KB |
Output is correct |
22 |
Correct |
37 ms |
22112 KB |
Output is correct |
23 |
Correct |
109 ms |
26708 KB |
Output is correct |
24 |
Correct |
54 ms |
22580 KB |
Output is correct |
25 |
Correct |
56 ms |
21328 KB |
Output is correct |
26 |
Correct |
55 ms |
21540 KB |
Output is correct |
27 |
Correct |
5 ms |
16732 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16764 KB |
Output is correct |
30 |
Correct |
4 ms |
16760 KB |
Output is correct |
31 |
Correct |
4 ms |
16732 KB |
Output is correct |
32 |
Correct |
5 ms |
16772 KB |
Output is correct |
33 |
Correct |
6 ms |
16732 KB |
Output is correct |
34 |
Correct |
9 ms |
16976 KB |
Output is correct |
35 |
Correct |
9 ms |
16976 KB |
Output is correct |
36 |
Correct |
28 ms |
20760 KB |
Output is correct |
37 |
Correct |
60 ms |
21328 KB |
Output is correct |
38 |
Correct |
124 ms |
24936 KB |
Output is correct |
39 |
Correct |
127 ms |
25916 KB |
Output is correct |
40 |
Correct |
94 ms |
23452 KB |
Output is correct |
41 |
Correct |
80 ms |
21084 KB |
Output is correct |
42 |
Correct |
93 ms |
21092 KB |
Output is correct |
43 |
Correct |
75 ms |
21828 KB |
Output is correct |
44 |
Correct |
4 ms |
16728 KB |
Output is correct |
45 |
Correct |
4 ms |
16728 KB |
Output is correct |
46 |
Correct |
4 ms |
16732 KB |
Output is correct |
47 |
Correct |
4 ms |
16728 KB |
Output is correct |
48 |
Correct |
5 ms |
16732 KB |
Output is correct |
49 |
Correct |
6 ms |
16988 KB |
Output is correct |
50 |
Correct |
6 ms |
17136 KB |
Output is correct |
51 |
Correct |
9 ms |
17244 KB |
Output is correct |
52 |
Correct |
8 ms |
17240 KB |
Output is correct |
53 |
Correct |
24 ms |
20816 KB |
Output is correct |
54 |
Correct |
136 ms |
25196 KB |
Output is correct |
55 |
Correct |
157 ms |
26188 KB |
Output is correct |
56 |
Correct |
107 ms |
23808 KB |
Output is correct |
57 |
Correct |
266 ms |
27604 KB |
Output is correct |
58 |
Correct |
26 ms |
21496 KB |
Output is correct |
59 |
Correct |
80 ms |
22424 KB |
Output is correct |