#include"bits/stdc++.h"
using namespace std;
#define ll long long
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false)
#define inf LLONG_MAX
#define l first
#define r second
const int lmt=50001;
ll dp[lmt][17][5][5];
int main(){
fastio;
for(int i=0;i<lmt;i++){
for(int j=0;j<17;j++){
for(int k=0;k<5;k++){
for(int l=0;l<5;l++) dp[i][j][k][l]=1e15;
}
}
}
int k,n,m,q;
cin>>k>>n>>m>>q;
for(int i=1;i<=m;i++){
ll u,v,t;
cin>>u>>v>>t;
dp[u/k][0][u%k][v%k]=t;
}
for(int mv=1;mv<17;mv++){
for(int i=0;i<=(n-1)/k;i++){
for(int j=0;j<k;j++){
for(int l=0;l<k;l++){
for(int o=0;o<k;o++){
int b=i+(1<<(mv-1));
if(b>(n-1)/k) continue;
dp[i][mv][j][l]=min(dp[i][mv][j][l],dp[i][mv-1][j][o]+dp[b][mv-1][o][l]);
}
}
}
}
}
while(q--){
int u,v;
cin>>u>>v;
int pre=u/k;
ll val[k];
for(int i=0;i<k;i++) val[i]=1e15;
val[u%k]=0;
int dis=v/k-u/k;
for(int j=16;j>=0;j--){
int b=pre+(1<<j);
if(b>v/k || dis<(1<<j)) continue;
dis-=(1<<j);
ll val2[k];
for(int i=0;i<k;i++) val2[i]=1e15;
for(int i=0;i<k;i++){
for(int l=0;l<k;l++){
val2[i]=min(val2[i],val[l]+dp[pre][j][l][i]);
}
}
for(int i=0;i<k;i++) val[i]=val2[i];
}
ll ans=val[v%k];
cout<<(ans==1e15?-1:ans)<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
167568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
139 ms |
168248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166552 KB |
Output is correct |
2 |
Incorrect |
68 ms |
166532 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166552 KB |
Output is correct |
2 |
Incorrect |
68 ms |
166532 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
167568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |