# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
588739 | krit3379 | Toll (BOI17_toll) | C++17 | 96 ms | 42736 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define N 50005
int mod;
long long dp[20][5][N];
vector<long long> v(5);
int t(int i,int j,int st){
return i/mod*mod+mod*(1<<st)+j;
}
int main(){
int n,m,q,st,ss,i,j,k,a,b,w,now;
scanf("%d %d %d %d",&mod,&n,&m,&q);
for(i=0;i<20;i++)for(j=0;j<5;j++)for(k=0;k<N;k++)dp[i][j][k]=1e15;
for(i=1;i<=m;i++){
scanf("%d %d %d",&a,&b,&w);
dp[0][b%mod][a]=w;
}
for(st=1;st<19;st++){
ss=1<<st;
for(i=0;i<n;i++){
if(i+mod*ss>=N)break;
for(j=0;j<mod;j++){
for(k=0;k<mod;k++){
dp[st][k][i]=min(dp[st][k][i],dp[st-1][j][i]+dp[st-1][k][t(i,j,st-1)]);
}
}
}
}
while(q--){
scanf("%d %d",&a,&b);
now=a/mod;
for(i=0;i<mod;i++)v[i]=1e15;
v[a%mod]=0;
for(st=19;st>=0;st--){
if(now*mod+mod*(1<<st)<=b/mod*mod){
vector<long long> temp(mod,1e15);
for(i=0;i<mod;i++)for(j=0;j<mod;j++){
temp[j]=min(temp[j],dp[st][j][now*mod+i]+v[i]);
}
for(i=0;i<mod;i++)v[i]=temp[i];
now+=1<<st;
}
}
if(v[b%mod]<1e15)printf("%lld\n",v[b%mod]);
else printf("-1\n");
}
return 0;
}
Compilation message (stderr)
# | 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... |