This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
const int lim=5e4+100;
#else
const int lim=3e3+3;
#endif
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define pb push_back
const int mod=998244353;
//const int mod=(1ll<<61)-1;
using pii=pair<int,int>;
int dist[16][lim][5][5];
inline void solve(){
int k,n,m,o;
cin>>k>>n>>m>>o;
for(int i=0;i<16;i++){
for(int j=0;j<=n/k;j++){
for(int ii=0;ii<k;ii++){
for(int jj=0;jj<k;jj++){
dist[i][j][ii][jj]=INT_MAX;
}
}
}
}
for(int i=0;i<m;i++){
int x,y,z;
cin>>x>>y>>z;
dist[0][x/k][x%k][y%k]=z;
}
for(int jp=1;jp<16;jp++){
for(int i=0;i<=n/k;i++){
if(lim<=i+(1<<jp))break;
for(int l=0;l<k;l++){
for(int r=0;r<k;r++){
for(int mid=0;mid<k;mid++){
dist[jp][i][l][r]=min(
dist[jp][i][l][r],
dist[jp-1][i][l][mid]+
dist[jp-1][i+(1<<(jp-1))][mid][r]
);
}
}
}
}
}
while(o--){
int a,b;
cin>>a>>b;
int cur=a/k,dest=b/k;
if(dest<=cur){
cout<<"-1\n";
continue;
}
int*now=new int[k],*to=new int[k];
for(int i=0;i<k;i++){
now[i]=to[i]=INT_MAX;
}
now[a%k]=0;
for(int jp=15;0<=jp;jp--){
if(cur+(1<<jp)<=dest){
for(int i=0;i<k;i++){
to[i]=INT_MAX;
for(int j=0;j<k;j++){
to[i]=min(to[i],
now[j]+dist[jp][cur][j][i]);
}
}
cur+=1<<jp;
swap(now,to);
}
}
if(now[b%k]^INT_MAX){
cout<<now[b%k]<<"\n";
}else{
cout<<"-1\n";
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#else
#endif
int t=1;
//cin>>t;
while (t--)
{
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |