#include <bits/stdc++.h>
#include <cassert>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define ROF(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k, n, m, o;
cin >> k >> n >> m >> o;
vector<vector<pair<int, int>>> G(n), odwr(n);
REP(i, m) {
int a, b, t;
cin >> a >> b >> t;
G[a].push_back({b, t});
odwr[b].push_back({a, t});
}
constexpr int inf = 1e9;
vector<int> a(o), b(o);
vector<int> Q, ans(o, inf);
REP(i, o) {
cin >> a[i] >> b[i];
Q.push_back(i);
}
vector lef(k, vector(n, inf)), rig(k, vector(n, inf));
function<void(int, int, vector<int>)> solve = [&](int l, int r, vector<int> v) {
if (v.size() == 0) return;
if (l == r) {
for (auto e : v) ans[e] = 0;
return;
}
int mid = (l+r)/2;
REP(j, k) {
lef[j][k*mid+j] = rig[j][k*mid+j] = 0;
ROF(i, mid*k-1, l*k) {
lef[j][i] = inf;
for (auto w : G[i]) {
lef[j][i] = min(lef[j][i], w.se+lef[j][w.fi]);
}
}
FOR(i, (mid+1)*k, min(r*k+k-1, n-1)) {
rig[j][i] = inf;
for (auto w : odwr[i]) {
rig[j][i] = min(rig[j][i], w.se+rig[j][w.fi]);
}
}
}
vector<int> todo[2];
for (auto e : v) {
int A = a[e], B = b[e];
assert(A >= l*k && r <= min(r*k+k-1, n-1));
if (A/k <= mid && mid < B/k) {
REP(i, k) {
ans[e] = min(ans[e], lef[i][A]+rig[i][B]);
}
continue;
}
todo[B/k > mid].push_back(e);
}
solve(l, mid, todo[0]); solve(mid+1, r, todo[1]);
};
solve(0, (n-1)/k, Q);
REP(i, o) cout << (ans[i]==inf?-1:ans[i]) << '\n';
return 0;
};
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6672 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
18 ms |
6600 KB |
Output is correct |
9 |
Correct |
17 ms |
6416 KB |
Output is correct |
10 |
Correct |
4 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
7032 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6672 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
18 ms |
6600 KB |
Output is correct |
9 |
Correct |
17 ms |
6416 KB |
Output is correct |
10 |
Correct |
4 ms |
3404 KB |
Output is correct |
11 |
Correct |
29 ms |
7032 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |