#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 998244353, maxn = 5e4 + 5, maxk = 5, maxlg = 18, inf = 1e9 + 5;
struct Query{
pp a, b;
int id;
};
int grp[maxn][maxk][maxk], ans[maxn], val[maxn][maxk][maxk], k;
void slv(int lx, int rx, vector<Query> v){
if(lx >= rx || v.empty()){
for(auto c: v){
ans[c.id] = -1;
}
return;
}
// cout << lx << ' ' << rx << ": ";
// for(auto c: v) cout << c.id << ' '; cout << endl;
int mid = (lx + rx) >> 1;
rep(i,0,k){
rep(j,0,k){
if(i != j) val[mid][i][j] = inf;
else val[mid][i][j] = 0;
}
}
per(i,mid - 1, lx){
rep(f,0,k){
rep(e,0,k){
val[i][f][e] = inf;
rep(x,0,k){
val[i][f][e] = min(val[i][f][e], val[i + 1][x][e] + grp[i][f][x]);
}
}
}
}
rep(i,mid + 1, rx + 1){
rep(f,0,k){
rep(e,0,k){
val[i][f][e] = inf;
rep(x,0,k){
val[i][f][e] = min(val[i][f][e], val[i - 1][f][x] + grp[i-1][x][e]);
}
}
}
}
vector<Query> l, r;
for(auto c: v){
if(c.b.ff < mid){
l.pb(c);
}else if(c.a.ff > mid){
r.pb(c);
}else{
ans[c.id] = inf;
rep(i,0,k){
ans[c.id] = min(ans[c.id], val[c.a.ff][c.a.ss][i] + val[c.b.ff][i][c.b.ss]);
}
if(ans[c.id] == inf) ans[c.id] = -1;
}
}
slv(lx, mid - 1, l), slv(mid + 1, rx, r);
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n, m, q; cin >> k >> n >> m >> q;
rep(i,0,n){
rep(j,0,k){
rep(t,0,k){
grp[i][j][t] = inf;
}
}
}
rep(i,0,m){
int u, v, w; cin >> u >> v >> w;
grp[u/k][u%k][v%k] = w;
}
vector<Query> qs;
rep(i,0,q){
int u, v; cin >> u >> v;
Query qq;
qq.a = {u/k, u%k}, qq.b = {v/k, v%k};
qq.id = i;
qs.pb(qq);
}
// for(auto c: qs) cout << c.a.ff << ' ' << c.a.ss << ' ' << c.b.ff << ' ' << c.b.ss << '\n'; cout << endl;
slv(0, n/k, qs);
rep(i,0,q) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
11660 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
26 ms |
11648 KB |
Output is correct |
9 |
Correct |
24 ms |
11652 KB |
Output is correct |
10 |
Correct |
12 ms |
10888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9988 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
4 ms |
1416 KB |
Output is correct |
8 |
Correct |
4 ms |
1416 KB |
Output is correct |
9 |
Correct |
28 ms |
11776 KB |
Output is correct |
10 |
Correct |
51 ms |
9860 KB |
Output is correct |
11 |
Correct |
35 ms |
10116 KB |
Output is correct |
12 |
Correct |
31 ms |
8968 KB |
Output is correct |
13 |
Correct |
44 ms |
6468 KB |
Output is correct |
14 |
Correct |
28 ms |
5956 KB |
Output is correct |
15 |
Correct |
24 ms |
5292 KB |
Output is correct |
16 |
Correct |
29 ms |
5296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
23 ms |
10820 KB |
Output is correct |
11 |
Correct |
41 ms |
9152 KB |
Output is correct |
12 |
Correct |
45 ms |
9024 KB |
Output is correct |
13 |
Correct |
49 ms |
9164 KB |
Output is correct |
14 |
Correct |
36 ms |
8648 KB |
Output is correct |
15 |
Correct |
30 ms |
5060 KB |
Output is correct |
16 |
Correct |
26 ms |
5048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
23 ms |
10820 KB |
Output is correct |
11 |
Correct |
41 ms |
9152 KB |
Output is correct |
12 |
Correct |
45 ms |
9024 KB |
Output is correct |
13 |
Correct |
49 ms |
9164 KB |
Output is correct |
14 |
Correct |
36 ms |
8648 KB |
Output is correct |
15 |
Correct |
30 ms |
5060 KB |
Output is correct |
16 |
Correct |
26 ms |
5048 KB |
Output is correct |
17 |
Correct |
47 ms |
9412 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
3 ms |
716 KB |
Output is correct |
27 |
Correct |
22 ms |
11084 KB |
Output is correct |
28 |
Correct |
49 ms |
9204 KB |
Output is correct |
29 |
Correct |
48 ms |
9524 KB |
Output is correct |
30 |
Correct |
42 ms |
8888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
11660 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
26 ms |
11648 KB |
Output is correct |
9 |
Correct |
24 ms |
11652 KB |
Output is correct |
10 |
Correct |
12 ms |
10888 KB |
Output is correct |
11 |
Correct |
33 ms |
9988 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1416 KB |
Output is correct |
18 |
Correct |
4 ms |
1416 KB |
Output is correct |
19 |
Correct |
28 ms |
11776 KB |
Output is correct |
20 |
Correct |
51 ms |
9860 KB |
Output is correct |
21 |
Correct |
35 ms |
10116 KB |
Output is correct |
22 |
Correct |
31 ms |
8968 KB |
Output is correct |
23 |
Correct |
44 ms |
6468 KB |
Output is correct |
24 |
Correct |
28 ms |
5956 KB |
Output is correct |
25 |
Correct |
24 ms |
5292 KB |
Output is correct |
26 |
Correct |
29 ms |
5296 KB |
Output is correct |
27 |
Correct |
0 ms |
216 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
0 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
23 ms |
10820 KB |
Output is correct |
37 |
Correct |
41 ms |
9152 KB |
Output is correct |
38 |
Correct |
45 ms |
9024 KB |
Output is correct |
39 |
Correct |
49 ms |
9164 KB |
Output is correct |
40 |
Correct |
36 ms |
8648 KB |
Output is correct |
41 |
Correct |
30 ms |
5060 KB |
Output is correct |
42 |
Correct |
26 ms |
5048 KB |
Output is correct |
43 |
Correct |
47 ms |
9412 KB |
Output is correct |
44 |
Correct |
0 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
2 ms |
716 KB |
Output is correct |
50 |
Correct |
2 ms |
716 KB |
Output is correct |
51 |
Correct |
2 ms |
716 KB |
Output is correct |
52 |
Correct |
3 ms |
716 KB |
Output is correct |
53 |
Correct |
22 ms |
11084 KB |
Output is correct |
54 |
Correct |
49 ms |
9204 KB |
Output is correct |
55 |
Correct |
48 ms |
9524 KB |
Output is correct |
56 |
Correct |
42 ms |
8888 KB |
Output is correct |
57 |
Correct |
89 ms |
10364 KB |
Output is correct |
58 |
Correct |
28 ms |
11664 KB |
Output is correct |
59 |
Correct |
42 ms |
10116 KB |
Output is correct |