#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll MAXN = 5e4 + 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e17;
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) int((x).size())
#define endll '\n'
ll dp[10][MAXN], ans[MAXN], n, k;
vector <pii> adj[MAXN], adj2[MAXN];
vector <pair <pii, int>> vec[4 * MAXN];
void find(int ind, int l, int r, int s = 0, int e = (n - 1) / k + 1){
int mid = (s + e) / 2, L = mid * k, R = L + k;
if(l <= R && r >= L) vec[mid].pb(Mp(Mp(l, r), ind)); return;
if(r < L) find(ind, l, r, s, mid);
else find(ind, l, r, mid, e);
}
void upd(int s, int e, bool f){
if(!f){
// cout << s << ' ' << e << ' ' << dp[0][0] << endl;
for(int i = e - 1; i >= s; i --){
for(int j = 0; j < k; j ++)
for(auto u : adj[i]) dp[j][i] = min(dp[j][i] , dp[j][u.F] + u.S);
}
// cout << dp[0][0] << endl;
}
else{
for(int i = s; i < e; i ++){
for(int j = 0; j < k; j ++)
for(auto u : adj2[i]) dp[j][i] = min(dp[j][i], dp[j][u.F] + u.S);
}
}
return;
}
void cal(int s = 0, int e = (n - 1) / k + 1){
int mid = (s + e) / 2 , L = mid * k, R = L + k, ss = s * k, ee = e * k;
// cout << "check " << mid << ' ' << Sz(vec[mid]) << ' ' << ss << ' ' << ee << ' ' << L << ' ' << R << endll;
for(int i = 0; i < k; i ++) fill(dp[i] + ss, dp[i] + ee + 1, INF);
for(int i = 0; i < k; ++i) dp[i][i + L] = 0;
upd(ss , L , 0);
upd(R , ee , 1);
// cout << "BEBIN " << endl;
for(auto i : vec[mid]) for(int j = 0; j < k; j ++) {
// cout << i.F.F << ' ' << i.F.S << ' ' << dp[j][i.F.F] << ' ' << dp[j][i.F.S] << endl;
ans[i.S] = min(ans[i.S], dp[j][i.F.F] + dp[j][i.F.S]);
}
// cout << "kharej" << endl;
if(e - s == 1) return ;
cal(s , mid);
cal(mid + 1 , e);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int m, q;
cin >> k >> n >> m >> q;
for(int i = 0; i < m; i ++){
ll u, v, w;
cin >> u >> v >> w;
adj[u].pb(Mp(v, w)), adj2[v].pb(Mp(u, w));
}
for(int i = 0; i < q; i ++){
int u, v;
cin >> u >> v;
if(u > v){
ans[i] = -1;
continue;
}
find(i, u, v);
}
fill(ans, ans + MAXN, INF);
cal();
for(int i = 0; i < q; i ++) cout << (ans[i] == INF ? -1 : ans[i]) << endll;
return 0;
}
Compilation message
toll.cpp: In function 'void find(int, int, int, int, int)':
toll.cpp:25:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
25 | if(l <= R && r >= L) vec[mid].pb(Mp(Mp(l, r), ind)); return;
| ^~
toll.cpp:25:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
25 | if(l <= R && r >= L) vec[mid].pb(Mp(Mp(l, r), ind)); return;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
297 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
330 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
313 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
313 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
297 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |