#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;
typedef long long ll;
int n, m, k, q;
vector<pii> adj[50006];
vector<pii> radj[50006];
int lx[10006];
int rx[10006];
ll ans[10006];
map<pii, int> edge;
vector<vector<ll>> calc(int l, int r, vi &qu){
vector<vector<ll>> vec(k*(r-l+1), vector<ll>(2*k));
if(r-l==1){
rep(i, 0, k-1){
rep(j, 0, k-1){
int t=edge[{l*k+i, r*k+j}];
vec[i][k+j]=vec[k+j][i]=(t?t:1e10);
}
}
return vec;
}
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);
}
vector<vector<ll>> lvec=calc(l, m, lq);
vector<vector<ll>> rvec=calc(m, r, rq);
trav(i, qu){
if(rx[i]/k<m || lx[i]/k>m) continue;
if(rx[i]/k==m){
ans[i]=lvec[lx[i]-l*k][k+rx[i]%k]; continue;
}
if(lx[i]/k==m){
ans[i]=rvec[rx[i]-m*k][lx[i]%k]; continue;
}
ans[i]=1e10;
rep(j, 0, k-1){
ans[i]=min(ans[i], lvec[lx[i]-l*k][k+j]+rvec[rx[i]-m*k][j]);
}
}
rep(i, l*k, m*k+k-1){
rep(j, 0, k-1){
vec[i-l*k][j]=lvec[i-l*k][j];
}
}
rep(i, m*k, r*k+k-1){
rep(j, k, 2*k-1){
vec[i-l*k][j]=rvec[i-m*k][j];
}
}
rep(i, (m+1)*k, r*k+k-1){
rep(j, 0, k-1){
vec[i-l*k][j]=1e10;
trav(p, radj[i]) vec[i-l*k][j]=min(vec[i-l*k][j], vec[p.f-l*k][j]+p.s);
}
}
for(int i=m*k-1; i>=l*k; i--){
rep(j, 0, k-1){
vec[i-l*k][j+k]=1e10;
trav(p, adj[i]) vec[i-l*k][j+k]=min(vec[i-l*k][j+k], vec[p.f-l*k][j+k]+p.s);
}
}
return vec;
}
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]==1e10?-1:(lx[i]/k==rx[i]/k?-1:ans[i])) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
20496 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 |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
4 ms |
3020 KB |
Output is correct |
7 |
Correct |
4 ms |
3020 KB |
Output is correct |
8 |
Correct |
180 ms |
20504 KB |
Output is correct |
9 |
Correct |
223 ms |
20148 KB |
Output is correct |
10 |
Correct |
93 ms |
14248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
28716 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 |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 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 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 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 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
20496 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 |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
4 ms |
3020 KB |
Output is correct |
7 |
Correct |
4 ms |
3020 KB |
Output is correct |
8 |
Correct |
180 ms |
20504 KB |
Output is correct |
9 |
Correct |
223 ms |
20148 KB |
Output is correct |
10 |
Correct |
93 ms |
14248 KB |
Output is correct |
11 |
Correct |
303 ms |
28716 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |