// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int N = 50001;
int k, n, m, o;
vector<pair<pair<int, int>, int> > qq;
vector<pair<pair<int, int>, int> > g[N];
int ans[N];
ll dist[N][5][5];
void kek(int l, int r, vector<pair<pair<int, int>, int> >& curq) {
vector<pair<pair<int, int>, int> > tol, tor, mid;
int m = (l + r) / 2;
for(int i = l; i <= r; i++)
for(int a = 0; a < k; a++)
for(int b = 0; b < k; b++)
dist[i][a][b] = 1e18;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
dist[m][i][j] = (i == j ? 0 : 1e18);
for(int i = m; i + 1 <= r; i++) {
for(auto& edge : g[i])
for(int st = 0; st < k; st++)
umin(dist[i + 1][st][edge.fi.se], dist[i][st][edge.fi.fi] + edge.se);
}
for(int i = m; i - 1 >= l; i--) {
for(auto& edge : g[i - 1])
for(int fs = 0; fs < k; fs++)
umin(dist[i - 1][fs][edge.fi.fi], dist[i][fs][edge.fi.se] + edge.se);
}
for(auto& x : curq)
if(x.fi.se / k < m) tol.pb(x);
else if(x.fi.fi / k > m) tor.pb(x);
else {
ll res = 1e18;
for(int inter = 0; inter < k; inter++)
umin(res, dist[x.fi.fi / k][inter][x.fi.fi % k] + dist[x.fi.se / k][inter][x.fi.se % k]);
ans[x.se] = (res == 1e18 ? -1 : res);
}
if(l == r) return;
if(!tol.empty()) kek(l, m - 1, tol);
if(!tor.empty()) kek(m + 1, r, tor);
}
void solve() {
cin >> k >> n >> m >> o;
for(int i = 0; i < m; i++) {
int a, b, t;
cin >> a >> b >> t;
g[a / k].pb(mp(mp(a % k, b % k), t));
}
for(int i = 0; i < o; i++) {
ans[i] = -1;
int a, b;
cin >> a >> b;
qq.pb(mp(mp(a, b), i));
}
kek(0, N / k, qq);
for(int i = 0; i < o; i++) cout << ans[i] << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14076 KB |
Output is correct |
2 |
Correct |
5 ms |
11220 KB |
Output is correct |
3 |
Correct |
5 ms |
11240 KB |
Output is correct |
4 |
Correct |
5 ms |
11236 KB |
Output is correct |
5 |
Correct |
6 ms |
11408 KB |
Output is correct |
6 |
Correct |
6 ms |
11384 KB |
Output is correct |
7 |
Correct |
6 ms |
11384 KB |
Output is correct |
8 |
Correct |
27 ms |
14048 KB |
Output is correct |
9 |
Correct |
25 ms |
13892 KB |
Output is correct |
10 |
Correct |
9 ms |
11668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9984 KB |
Output is correct |
2 |
Correct |
5 ms |
11220 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
2 ms |
4692 KB |
Output is correct |
5 |
Correct |
3 ms |
3924 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
8 ms |
12308 KB |
Output is correct |
8 |
Correct |
7 ms |
7504 KB |
Output is correct |
9 |
Correct |
23 ms |
14000 KB |
Output is correct |
10 |
Correct |
55 ms |
10264 KB |
Output is correct |
11 |
Correct |
38 ms |
10100 KB |
Output is correct |
12 |
Correct |
27 ms |
7944 KB |
Output is correct |
13 |
Correct |
54 ms |
8560 KB |
Output is correct |
14 |
Correct |
36 ms |
8616 KB |
Output is correct |
15 |
Correct |
28 ms |
6140 KB |
Output is correct |
16 |
Correct |
29 ms |
6204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11220 KB |
Output is correct |
2 |
Correct |
3 ms |
6416 KB |
Output is correct |
3 |
Correct |
3 ms |
4692 KB |
Output is correct |
4 |
Correct |
2 ms |
3944 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
6 ms |
11348 KB |
Output is correct |
7 |
Correct |
5 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
3632 KB |
Output is correct |
9 |
Correct |
4 ms |
4004 KB |
Output is correct |
10 |
Correct |
25 ms |
13624 KB |
Output is correct |
11 |
Correct |
38 ms |
9592 KB |
Output is correct |
12 |
Correct |
54 ms |
9824 KB |
Output is correct |
13 |
Correct |
54 ms |
10876 KB |
Output is correct |
14 |
Correct |
45 ms |
8664 KB |
Output is correct |
15 |
Correct |
33 ms |
6048 KB |
Output is correct |
16 |
Correct |
35 ms |
6072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11220 KB |
Output is correct |
2 |
Correct |
3 ms |
6416 KB |
Output is correct |
3 |
Correct |
3 ms |
4692 KB |
Output is correct |
4 |
Correct |
2 ms |
3944 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
6 ms |
11348 KB |
Output is correct |
7 |
Correct |
5 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
3632 KB |
Output is correct |
9 |
Correct |
4 ms |
4004 KB |
Output is correct |
10 |
Correct |
25 ms |
13624 KB |
Output is correct |
11 |
Correct |
38 ms |
9592 KB |
Output is correct |
12 |
Correct |
54 ms |
9824 KB |
Output is correct |
13 |
Correct |
54 ms |
10876 KB |
Output is correct |
14 |
Correct |
45 ms |
8664 KB |
Output is correct |
15 |
Correct |
33 ms |
6048 KB |
Output is correct |
16 |
Correct |
35 ms |
6072 KB |
Output is correct |
17 |
Correct |
41 ms |
9692 KB |
Output is correct |
18 |
Correct |
5 ms |
11240 KB |
Output is correct |
19 |
Correct |
3 ms |
6356 KB |
Output is correct |
20 |
Correct |
3 ms |
4712 KB |
Output is correct |
21 |
Correct |
3 ms |
3864 KB |
Output is correct |
22 |
Correct |
2 ms |
3428 KB |
Output is correct |
23 |
Correct |
6 ms |
11604 KB |
Output is correct |
24 |
Correct |
5 ms |
6740 KB |
Output is correct |
25 |
Correct |
5 ms |
3864 KB |
Output is correct |
26 |
Correct |
4 ms |
4308 KB |
Output is correct |
27 |
Correct |
28 ms |
13772 KB |
Output is correct |
28 |
Correct |
54 ms |
10080 KB |
Output is correct |
29 |
Correct |
57 ms |
11024 KB |
Output is correct |
30 |
Correct |
45 ms |
8772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14076 KB |
Output is correct |
2 |
Correct |
5 ms |
11220 KB |
Output is correct |
3 |
Correct |
5 ms |
11240 KB |
Output is correct |
4 |
Correct |
5 ms |
11236 KB |
Output is correct |
5 |
Correct |
6 ms |
11408 KB |
Output is correct |
6 |
Correct |
6 ms |
11384 KB |
Output is correct |
7 |
Correct |
6 ms |
11384 KB |
Output is correct |
8 |
Correct |
27 ms |
14048 KB |
Output is correct |
9 |
Correct |
25 ms |
13892 KB |
Output is correct |
10 |
Correct |
9 ms |
11668 KB |
Output is correct |
11 |
Correct |
35 ms |
9984 KB |
Output is correct |
12 |
Correct |
5 ms |
11220 KB |
Output is correct |
13 |
Correct |
3 ms |
6356 KB |
Output is correct |
14 |
Correct |
2 ms |
4692 KB |
Output is correct |
15 |
Correct |
3 ms |
3924 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
8 ms |
12308 KB |
Output is correct |
18 |
Correct |
7 ms |
7504 KB |
Output is correct |
19 |
Correct |
23 ms |
14000 KB |
Output is correct |
20 |
Correct |
55 ms |
10264 KB |
Output is correct |
21 |
Correct |
38 ms |
10100 KB |
Output is correct |
22 |
Correct |
27 ms |
7944 KB |
Output is correct |
23 |
Correct |
54 ms |
8560 KB |
Output is correct |
24 |
Correct |
36 ms |
8616 KB |
Output is correct |
25 |
Correct |
28 ms |
6140 KB |
Output is correct |
26 |
Correct |
29 ms |
6204 KB |
Output is correct |
27 |
Correct |
5 ms |
11220 KB |
Output is correct |
28 |
Correct |
3 ms |
6416 KB |
Output is correct |
29 |
Correct |
3 ms |
4692 KB |
Output is correct |
30 |
Correct |
2 ms |
3944 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
6 ms |
11348 KB |
Output is correct |
33 |
Correct |
5 ms |
6484 KB |
Output is correct |
34 |
Correct |
4 ms |
3632 KB |
Output is correct |
35 |
Correct |
4 ms |
4004 KB |
Output is correct |
36 |
Correct |
25 ms |
13624 KB |
Output is correct |
37 |
Correct |
38 ms |
9592 KB |
Output is correct |
38 |
Correct |
54 ms |
9824 KB |
Output is correct |
39 |
Correct |
54 ms |
10876 KB |
Output is correct |
40 |
Correct |
45 ms |
8664 KB |
Output is correct |
41 |
Correct |
33 ms |
6048 KB |
Output is correct |
42 |
Correct |
35 ms |
6072 KB |
Output is correct |
43 |
Correct |
41 ms |
9692 KB |
Output is correct |
44 |
Correct |
5 ms |
11240 KB |
Output is correct |
45 |
Correct |
3 ms |
6356 KB |
Output is correct |
46 |
Correct |
3 ms |
4712 KB |
Output is correct |
47 |
Correct |
3 ms |
3864 KB |
Output is correct |
48 |
Correct |
2 ms |
3428 KB |
Output is correct |
49 |
Correct |
6 ms |
11604 KB |
Output is correct |
50 |
Correct |
5 ms |
6740 KB |
Output is correct |
51 |
Correct |
5 ms |
3864 KB |
Output is correct |
52 |
Correct |
4 ms |
4308 KB |
Output is correct |
53 |
Correct |
28 ms |
13772 KB |
Output is correct |
54 |
Correct |
54 ms |
10080 KB |
Output is correct |
55 |
Correct |
57 ms |
11024 KB |
Output is correct |
56 |
Correct |
45 ms |
8772 KB |
Output is correct |
57 |
Correct |
89 ms |
10432 KB |
Output is correct |
58 |
Correct |
27 ms |
14000 KB |
Output is correct |
59 |
Correct |
46 ms |
10136 KB |
Output is correct |