Submission #862649

#TimeUsernameProblemLanguageResultExecution timeMemory
862649Ahmed57Toll (BOI17_toll)C++17
100 / 100
342 ms51732 KiB
#include <iostream> #include <bits/stdc++.h> using namespace std; long long k;vector<pair<int,int>> adj[100001],rev[1000001]; long long an[100001]; map<pair<int,int>,long long> mp; void dc(int l,int r,vector<pair<pair<int,int>,int>> qu){ if(l>=r)return; int md = (l+r)/2; long long ll[k][(int)k*(md-l+1)],rr[k][(k*(r-md))]; for(int j = md;j>=l;j--){ for(int z = 0;z<k;z++){ for(int a = 0;a<k;a++){ if(j==md){ if(z==a){ ll[a][((md+1)*k-1)-(j*k+z)] = 0; } else ll[a][((md+1)*k-1)-(j*k+z)] = 1e9; continue; } ll[a][((md+1)*k-1)-(j*k+z)] = 1e9; for(auto e:adj[j*k+z]){ ll[a][((md+1)*k-1)-(j*k+z)] = min(ll[a][((md+1)*k-1)-(j*k+z)],ll[a][((md+1)*k-1)-e.first]+e.second); } } } } for(int j = md+1;j<=r;j++){ for(int z = 0;z<k;z++){ for(int a = 0;a<k;a++){ if(j==md+1){ if(z==a)rr[a][(j*k+z)-((md+1)*k)] = 0; else rr[a][(j*k+z)-((md+1)*k)] = 1e9; continue; } rr[a][(j*k+z)-((md+1)*k)] = 1e9; for(auto e:rev[j*k+z]){ rr[a][(j*k+z)-((md+1)*k)] = min(rr[a][(j*k+z)-((md+1)*k)],rr[a][e.first-((md+1)*k)]+e.second); } } } } vector<pair<pair<int,int>,int>> R,L; for(auto i:qu){ if(i.first.first/k>md){ R.push_back(i); }else if(i.first.second/k<md){ L.push_back(i); }else{ if((i.first.second/k)==md){ an[i.second] = ll[i.first.second%k][((md+1)*k-1)-i.first.first]; }else{ long long ans = 1e18; for(int z = 0;z<k;z++){ for(int e = 0;e<k;e++){ ans = min(ans,ll[z][((md+1)*k-1)-i.first.first]+rr[e][i.first.second-((md+1)*k)]+(mp.count(make_pair((md*k)+z,(md+1)*k+e))==0?1000000000:mp[{(md*k)+z,(md+1)*k+e}])); } } an[i.second] = ans; } } } dc(l,md-1,L);dc(md+1,r,R); } int main(){ memset(an,-1,sizeof an); int n,m,o; cin>>k>>n>>m>>o; for(int i = 0;i<m;i++){ long long a,b,c; cin>>a>>b>>c; mp[{a,b}] = c; adj[a].push_back({b,c}); rev[b].push_back({a,c}); } vector<pair<pair<int,int>,int>> qu; for(int i = 0;i<o;i++){ long long a,b;cin>>a>>b; if(a==b){ an[i] = 0;continue; } qu.push_back({{a,b},i}); } dc(0,(n-1)/k,qu); for(int i = 0;i<o;i++){ cout<<(an[i]>=1e9?-1:an[i])<<endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...