#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 + 5;
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 |
149 ms |
202976 KB |
Output is correct |
2 |
Correct |
3 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3044 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
5 ms |
6996 KB |
Output is correct |
6 |
Correct |
5 ms |
6996 KB |
Output is correct |
7 |
Correct |
5 ms |
7020 KB |
Output is correct |
8 |
Correct |
135 ms |
203212 KB |
Output is correct |
9 |
Correct |
136 ms |
203052 KB |
Output is correct |
10 |
Correct |
104 ms |
201372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
202180 KB |
Output is correct |
2 |
Correct |
2 ms |
3040 KB |
Output is correct |
3 |
Correct |
3 ms |
3040 KB |
Output is correct |
4 |
Correct |
2 ms |
3040 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 |
7232 KB |
Output is correct |
8 |
Correct |
9 ms |
7252 KB |
Output is correct |
9 |
Correct |
139 ms |
203036 KB |
Output is correct |
10 |
Correct |
247 ms |
203296 KB |
Output is correct |
11 |
Correct |
180 ms |
202616 KB |
Output is correct |
12 |
Correct |
184 ms |
202212 KB |
Output is correct |
13 |
Incorrect |
287 ms |
121900 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
3 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
6 ms |
6996 KB |
Output is correct |
7 |
Correct |
7 ms |
6996 KB |
Output is correct |
8 |
Correct |
11 ms |
6956 KB |
Output is correct |
9 |
Correct |
8 ms |
7044 KB |
Output is correct |
10 |
Correct |
127 ms |
202444 KB |
Output is correct |
11 |
Correct |
166 ms |
202424 KB |
Output is correct |
12 |
Correct |
248 ms |
202976 KB |
Output is correct |
13 |
Correct |
240 ms |
203224 KB |
Output is correct |
14 |
Correct |
204 ms |
202524 KB |
Output is correct |
15 |
Incorrect |
165 ms |
120424 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
3 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
6 ms |
6996 KB |
Output is correct |
7 |
Correct |
7 ms |
6996 KB |
Output is correct |
8 |
Correct |
11 ms |
6956 KB |
Output is correct |
9 |
Correct |
8 ms |
7044 KB |
Output is correct |
10 |
Correct |
127 ms |
202444 KB |
Output is correct |
11 |
Correct |
166 ms |
202424 KB |
Output is correct |
12 |
Correct |
248 ms |
202976 KB |
Output is correct |
13 |
Correct |
240 ms |
203224 KB |
Output is correct |
14 |
Correct |
204 ms |
202524 KB |
Output is correct |
15 |
Incorrect |
165 ms |
120424 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
202976 KB |
Output is correct |
2 |
Correct |
3 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3044 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
5 ms |
6996 KB |
Output is correct |
6 |
Correct |
5 ms |
6996 KB |
Output is correct |
7 |
Correct |
5 ms |
7020 KB |
Output is correct |
8 |
Correct |
135 ms |
203212 KB |
Output is correct |
9 |
Correct |
136 ms |
203052 KB |
Output is correct |
10 |
Correct |
104 ms |
201372 KB |
Output is correct |
11 |
Correct |
171 ms |
202180 KB |
Output is correct |
12 |
Correct |
2 ms |
3040 KB |
Output is correct |
13 |
Correct |
3 ms |
3040 KB |
Output is correct |
14 |
Correct |
2 ms |
3040 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 |
7232 KB |
Output is correct |
18 |
Correct |
9 ms |
7252 KB |
Output is correct |
19 |
Correct |
139 ms |
203036 KB |
Output is correct |
20 |
Correct |
247 ms |
203296 KB |
Output is correct |
21 |
Correct |
180 ms |
202616 KB |
Output is correct |
22 |
Correct |
184 ms |
202212 KB |
Output is correct |
23 |
Incorrect |
287 ms |
121900 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |