#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update> ordset;
#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")
ll INF = 2e18;
const ll mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll n, ll m){
if(m == 0) return 1ll;
if(m & 1ll) return (n * binpow(n, m - 1ll)) % mod;
ll b = binpow(n, m / 2ll);
return (b*b) % mod;
}
int n, k, m, o;
vector<vector<pll>> b, a;
ll ans[50005][19][22];
void dq(int l, int r, int lev){
if(l > r) return;
int m = (l + r) / 2;
for(int i = m; i >= l; i--)
for(int j = 0; j <= 4 * k; j++) ans[i][lev][j] = (i == m - j ? 0ll : INF);
for(int i = m + 1; i <= r; i++){
for(int j = 0; j <= 4* k; j++) ans[i][lev][j] = INF;
for(pll u : b[i])
for(int j = 0; j <= 4 * k; j++) if(m - j >= l && u.ff >= l) ans[i][lev][j] = min(ans[i][lev][j], ans[u.ff][lev][j] + u.ss);
}
// cout << l << ' ' << r << ' ' << m << ' ' << lev << '\n';
for(int i = m; i >= l; i--){
for(pll u : a[i]){
if(u.ff > m) continue;
//cout << i << ' ' << u.ff << ' ' << u.ss << '\n';
for(int j = 0; j <= 4* k; j++) if(m - j >= l) ans[i][lev][j] = min(ans[i][lev][j], ans[u.ff][lev][j] + u.ss);
}
}
// for(int i = l; i <= r; i++){
// cout << i << '\n';
// for(int j = 0; j <= 3 * k; j++) cout << ans[i][lev][j] << ' ';
// cout << '\n';
// }
dq(l, m - 1, lev + 1);
dq(m + 1, r, lev + 1);
}
ll ask(int l, int r, int lev, int a, int b){
assert(l <= r);
int m = (l + r) / 2;
// cout << l << ' ' << r << ' ' << m << '\n';
if(a <= m && m + 1 <= b){
// cout << l << ' ' << r << ' ' << m << ' ' << a << ' ' << b << '\n';
ll res = INF;
for(int j = 0; j <= 4*k; j++) res = min(res, ans[a][lev][j] + ans[b][lev][j]);
return res;
}
if(a < m && b == m) return ans[a][lev][0];
if(a > m) return ask(m + 1, r, lev + 1, a, b);
return ask(l, m - 1, lev + 1, a, b);
}
void solve(){
cin >> k >> n >> m >> o;
a.resize(n);
b.resize(n);
for(int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
a[u].push_back({v, w});
b[v].push_back({u, w});
}
dq(0, n - 1, 0);
for(int i = 0; i < o; i++){
int u, v;
cin >> u >> v;
if(u == v){
cout << 0 << '\n';
continue;
}
if(u > v){
cout << -1 << '\n';
continue;
}
ll res = ask(0, n - 1, 0, u, v);
cout << (res == INF ? -1 : res) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int tt;
//cin >> tt;
tt = 1;
while(tt--){
solve();
//brute();
#ifdef LOCAL
cout << "__________________________________" << endl;
#endif
}
#ifdef LOCAL
cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
169412 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
3668 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
2 ms |
3692 KB |
Output is correct |
8 |
Correct |
110 ms |
169332 KB |
Output is correct |
9 |
Correct |
127 ms |
169336 KB |
Output is correct |
10 |
Correct |
77 ms |
166256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
171352 KB |
Output is correct |
2 |
Correct |
1 ms |
260 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
3740 KB |
Output is correct |
8 |
Correct |
5 ms |
3796 KB |
Output is correct |
9 |
Correct |
124 ms |
169456 KB |
Output is correct |
10 |
Correct |
205 ms |
174156 KB |
Output is correct |
11 |
Correct |
159 ms |
171644 KB |
Output is correct |
12 |
Correct |
153 ms |
170400 KB |
Output is correct |
13 |
Correct |
193 ms |
109288 KB |
Output is correct |
14 |
Correct |
114 ms |
105152 KB |
Output is correct |
15 |
Correct |
114 ms |
103876 KB |
Output is correct |
16 |
Correct |
114 ms |
103892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
2 ms |
3664 KB |
Output is correct |
8 |
Correct |
5 ms |
3924 KB |
Output is correct |
9 |
Correct |
3 ms |
3796 KB |
Output is correct |
10 |
Correct |
107 ms |
169280 KB |
Output is correct |
11 |
Correct |
164 ms |
171292 KB |
Output is correct |
12 |
Correct |
219 ms |
174044 KB |
Output is correct |
13 |
Correct |
194 ms |
174856 KB |
Output is correct |
14 |
Correct |
170 ms |
172608 KB |
Output is correct |
15 |
Correct |
110 ms |
103904 KB |
Output is correct |
16 |
Correct |
111 ms |
103892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
2 ms |
3664 KB |
Output is correct |
8 |
Correct |
5 ms |
3924 KB |
Output is correct |
9 |
Correct |
3 ms |
3796 KB |
Output is correct |
10 |
Correct |
107 ms |
169280 KB |
Output is correct |
11 |
Correct |
164 ms |
171292 KB |
Output is correct |
12 |
Correct |
219 ms |
174044 KB |
Output is correct |
13 |
Correct |
194 ms |
174856 KB |
Output is correct |
14 |
Correct |
170 ms |
172608 KB |
Output is correct |
15 |
Correct |
110 ms |
103904 KB |
Output is correct |
16 |
Correct |
111 ms |
103892 KB |
Output is correct |
17 |
Correct |
150 ms |
171240 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
3668 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
5 ms |
3796 KB |
Output is correct |
26 |
Correct |
4 ms |
3796 KB |
Output is correct |
27 |
Correct |
115 ms |
169432 KB |
Output is correct |
28 |
Correct |
192 ms |
174068 KB |
Output is correct |
29 |
Correct |
220 ms |
174964 KB |
Output is correct |
30 |
Correct |
177 ms |
172804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
169412 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
3668 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
2 ms |
3692 KB |
Output is correct |
8 |
Correct |
110 ms |
169332 KB |
Output is correct |
9 |
Correct |
127 ms |
169336 KB |
Output is correct |
10 |
Correct |
77 ms |
166256 KB |
Output is correct |
11 |
Correct |
158 ms |
171352 KB |
Output is correct |
12 |
Correct |
1 ms |
260 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
5 ms |
3740 KB |
Output is correct |
18 |
Correct |
5 ms |
3796 KB |
Output is correct |
19 |
Correct |
124 ms |
169456 KB |
Output is correct |
20 |
Correct |
205 ms |
174156 KB |
Output is correct |
21 |
Correct |
159 ms |
171644 KB |
Output is correct |
22 |
Correct |
153 ms |
170400 KB |
Output is correct |
23 |
Correct |
193 ms |
109288 KB |
Output is correct |
24 |
Correct |
114 ms |
105152 KB |
Output is correct |
25 |
Correct |
114 ms |
103876 KB |
Output is correct |
26 |
Correct |
114 ms |
103892 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
3668 KB |
Output is correct |
33 |
Correct |
2 ms |
3664 KB |
Output is correct |
34 |
Correct |
5 ms |
3924 KB |
Output is correct |
35 |
Correct |
3 ms |
3796 KB |
Output is correct |
36 |
Correct |
107 ms |
169280 KB |
Output is correct |
37 |
Correct |
164 ms |
171292 KB |
Output is correct |
38 |
Correct |
219 ms |
174044 KB |
Output is correct |
39 |
Correct |
194 ms |
174856 KB |
Output is correct |
40 |
Correct |
170 ms |
172608 KB |
Output is correct |
41 |
Correct |
110 ms |
103904 KB |
Output is correct |
42 |
Correct |
111 ms |
103892 KB |
Output is correct |
43 |
Correct |
150 ms |
171240 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
3 ms |
3668 KB |
Output is correct |
50 |
Correct |
3 ms |
3796 KB |
Output is correct |
51 |
Correct |
5 ms |
3796 KB |
Output is correct |
52 |
Correct |
4 ms |
3796 KB |
Output is correct |
53 |
Correct |
115 ms |
169432 KB |
Output is correct |
54 |
Correct |
192 ms |
174068 KB |
Output is correct |
55 |
Correct |
220 ms |
174964 KB |
Output is correct |
56 |
Correct |
177 ms |
172804 KB |
Output is correct |
57 |
Correct |
254 ms |
175392 KB |
Output is correct |
58 |
Correct |
114 ms |
169804 KB |
Output is correct |
59 |
Correct |
160 ms |
171892 KB |
Output is correct |