#include <iostream>
#include <array>
#include <cassert>
#include <algorithm>
#include <string>
#include <bitset>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
#include <bits/stdc++.h>
#define cerr if(false)cerr
#endif
using namespace std;
using ll = long long;
const int N = 1e6 + 66;
const int K = 7;
using mat = array<array<ll, K>, K>;
mat g[N];
const ll inf = 1e18 + 123;
mat compute(mat a, mat b) {
mat c; for (int i = 0 ; i < K ; ++ i) for (int j = 0 ; j < K ; ++ j) c[i][j] = (i == j ? 0 : inf);
for (int i = 0 ; i < K ; ++ i) for (int j = 0 ; j < K ; ++ j) for (int k = 0 ; k < K ; ++ k) {
c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
}
return c;
}
mat compute(int l, int r) {
mat cur; for (int i = 0 ; i < K ; ++ i) for (int j = 0 ; j < K ; ++ j) cur[i][j] = (i == j ? 0 : inf);
for (int i = l ; i <= r ; ++ i) {
cur = compute(cur, g[i]);
}
return cur;
}
mat mid;
int qa[N], qb[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n, m, q;
cin >> k >> n >> m >> q;
for (int b = 0 ; b < n ; ++ b) {
for (int i = 0 ; i < k ; ++ i) {
for (int j = 0 ; j < k ; ++ j) {
g[b][i][j] = (i == j ? 0 : inf);
}
}
}
while (m--) {
int a, b, c;
cin >> a >> b >> c;
int ba = a / k, bb = b / k;
assert(ba + 1 == bb);
int ia = a % k, ib = b % k;
g[ba][ia][ib] = c;
}
for (int i = 0 ; i < q ; ++ i) {
cin >> qa[i] >> qb[i];
int ba = qa[i] / k, bb = qb[i] / k;
int ia = qa[i] % k, ib = qb[i] % k;
ll ans;
if (ba == bb) {
ans = inf;
} else if (ba == bb - 1) {
ans = g[ba][ia][ib];
} else {
ans = inf;
int l = ba + 1, r = bb - 1;
mid = compute(l, r);
for (int s = 0 ; s < k ; ++ s) {
for (int t = 0 ; t < k ; ++ t) {
ans = min(ans, mid[s][t] + g[ba][ia][s] + g[bb - 1][t][ib]);
}
}
}
if (ans >= inf) ans = -1;
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3028 ms |
20416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3056 ms |
21108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3028 ms |
20416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |