#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) x.bg(), x.ed()
#define sz(x) (int)x.size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int i = l; i <= r; ++i)
#define FOS(i, r, l) for (int i = r; i >= l; --i)
#define FRN(i, n) for (int i = 0; i < n; ++i)
#define FSN(i, n) for (int i = n - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : x)
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
const int oo = 1e9 + 5;
const int N = 5e4 + 5;
const int D = 205;
const int MAXD = 255;
const int Dxk = 1e3 + 55;
int n, m, k, o, sb[MAXD], eb[MAXD], d[MAXD][Dxk][Dxk], d1[10][10], d2[10][10], ans[N];
pi q[N];
vi q1[MAXD][MAXD];
vpi adj[N];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> k >> n >> m >> o;
FOR(i, 1, m) {
int a, b, t;
cin >> a >> b >> t;
adj[a].pb({b, t});
}
FOR(i, 1, o) {
cin >> q[i].st >> q[i].nd;
q1[q[i].st / k / D][q[i].nd / k / D].pb(i);
ans[i] = oo;
}
memset(sb, -1, sizeof sb);
memset(eb, -1, sizeof eb);
FRN(i, n) {
if (sb[i / k / D] == -1) sb[i / k / D] = i;
eb[i / k / D] = i;
}
FOR(i, 0, (n - 1) / k / D) {
FOR(j, sb[i], eb[i]) {
FOR(z, 0, eb[i] - sb[i]) d[i][j - sb[i]][z] = oo;
d[i][j - sb[i]][j - sb[i]] = 0;
}
FOS(j, eb[i], sb[i])
EACH(z, adj[j]) {
int v = z.st - sb[i], t = z.nd;
if (v + sb[i] > eb[i]) continue;
FOR(x, 0, eb[i] - sb[i]) d[i][j - sb[i]][x] = min(d[i][j - sb[i]][x], d[i][v][x] + t);
}
}
FOR(i, 0, (n - 1) / k / D) {
EACH(j, q1[i][i]) ans[j] = d[i][q[j].st - sb[i]][q[j].nd - sb[i]];
if (i == n / k / D) continue;
FRN(j, 5)
FRN(z, 5)
d1[j][z] = oo;
FRN(j, 5) {
int x = eb[i] - j;
if (x < sb[i]) continue;
EACH(z, adj[x]) {
int v = z.st, t = z.nd;
if (v <= eb[i]) continue;
d1[j][v - sb[i + 1]] = t;
}
}
FOR(j, i + 1, (n - 1) / k / D) {
EACH(z, q1[i][j]) {
int a = q[z].st, b = q[z].nd;
FRN(x1, 5) {
int x = eb[i] - x1;
if (x < sb[i]) continue;
FRN(y1, 5) {
int y = sb[j] + y1;
if (y > eb[j]) continue;
ans[z] = min(1LL * ans[z], 1LL * d[i][a - sb[i]][x - sb[i]] + d1[x1][y1] + d[j][y - sb[j]][b - sb[j]]);
}
}
}
if (j == (n - 1) / k / D) break;
FRN(x1, 5)
FRN(x2, 5)
d2[x1][x2] = oo;
FRN(x1, 5) {
int x = eb[i] - x1;
if (x < sb[i]) continue;
FRN(x2, 5) {
int a = sb[j] + x2;
if (a > eb[j]) continue;
FRN(y2, 5) {
int b = eb[j] - y2;
if (b < sb[j]) continue;
EACH(y1, adj[b]) {
int y = y1.st, t = y1.nd;
if (y <= eb[j]) continue;
d2[x1][y - sb[j + 1]] = min(1LL * d2[x1][y - sb[j + 1]], 1LL * d1[x1][x2] + d[j][x2][b - sb[j]] + t);
}
}
}
}
swap(d1, d2);
}
}
FOR(i, 1, o) cout << (ans[i] < oo? ans[i] : -1) << '\n';
return 0;
}
/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
212556 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
5 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
163 ms |
212564 KB |
Output is correct |
9 |
Correct |
136 ms |
212548 KB |
Output is correct |
10 |
Correct |
104 ms |
211052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
211868 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
1 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
7 ms |
7380 KB |
Output is correct |
8 |
Correct |
8 ms |
7380 KB |
Output is correct |
9 |
Correct |
131 ms |
212300 KB |
Output is correct |
10 |
Correct |
273 ms |
212640 KB |
Output is correct |
11 |
Correct |
198 ms |
211952 KB |
Output is correct |
12 |
Correct |
210 ms |
211580 KB |
Output is correct |
13 |
Correct |
326 ms |
129812 KB |
Output is correct |
14 |
Correct |
176 ms |
129252 KB |
Output is correct |
15 |
Correct |
192 ms |
128792 KB |
Output is correct |
16 |
Correct |
233 ms |
128696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3080 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
3 ms |
3028 KB |
Output is correct |
6 |
Correct |
5 ms |
7124 KB |
Output is correct |
7 |
Correct |
7 ms |
7124 KB |
Output is correct |
8 |
Correct |
12 ms |
7252 KB |
Output is correct |
9 |
Correct |
10 ms |
7252 KB |
Output is correct |
10 |
Correct |
137 ms |
212164 KB |
Output is correct |
11 |
Correct |
168 ms |
211692 KB |
Output is correct |
12 |
Correct |
256 ms |
212484 KB |
Output is correct |
13 |
Correct |
248 ms |
212692 KB |
Output is correct |
14 |
Correct |
222 ms |
212040 KB |
Output is correct |
15 |
Correct |
192 ms |
128232 KB |
Output is correct |
16 |
Correct |
172 ms |
128704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3080 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
3 ms |
3028 KB |
Output is correct |
6 |
Correct |
5 ms |
7124 KB |
Output is correct |
7 |
Correct |
7 ms |
7124 KB |
Output is correct |
8 |
Correct |
12 ms |
7252 KB |
Output is correct |
9 |
Correct |
10 ms |
7252 KB |
Output is correct |
10 |
Correct |
137 ms |
212164 KB |
Output is correct |
11 |
Correct |
168 ms |
211692 KB |
Output is correct |
12 |
Correct |
256 ms |
212484 KB |
Output is correct |
13 |
Correct |
248 ms |
212692 KB |
Output is correct |
14 |
Correct |
222 ms |
212040 KB |
Output is correct |
15 |
Correct |
192 ms |
128232 KB |
Output is correct |
16 |
Correct |
172 ms |
128704 KB |
Output is correct |
17 |
Correct |
175 ms |
212216 KB |
Output is correct |
18 |
Correct |
2 ms |
3044 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
2 ms |
3040 KB |
Output is correct |
21 |
Correct |
2 ms |
3028 KB |
Output is correct |
22 |
Correct |
3 ms |
3028 KB |
Output is correct |
23 |
Correct |
5 ms |
7252 KB |
Output is correct |
24 |
Correct |
6 ms |
7252 KB |
Output is correct |
25 |
Correct |
10 ms |
7408 KB |
Output is correct |
26 |
Correct |
8 ms |
7280 KB |
Output is correct |
27 |
Correct |
146 ms |
212612 KB |
Output is correct |
28 |
Correct |
249 ms |
212764 KB |
Output is correct |
29 |
Correct |
261 ms |
213068 KB |
Output is correct |
30 |
Correct |
229 ms |
212448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
212556 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
5 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
163 ms |
212564 KB |
Output is correct |
9 |
Correct |
136 ms |
212548 KB |
Output is correct |
10 |
Correct |
104 ms |
211052 KB |
Output is correct |
11 |
Correct |
213 ms |
211868 KB |
Output is correct |
12 |
Correct |
2 ms |
3028 KB |
Output is correct |
13 |
Correct |
2 ms |
3028 KB |
Output is correct |
14 |
Correct |
1 ms |
3028 KB |
Output is correct |
15 |
Correct |
2 ms |
3028 KB |
Output is correct |
16 |
Correct |
2 ms |
3028 KB |
Output is correct |
17 |
Correct |
7 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
7380 KB |
Output is correct |
19 |
Correct |
131 ms |
212300 KB |
Output is correct |
20 |
Correct |
273 ms |
212640 KB |
Output is correct |
21 |
Correct |
198 ms |
211952 KB |
Output is correct |
22 |
Correct |
210 ms |
211580 KB |
Output is correct |
23 |
Correct |
326 ms |
129812 KB |
Output is correct |
24 |
Correct |
176 ms |
129252 KB |
Output is correct |
25 |
Correct |
192 ms |
128792 KB |
Output is correct |
26 |
Correct |
233 ms |
128696 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
3028 KB |
Output is correct |
29 |
Correct |
2 ms |
3080 KB |
Output is correct |
30 |
Correct |
2 ms |
3028 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
5 ms |
7124 KB |
Output is correct |
33 |
Correct |
7 ms |
7124 KB |
Output is correct |
34 |
Correct |
12 ms |
7252 KB |
Output is correct |
35 |
Correct |
10 ms |
7252 KB |
Output is correct |
36 |
Correct |
137 ms |
212164 KB |
Output is correct |
37 |
Correct |
168 ms |
211692 KB |
Output is correct |
38 |
Correct |
256 ms |
212484 KB |
Output is correct |
39 |
Correct |
248 ms |
212692 KB |
Output is correct |
40 |
Correct |
222 ms |
212040 KB |
Output is correct |
41 |
Correct |
192 ms |
128232 KB |
Output is correct |
42 |
Correct |
172 ms |
128704 KB |
Output is correct |
43 |
Correct |
175 ms |
212216 KB |
Output is correct |
44 |
Correct |
2 ms |
3044 KB |
Output is correct |
45 |
Correct |
2 ms |
3028 KB |
Output is correct |
46 |
Correct |
2 ms |
3040 KB |
Output is correct |
47 |
Correct |
2 ms |
3028 KB |
Output is correct |
48 |
Correct |
3 ms |
3028 KB |
Output is correct |
49 |
Correct |
5 ms |
7252 KB |
Output is correct |
50 |
Correct |
6 ms |
7252 KB |
Output is correct |
51 |
Correct |
10 ms |
7408 KB |
Output is correct |
52 |
Correct |
8 ms |
7280 KB |
Output is correct |
53 |
Correct |
146 ms |
212612 KB |
Output is correct |
54 |
Correct |
249 ms |
212764 KB |
Output is correct |
55 |
Correct |
261 ms |
213068 KB |
Output is correct |
56 |
Correct |
229 ms |
212448 KB |
Output is correct |
57 |
Correct |
345 ms |
213000 KB |
Output is correct |
58 |
Correct |
138 ms |
212920 KB |
Output is correct |
59 |
Correct |
182 ms |
212584 KB |
Output is correct |