#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, k, q;
cin >> k >> n >> m >> q;
vector queries(n, vector<pair<int, int>>());
vector<int> ans(q);
vector<vector<pair<int, int>>> out(n);
while (m--) {
int u, v, t;
cin >> u >> v >> t;
out[u].emplace_back(v, t);
}
for (int i = 0; i < q; i++) {
int u, v;
cin >> u >> v;
queries[u].emplace_back(v, i);
}
vector dist(k, vector(n, inf));
for (int i = n - 1; i / k == (n - 1) / k; i--) dist[i % k][i] = 0;
for (int i = (n - 1) / k; i >= 0; i--) {
int left = k * i, right = min(n - 1, k * (i + 1) - 1);
vector new_dist(k, vector(n, inf));
for (int u = left; u <= right; u++) {
new_dist[u % k][u] = 0;
for (auto [v, t]: out[u]) {
for (int j = u; j < n; j++) {
new_dist[u % k][j] = min(new_dist[u % k][j], dist[v % k][j] + t);
}
}
for (auto [v, j]: queries[u]) {
ans[j] = new_dist[u % k][v];
}
}
dist = new_dist;
}
for (int i = 0; i < q; i++) cout << (ans[i] == inf ? -1 : ans[i]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3021 ms |
5272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3024 ms |
6076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
348 KB |
Output is correct |
8 |
Correct |
25 ms |
604 KB |
Output is correct |
9 |
Correct |
19 ms |
600 KB |
Output is correct |
10 |
Execution timed out |
3052 ms |
5528 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
348 KB |
Output is correct |
8 |
Correct |
25 ms |
604 KB |
Output is correct |
9 |
Correct |
19 ms |
600 KB |
Output is correct |
10 |
Execution timed out |
3052 ms |
5528 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3021 ms |
5272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |