#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define pb push_back
#define f first
#define s second
int K, N, M, O; vector<pii> adj[50010]; vector<pii> Q;
int DP[50010][5][5]; int ans[100010]; const int INF = 1061109567;
void divide(int L, int R, vector<int> V) {
if (R < L) return ; int mid = (L + R) / 2;
for (int i = L; i <= R; i++) memset(DP[i], 0x3f, sizeof DP[i]);
for (int i = 0; i < K; i++) for (int j = 0; j < K; j++) DP[mid][i][j] = (i == j) ? 0 : INF;
for (int i = mid - 1; i >= L; i--) for (int a = 0; a < K; a++) for (int b = 0; b < K; b++) {
for (pii NXT : adj[i * K + a]) DP[i][a][b] = min(DP[i][a][b], DP[i + 1][NXT.f % K][b] + NXT.s);
}
for (int i = mid + 1; i <= R; i++) for (int a = 0; a < K; a++) for (int b = 0; b < K; b++) {
for (pii NXT : adj[(i - 1) * K + b]) DP[i][a][NXT.f % K] = min(DP[i][a][NXT.f % K], DP[i - 1][a][b] + NXT.s);
}
vector<int> todo[2]; for (int T : V) { pii P = Q[T];
if (P.f / K <= mid && mid <= P.s / K) for (int i = 0; i < K; i++) {
ans[T] = min(ans[T], DP[P.f / K][P.f % K][i] + DP[P.s / K][i][P.s % K]);
}
else todo[P.f / K > mid].pb(T);
}
divide(L, mid - 1, todo[0]); divide(mid + 1, R, todo[1]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> K >> N >> M >> O; memset(ans, 0x3f, sizeof ans);
for (int i = 0; i < M; i++) { int A, B, T; cin >> A >> B >> T; adj[A].pb({B, T}); }
vector<int> todo; for (int i = 0; i < O; i++) { int A, B; cin >> A >> B; Q.pb({A, B}); todo.pb(i); }
divide(0, N / K, todo); for (int i = 0; i < O; i++) cout << ((ans[i] == INF) ? -1 : ans[i]) << "\n";
}
Compilation message
toll.cpp: In function 'void divide(int, int, std::vector<int>)':
toll.cpp:18:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
18 | if (R < L) return ; int mid = (L + R) / 2;
| ^~
toll.cpp:18:25: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
18 | if (R < L) return ; int mid = (L + R) / 2;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
9708 KB |
Output is correct |
2 |
Correct |
2 ms |
1900 KB |
Output is correct |
3 |
Correct |
2 ms |
2028 KB |
Output is correct |
4 |
Correct |
2 ms |
1900 KB |
Output is correct |
5 |
Correct |
3 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
3 ms |
2176 KB |
Output is correct |
8 |
Correct |
50 ms |
9584 KB |
Output is correct |
9 |
Correct |
49 ms |
9452 KB |
Output is correct |
10 |
Correct |
16 ms |
7148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
7936 KB |
Output is correct |
2 |
Correct |
2 ms |
2048 KB |
Output is correct |
3 |
Correct |
3 ms |
1900 KB |
Output is correct |
4 |
Correct |
2 ms |
1900 KB |
Output is correct |
5 |
Correct |
2 ms |
1900 KB |
Output is correct |
6 |
Correct |
2 ms |
1900 KB |
Output is correct |
7 |
Correct |
5 ms |
2412 KB |
Output is correct |
8 |
Correct |
6 ms |
2412 KB |
Output is correct |
9 |
Correct |
49 ms |
9580 KB |
Output is correct |
10 |
Correct |
132 ms |
8684 KB |
Output is correct |
11 |
Correct |
78 ms |
7988 KB |
Output is correct |
12 |
Correct |
69 ms |
6636 KB |
Output is correct |
13 |
Correct |
143 ms |
7788 KB |
Output is correct |
14 |
Correct |
71 ms |
6124 KB |
Output is correct |
15 |
Correct |
73 ms |
5228 KB |
Output is correct |
16 |
Correct |
69 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
2 |
Correct |
2 ms |
1900 KB |
Output is correct |
3 |
Correct |
2 ms |
1900 KB |
Output is correct |
4 |
Correct |
3 ms |
1900 KB |
Output is correct |
5 |
Correct |
2 ms |
1900 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
3 ms |
2028 KB |
Output is correct |
8 |
Correct |
5 ms |
2028 KB |
Output is correct |
9 |
Correct |
5 ms |
2028 KB |
Output is correct |
10 |
Correct |
50 ms |
9196 KB |
Output is correct |
11 |
Correct |
73 ms |
7404 KB |
Output is correct |
12 |
Correct |
117 ms |
8172 KB |
Output is correct |
13 |
Correct |
135 ms |
8840 KB |
Output is correct |
14 |
Correct |
97 ms |
7532 KB |
Output is correct |
15 |
Correct |
86 ms |
4972 KB |
Output is correct |
16 |
Correct |
69 ms |
5088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
2 |
Correct |
2 ms |
1900 KB |
Output is correct |
3 |
Correct |
2 ms |
1900 KB |
Output is correct |
4 |
Correct |
3 ms |
1900 KB |
Output is correct |
5 |
Correct |
2 ms |
1900 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
3 ms |
2028 KB |
Output is correct |
8 |
Correct |
5 ms |
2028 KB |
Output is correct |
9 |
Correct |
5 ms |
2028 KB |
Output is correct |
10 |
Correct |
50 ms |
9196 KB |
Output is correct |
11 |
Correct |
73 ms |
7404 KB |
Output is correct |
12 |
Correct |
117 ms |
8172 KB |
Output is correct |
13 |
Correct |
135 ms |
8840 KB |
Output is correct |
14 |
Correct |
97 ms |
7532 KB |
Output is correct |
15 |
Correct |
86 ms |
4972 KB |
Output is correct |
16 |
Correct |
69 ms |
5088 KB |
Output is correct |
17 |
Correct |
86 ms |
7620 KB |
Output is correct |
18 |
Correct |
3 ms |
1900 KB |
Output is correct |
19 |
Correct |
3 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1900 KB |
Output is correct |
22 |
Correct |
2 ms |
1900 KB |
Output is correct |
23 |
Correct |
4 ms |
2156 KB |
Output is correct |
24 |
Correct |
5 ms |
2156 KB |
Output is correct |
25 |
Correct |
5 ms |
2156 KB |
Output is correct |
26 |
Correct |
5 ms |
2284 KB |
Output is correct |
27 |
Correct |
53 ms |
9324 KB |
Output is correct |
28 |
Correct |
109 ms |
8300 KB |
Output is correct |
29 |
Correct |
134 ms |
8812 KB |
Output is correct |
30 |
Correct |
106 ms |
7660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
9708 KB |
Output is correct |
2 |
Correct |
2 ms |
1900 KB |
Output is correct |
3 |
Correct |
2 ms |
2028 KB |
Output is correct |
4 |
Correct |
2 ms |
1900 KB |
Output is correct |
5 |
Correct |
3 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2028 KB |
Output is correct |
7 |
Correct |
3 ms |
2176 KB |
Output is correct |
8 |
Correct |
50 ms |
9584 KB |
Output is correct |
9 |
Correct |
49 ms |
9452 KB |
Output is correct |
10 |
Correct |
16 ms |
7148 KB |
Output is correct |
11 |
Correct |
80 ms |
7936 KB |
Output is correct |
12 |
Correct |
2 ms |
2048 KB |
Output is correct |
13 |
Correct |
3 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1900 KB |
Output is correct |
17 |
Correct |
5 ms |
2412 KB |
Output is correct |
18 |
Correct |
6 ms |
2412 KB |
Output is correct |
19 |
Correct |
49 ms |
9580 KB |
Output is correct |
20 |
Correct |
132 ms |
8684 KB |
Output is correct |
21 |
Correct |
78 ms |
7988 KB |
Output is correct |
22 |
Correct |
69 ms |
6636 KB |
Output is correct |
23 |
Correct |
143 ms |
7788 KB |
Output is correct |
24 |
Correct |
71 ms |
6124 KB |
Output is correct |
25 |
Correct |
73 ms |
5228 KB |
Output is correct |
26 |
Correct |
69 ms |
5100 KB |
Output is correct |
27 |
Correct |
3 ms |
1900 KB |
Output is correct |
28 |
Correct |
2 ms |
1900 KB |
Output is correct |
29 |
Correct |
2 ms |
1900 KB |
Output is correct |
30 |
Correct |
3 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
1900 KB |
Output is correct |
32 |
Correct |
3 ms |
2028 KB |
Output is correct |
33 |
Correct |
3 ms |
2028 KB |
Output is correct |
34 |
Correct |
5 ms |
2028 KB |
Output is correct |
35 |
Correct |
5 ms |
2028 KB |
Output is correct |
36 |
Correct |
50 ms |
9196 KB |
Output is correct |
37 |
Correct |
73 ms |
7404 KB |
Output is correct |
38 |
Correct |
117 ms |
8172 KB |
Output is correct |
39 |
Correct |
135 ms |
8840 KB |
Output is correct |
40 |
Correct |
97 ms |
7532 KB |
Output is correct |
41 |
Correct |
86 ms |
4972 KB |
Output is correct |
42 |
Correct |
69 ms |
5088 KB |
Output is correct |
43 |
Correct |
86 ms |
7620 KB |
Output is correct |
44 |
Correct |
3 ms |
1900 KB |
Output is correct |
45 |
Correct |
3 ms |
1900 KB |
Output is correct |
46 |
Correct |
2 ms |
1900 KB |
Output is correct |
47 |
Correct |
2 ms |
1900 KB |
Output is correct |
48 |
Correct |
2 ms |
1900 KB |
Output is correct |
49 |
Correct |
4 ms |
2156 KB |
Output is correct |
50 |
Correct |
5 ms |
2156 KB |
Output is correct |
51 |
Correct |
5 ms |
2156 KB |
Output is correct |
52 |
Correct |
5 ms |
2284 KB |
Output is correct |
53 |
Correct |
53 ms |
9324 KB |
Output is correct |
54 |
Correct |
109 ms |
8300 KB |
Output is correct |
55 |
Correct |
134 ms |
8812 KB |
Output is correct |
56 |
Correct |
106 ms |
7660 KB |
Output is correct |
57 |
Correct |
192 ms |
9556 KB |
Output is correct |
58 |
Correct |
49 ms |
9580 KB |
Output is correct |
59 |
Correct |
98 ms |
8044 KB |
Output is correct |