#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 5e4, B = 100, inf = 1e9;
int k, n, m, o;
int d[N][500], id[N], blk[N], lt[B], rt[B], nxt[N];
vector<array<int, 2>> g[N];
int qry(vector<array<int, 2>> a, int b) {
int id = blk[a[0][0]];
if (id == blk[b]) {
int res = inf;
b -= lt[id];
for (auto [u, c] : a) {
if (d[u][b] < inf) {
res = min(res, c + d[u][b]);
}
}
return res == inf ? -1 : res;
}
fill(nxt + rt[id] + 1, nxt + rt[id] + 1 + k, inf);
for (int i = rt[id] - k + 1, j = 0; i <= rt[id]; ++i, ++j) {
int k = i - lt[id], cur = inf;
for (auto [u, c] : a) {
if (d[u][k] < inf) {
cur = min(cur, c + d[u][k]);
}
}
if (cur != inf) {
for (auto [v, c] : g[i]) {
nxt[v] = min(nxt[v], cur + c);
}
}
}
vector<array<int, 2>> nw;
for (int i = rt[id] + 1; i <= rt[id] + k; ++i) {
if (nxt[i] != inf) {
nw.push_back({i, nxt[i]});
}
}
return !nw.size() ? -1 : qry(nw, b);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n >> m >> o;
for (int i = 0; i < n; ++i) {
blk[i] = i / k / B;
rt[blk[i]] = i;
}
for (int i = n - 1; ~i; --i) {
lt[blk[i]] = i;
}
while (m--) {
int a, b, c; cin >> a >> b >> c;
g[a].push_back({b, c});
}
memset(d, 0x3f, sizeof(d));
for (int i = 0; i < n; ++i) {
int id = blk[i];
d[i][i - lt[id]] = 0;
for (int j = i; blk[j] == id; ++j) {
if (d[i][j - lt[id]] < inf) {
for (auto [k, w] : g[j]) {
if (k <= rt[id]) {
d[i][k - lt[id]] = d[i][j - lt[id]] + w;
}
}
}
}
}
while (o--) {
int a, b; cin >> a >> b;
cout << qry({{a, 0}}, b) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
80 ms |
102308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
93 ms |
102756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
99368 KB |
Output is correct |
2 |
Incorrect |
48 ms |
99472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
99368 KB |
Output is correct |
2 |
Incorrect |
48 ms |
99472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
80 ms |
102308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |