# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1087267 | PieArmy | Toll (BOI17_toll) | C++17 | 65 ms | 19288 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.
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=1000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
int k,n,m,q;
int nex[50001][16][5];
void code(){
cin>>k>>n>>m>>q;
for(int i=0;i<n;i++){
for(int j=0;j<16;j++){
for(int l=0;l<k;l++){
nex[i][j][l]=sonsuz;
}
}
}
for(int i=1;i<=m;i++){
int a,b,t;cin>>a>>b>>t;
nex[a][0][b%k]=min(t,nex[a][0][b%k]);
}
for(int i=1;i<16;i++){
for(int x=0;x<n;x++){
for(int a=0;a<k;a++){
for(int b=0;b<k;b++){
int y=((x/k)+pie(i-1))*k+a;
if(y>=n)continue;
nex[x][i][b]=min(nex[x][i][b],nex[x][i-1][a]+nex[y][i-1][b]);
}
}
}
}
while(q--){
int yunuy,suayp;cin>>yunuy>>suayp;
int pos=yunuy/k,tar=suayp/k;
int ans[k];for(int &x:ans)x=sonsuz;
ans[yunuy%k]=0;
for(int i=0;i<16;i++){
if((tar-pos)&pie(i)){
int yeni[k];for(int &x:yeni)x=sonsuz;
for(int a=0;a<k;a++){
if(ans[a]==sonsuz)continue;
for(int b=0;b<k;b++){
if(nex[pos*k+a][i][b]==sonsuz)continue;
yeni[b]=min(yeni[b],ans[a]+nex[pos*k+a][i][b]);
}
}
for(int j=0;j<k;j++){
ans[j]=yeni[j];
}
pos+=pie(i);
}
}
if(ans[suayp%k]==sonsuz){
cout<<-1;
}
else cout<<ans[suayp%k];
cout<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();cout<<endl;}
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... |