#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
#define lid (id*2)
#define rid (id*2+1)
#define mid ((l+r)/2)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll K = 5;
const ll N = 5e4 + 50;
const ll inf = 1e9;
ll n, m, k, q, cnt;
vector<pii> adj[N];
struct dt{
int dist[K][K];
dt(int x = inf, int y = K){
for(int i = 0; i < y; i++){
for(int j = 0; j < y; j++){
dist[i][j] = inf;
}
}
}
};
dt merg(dt l, dt r){
dt out;
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int o = 0; o < k; o++){
out.dist[i][j] = min(out.dist[i][j], l.dist[i][o] + r.dist[o][j]);
}
}
}
return out;
}
dt seg[N*4];
void build(int l = 0, int r = cnt, int id = 1){
if(l+1 == r){
for(int i = l*k; i < l*k+k; i++){
for(auto [j, w]: adj[i]){
seg[id].dist[i%k][j%k] = min(seg[id].dist[i%k][j%k], w);
}
}
return;
}
build(l, mid, lid);
build(mid, r, rid);
seg[id] = merg(seg[lid], seg[rid]);
}
dt get(int ql, int qr, int l = 0, int r = cnt, int id = 1){
if(ql <= l && r <= qr) return seg[id];
if(qr <= mid) return get(ql, qr, l, mid, lid);
if(mid <= ql) return get(ql, qr, mid, r, rid);
return merg(get(ql, qr, l, mid, lid), get(ql, qr, mid, r, rid));
}
int main(){
fast_io;
cin >> k >> n >> m >> q;
while(n % k != 0) n++;
cnt = n/k;
int u, v, w;
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
adj[u].pb(Mp(v, w));
}
build();
while(q--){
cin >> u >> v;
if(u/k == v/k){
cout << -1 << endl;
continue;
}
ll out = get(u/k, v/k).dist[u%k][v%k];
if(out < inf) cout << out << endl;
else cout << -1 << endl;
}
}
Compilation message
toll.cpp: In function 'void build(int, int, int)':
toll.cpp:66:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [j, w]: adj[i]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23636 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21080 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21084 KB |
Output is correct |
8 |
Correct |
31 ms |
23704 KB |
Output is correct |
9 |
Correct |
26 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
21356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
24384 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
6 ms |
21340 KB |
Output is correct |
8 |
Correct |
7 ms |
21340 KB |
Output is correct |
9 |
Correct |
20 ms |
23568 KB |
Output is correct |
10 |
Correct |
46 ms |
26104 KB |
Output is correct |
11 |
Correct |
30 ms |
24404 KB |
Output is correct |
12 |
Correct |
26 ms |
23896 KB |
Output is correct |
13 |
Correct |
41 ms |
26196 KB |
Output is correct |
14 |
Correct |
27 ms |
24232 KB |
Output is correct |
15 |
Correct |
23 ms |
23596 KB |
Output is correct |
16 |
Correct |
25 ms |
23632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21080 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21176 KB |
Output is correct |
8 |
Correct |
5 ms |
21312 KB |
Output is correct |
9 |
Correct |
4 ms |
21176 KB |
Output is correct |
10 |
Correct |
16 ms |
23384 KB |
Output is correct |
11 |
Correct |
25 ms |
24096 KB |
Output is correct |
12 |
Correct |
36 ms |
25748 KB |
Output is correct |
13 |
Correct |
45 ms |
26704 KB |
Output is correct |
14 |
Correct |
30 ms |
24912 KB |
Output is correct |
15 |
Correct |
22 ms |
23632 KB |
Output is correct |
16 |
Correct |
21 ms |
23644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21080 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21176 KB |
Output is correct |
8 |
Correct |
5 ms |
21312 KB |
Output is correct |
9 |
Correct |
4 ms |
21176 KB |
Output is correct |
10 |
Correct |
16 ms |
23384 KB |
Output is correct |
11 |
Correct |
25 ms |
24096 KB |
Output is correct |
12 |
Correct |
36 ms |
25748 KB |
Output is correct |
13 |
Correct |
45 ms |
26704 KB |
Output is correct |
14 |
Correct |
30 ms |
24912 KB |
Output is correct |
15 |
Correct |
22 ms |
23632 KB |
Output is correct |
16 |
Correct |
21 ms |
23644 KB |
Output is correct |
17 |
Correct |
27 ms |
24152 KB |
Output is correct |
18 |
Correct |
3 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21228 KB |
Output is correct |
20 |
Correct |
3 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
3 ms |
21340 KB |
Output is correct |
23 |
Correct |
5 ms |
21240 KB |
Output is correct |
24 |
Correct |
5 ms |
21084 KB |
Output is correct |
25 |
Correct |
6 ms |
21336 KB |
Output is correct |
26 |
Correct |
6 ms |
21340 KB |
Output is correct |
27 |
Correct |
19 ms |
23632 KB |
Output is correct |
28 |
Correct |
38 ms |
25788 KB |
Output is correct |
29 |
Correct |
42 ms |
26200 KB |
Output is correct |
30 |
Correct |
32 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23636 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21080 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21084 KB |
Output is correct |
8 |
Correct |
31 ms |
23704 KB |
Output is correct |
9 |
Correct |
26 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
21356 KB |
Output is correct |
11 |
Correct |
29 ms |
24384 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21084 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
6 ms |
21340 KB |
Output is correct |
18 |
Correct |
7 ms |
21340 KB |
Output is correct |
19 |
Correct |
20 ms |
23568 KB |
Output is correct |
20 |
Correct |
46 ms |
26104 KB |
Output is correct |
21 |
Correct |
30 ms |
24404 KB |
Output is correct |
22 |
Correct |
26 ms |
23896 KB |
Output is correct |
23 |
Correct |
41 ms |
26196 KB |
Output is correct |
24 |
Correct |
27 ms |
24232 KB |
Output is correct |
25 |
Correct |
23 ms |
23596 KB |
Output is correct |
26 |
Correct |
25 ms |
23632 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
3 ms |
21080 KB |
Output is correct |
29 |
Correct |
3 ms |
21084 KB |
Output is correct |
30 |
Correct |
3 ms |
21084 KB |
Output is correct |
31 |
Correct |
4 ms |
21084 KB |
Output is correct |
32 |
Correct |
4 ms |
21084 KB |
Output is correct |
33 |
Correct |
4 ms |
21176 KB |
Output is correct |
34 |
Correct |
5 ms |
21312 KB |
Output is correct |
35 |
Correct |
4 ms |
21176 KB |
Output is correct |
36 |
Correct |
16 ms |
23384 KB |
Output is correct |
37 |
Correct |
25 ms |
24096 KB |
Output is correct |
38 |
Correct |
36 ms |
25748 KB |
Output is correct |
39 |
Correct |
45 ms |
26704 KB |
Output is correct |
40 |
Correct |
30 ms |
24912 KB |
Output is correct |
41 |
Correct |
22 ms |
23632 KB |
Output is correct |
42 |
Correct |
21 ms |
23644 KB |
Output is correct |
43 |
Correct |
27 ms |
24152 KB |
Output is correct |
44 |
Correct |
3 ms |
21084 KB |
Output is correct |
45 |
Correct |
3 ms |
21228 KB |
Output is correct |
46 |
Correct |
3 ms |
21084 KB |
Output is correct |
47 |
Correct |
3 ms |
21084 KB |
Output is correct |
48 |
Correct |
3 ms |
21340 KB |
Output is correct |
49 |
Correct |
5 ms |
21240 KB |
Output is correct |
50 |
Correct |
5 ms |
21084 KB |
Output is correct |
51 |
Correct |
6 ms |
21336 KB |
Output is correct |
52 |
Correct |
6 ms |
21340 KB |
Output is correct |
53 |
Correct |
19 ms |
23632 KB |
Output is correct |
54 |
Correct |
38 ms |
25788 KB |
Output is correct |
55 |
Correct |
42 ms |
26200 KB |
Output is correct |
56 |
Correct |
32 ms |
24924 KB |
Output is correct |
57 |
Correct |
62 ms |
27520 KB |
Output is correct |
58 |
Correct |
25 ms |
23636 KB |
Output is correct |
59 |
Correct |
32 ms |
24528 KB |
Output is correct |