#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(x, a) for(auto &x:a)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define nl '\n'
#define pb push_back
typedef vector<int> vi;
typedef pair<int, int> pii;
int n, m, k, q;
vector<pii> adj[50001];
vector<pii> radj[50001];
int lx[10001];
int rx[10001];
int ans[10001];
map<pii, int> edge;
map<pii, int> calc(int l, int r, vi qu){
map<pii, int> mp;
if(r-l==1){
rep(i, l*k, l*k+k-1){
rep(j, r*k, r*k+k-1){
int t=edge[{i, j}];
mp[{i, j}]=mp[{j, i}]=(t?t:1e9);
}
}
return mp;
}
int m=(l+r)/2;
vi lq, rq;
trav(i, qu){
if(rx[i]/k<m) lq.pb(i);
if(lx[i]/k>m) rq.pb(i);
}
map<pii, int> lmp=calc(l, m, lq);
map<pii, int> rmp=calc(m, r, rq);
trav(i, qu){
if(rx[i]/k<m || lx[i]/k>m) continue;
if(rx[i]/k==m){
ans[i]=lmp[{lx[i], rx[i]}]; continue;
}
if(lx[i]/k==m){
ans[i]=rmp[{lx[i], rx[i]}]; continue;
}
ans[i]=1e9;
rep(j, m*k, m*k+k-1){
ans[i]=min(ans[i], lmp[{lx[i], j}]+rmp[{j, rx[i]}]);
}
}
mp.insert(all(lmp));
mp.insert(all(rmp));
rep(i, (m+1)*k, r*k+k-1){
rep(j, l*k, l*k+k-1){
mp[{i, j}]=1e9;
trav(p, radj[i]) mp[{i, j}]=min(mp[{i, j}], mp[{j, p.f}]+p.s);
mp[{j, i}]=mp[{i, j}];
}
}
for(int i=m*k-1; i>=l*k; i--){
rep(j, r*k, r*k+k-1){
mp[{i, j}]=1e9;
trav(p, adj[i]) mp[{i, j}]=min(mp[{i, j}], mp[{j, p.f}]+p.s);
mp[{j, i}]=mp[{i, j}];
}
}
return mp;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> k >> n >> m >> q;
rep(i, 1, m){
int a, b, t; cin >> a >> b >> t;
adj[a].pb({b, t});
radj[b].pb({a, t});
edge[{a, b}]=edge[{b, a}]=t;
}
vi qu;
rep(i, 1, q){
cin >> lx[i] >> rx[i];
qu.pb(i);
}
calc(0, (n-1)/k, qu);
rep(i, 1, q) cout << (ans[i]==1e9?-1:ans[i]) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2595 ms |
203552 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
19 ms |
5188 KB |
Output is correct |
6 |
Correct |
20 ms |
5252 KB |
Output is correct |
7 |
Correct |
19 ms |
5164 KB |
Output is correct |
8 |
Correct |
2546 ms |
203432 KB |
Output is correct |
9 |
Correct |
2581 ms |
202984 KB |
Output is correct |
10 |
Correct |
2151 ms |
196368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3095 ms |
184744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2595 ms |
203552 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
19 ms |
5188 KB |
Output is correct |
6 |
Correct |
20 ms |
5252 KB |
Output is correct |
7 |
Correct |
19 ms |
5164 KB |
Output is correct |
8 |
Correct |
2546 ms |
203432 KB |
Output is correct |
9 |
Correct |
2581 ms |
202984 KB |
Output is correct |
10 |
Correct |
2151 ms |
196368 KB |
Output is correct |
11 |
Execution timed out |
3095 ms |
184744 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |