#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ar array
const int inf = 1e9;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k, n, m, q;
cin >> k >> n >> m >> q;
vector <vector<ar<int,2>>> adj(n), rev(n);
for ( int i = 0; i < m; i++ ){
int u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w});
rev[v].pb({u, w});
}
vector <vector<int>> qu(n / k + 1);
for ( int i = 0; i < n; i++ ) qu[i / k].pb(i);
auto calc = [&](int sx, int l, int r, auto &adj){
vector <int> dp(r - l + 1, inf);
dp[sx - l] = 0;
priority_queue <ar<int,2>> q;
q.push({0, sx});
while ( !q.empty() ){
auto [val, u] = q.top();
q.pop(); val *= -1;
if ( val != dp[u - l] ) continue;
for ( auto &[v, w]: adj[u] ){
if ( v < l || v > r ) continue;
if ( dp[v - l] > val + w ){
dp[v - l] = val + w;
q.push({-dp[v - l], v});
}
}
}
return dp;
};
vector <vector<vector<int>>> lf(20, vector <vector<int>> (n)), rg(20, vector <vector<int>> (n));
auto dnc = [&](auto dnc, int l, int r, int lvl) -> void{
if ( l > r ) return;
int m = (l + r) / 2;
for ( auto &u: qu[m] ){
lf[lvl][u] = calc(u, l * k, (m + 1) * k - 1, rev);
rg[lvl][u] = calc(u, m * k, (r + 1) * k - 1, adj);
}
dnc(dnc, l, m - 1, lvl + 1);
dnc(dnc, m + 1, r, lvl + 1);
};
dnc(dnc, 0, (n - 1) / k, 0);
auto qry = [&](int u, int v){
if ( u / k == v / k ) return -1;
int l = 0, r = (n - 1) / k, lvl = 0;
while ( true ){
int m = (l + r) / 2;
if ( v < m * k ) r = m - 1;
else if ( u >= (m + 1) * k ) l = m + 1;
else{
int opt = inf;
for ( auto &t: qu[m] ){
if ( l * k <= t && (r + 1) * k > t ){
opt = min(opt, lf[lvl][t][u - l * k] + rg[lvl][t][v - m * k]);
}
}
if ( opt == inf ) opt = -1;
return opt;
}
lvl += 1;
}
};
while ( q-- ){
int u, v; cin >> u >> v;
cout << qry(u, v) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
61032 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
3 ms |
1616 KB |
Output is correct |
8 |
Correct |
97 ms |
61876 KB |
Output is correct |
9 |
Correct |
81 ms |
61704 KB |
Output is correct |
10 |
Correct |
54 ms |
57780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
61712 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
4 ms |
1616 KB |
Output is correct |
8 |
Correct |
5 ms |
1672 KB |
Output is correct |
9 |
Correct |
90 ms |
61876 KB |
Output is correct |
10 |
Correct |
226 ms |
67252 KB |
Output is correct |
11 |
Correct |
140 ms |
63412 KB |
Output is correct |
12 |
Correct |
102 ms |
64552 KB |
Output is correct |
13 |
Correct |
272 ms |
45768 KB |
Output is correct |
14 |
Correct |
143 ms |
40584 KB |
Output is correct |
15 |
Correct |
165 ms |
42120 KB |
Output is correct |
16 |
Correct |
165 ms |
42040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
7 ms |
1616 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
90 ms |
61620 KB |
Output is correct |
11 |
Correct |
144 ms |
63156 KB |
Output is correct |
12 |
Correct |
233 ms |
66956 KB |
Output is correct |
13 |
Correct |
247 ms |
67764 KB |
Output is correct |
14 |
Correct |
191 ms |
65880 KB |
Output is correct |
15 |
Correct |
158 ms |
42092 KB |
Output is correct |
16 |
Correct |
159 ms |
42120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
7 ms |
1616 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
90 ms |
61620 KB |
Output is correct |
11 |
Correct |
144 ms |
63156 KB |
Output is correct |
12 |
Correct |
233 ms |
66956 KB |
Output is correct |
13 |
Correct |
247 ms |
67764 KB |
Output is correct |
14 |
Correct |
191 ms |
65880 KB |
Output is correct |
15 |
Correct |
158 ms |
42092 KB |
Output is correct |
16 |
Correct |
159 ms |
42120 KB |
Output is correct |
17 |
Correct |
137 ms |
63260 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
424 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
3 ms |
1616 KB |
Output is correct |
24 |
Correct |
3 ms |
1616 KB |
Output is correct |
25 |
Correct |
6 ms |
1616 KB |
Output is correct |
26 |
Correct |
5 ms |
1616 KB |
Output is correct |
27 |
Correct |
99 ms |
61628 KB |
Output is correct |
28 |
Correct |
234 ms |
66996 KB |
Output is correct |
29 |
Correct |
252 ms |
67976 KB |
Output is correct |
30 |
Correct |
178 ms |
65972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
61032 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
3 ms |
1616 KB |
Output is correct |
8 |
Correct |
97 ms |
61876 KB |
Output is correct |
9 |
Correct |
81 ms |
61704 KB |
Output is correct |
10 |
Correct |
54 ms |
57780 KB |
Output is correct |
11 |
Correct |
134 ms |
61712 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
4 ms |
1616 KB |
Output is correct |
18 |
Correct |
5 ms |
1672 KB |
Output is correct |
19 |
Correct |
90 ms |
61876 KB |
Output is correct |
20 |
Correct |
226 ms |
67252 KB |
Output is correct |
21 |
Correct |
140 ms |
63412 KB |
Output is correct |
22 |
Correct |
102 ms |
64552 KB |
Output is correct |
23 |
Correct |
272 ms |
45768 KB |
Output is correct |
24 |
Correct |
143 ms |
40584 KB |
Output is correct |
25 |
Correct |
165 ms |
42120 KB |
Output is correct |
26 |
Correct |
165 ms |
42040 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
1616 KB |
Output is correct |
33 |
Correct |
2 ms |
1616 KB |
Output is correct |
34 |
Correct |
7 ms |
1616 KB |
Output is correct |
35 |
Correct |
6 ms |
1784 KB |
Output is correct |
36 |
Correct |
90 ms |
61620 KB |
Output is correct |
37 |
Correct |
144 ms |
63156 KB |
Output is correct |
38 |
Correct |
233 ms |
66956 KB |
Output is correct |
39 |
Correct |
247 ms |
67764 KB |
Output is correct |
40 |
Correct |
191 ms |
65880 KB |
Output is correct |
41 |
Correct |
158 ms |
42092 KB |
Output is correct |
42 |
Correct |
159 ms |
42120 KB |
Output is correct |
43 |
Correct |
137 ms |
63260 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
424 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
3 ms |
1616 KB |
Output is correct |
50 |
Correct |
3 ms |
1616 KB |
Output is correct |
51 |
Correct |
6 ms |
1616 KB |
Output is correct |
52 |
Correct |
5 ms |
1616 KB |
Output is correct |
53 |
Correct |
99 ms |
61628 KB |
Output is correct |
54 |
Correct |
234 ms |
66996 KB |
Output is correct |
55 |
Correct |
252 ms |
67976 KB |
Output is correct |
56 |
Correct |
178 ms |
65972 KB |
Output is correct |
57 |
Correct |
344 ms |
71088 KB |
Output is correct |
58 |
Correct |
87 ms |
61932 KB |
Output is correct |
59 |
Correct |
141 ms |
63420 KB |
Output is correct |