Submission #869131

#TimeUsernameProblemLanguageResultExecution timeMemory
869131ElioritaToll (BOI17_toll)C++14
100 / 100
141 ms99668 KiB
#include<bits/stdc++.h> #define int long long #define pb push_back #define x first #define y second #define getbit(u,i) ((u>>i)&1) #define all(x) x.begin(),x.end() #define N 200001 using namespace std; typedef pair<int,int> ii; typedef pair<double,double> dd; int k,m,n,q,h[N],p[50010][17][6],s[50010][17][6],d[N],cur[6],nxt[6]; vector<ii> g[N],f[N]; vector<int> del; const int inf=4557430888798830399; void init() { for(int i=0;i<n;i++) h[i]=i/k+1; for(int dep=n/k;dep>=0;dep--) { for(int u=dep*k;u<=dep*k+k-1;u++) { for(ii v : g[u]) { p[u][0][v.x%k]=v.x; s[u][0][v.x%k]=v.y; } for(int i=1;i<=16;i++) { for(int j=0;j<k;j++) { for(int l=0;l<k;l++) { if(p[u][i-1][l]!=inf&&p[p[u][i-1][l]][i-1][j]!=inf) { p[u][i][j]=p[p[u][i-1][l]][i-1][j]; s[u][i][j]=min(s[u][i][j],s[u][i-1][l]+s[p[u][i-1][l]][i-1][j]); } } } } } } } priority_queue<ii,vector<ii>,greater<ii>> pq; void dijkstra(int u) { d[u]=0; pq.push({0,u}); while(!pq.empty()) { int u=pq.top().y; int du=pq.top().x; pq.pop(); if(du!=d[u]) continue; for(ii e : f[u]) { int v=e.y,dv=e.x; if(d[v]>du+dv) { d[v]=du+dv; pq.push({d[v],v}); } } } } void get(int u,int v) { int base=h[v]-h[u],last=0; for(int i=0;i<k;i++) cur[i]=i+(u/k)*k; d[v]=inf; for(int i=16;i>=0;i--) { if(getbit(base,i)) { last+=(1<<i); for(int j=0;j<k;j++) { // if(i==1) cout<<cur[j]<<' '; if(cur[j]!=inf) { for(int l=0;l<k;l++) { if(s[cur[j]][i][l]!=inf) { del.push_back(cur[j]); nxt[l]=1; f[cur[j]].pb({s[cur[j]][i][l],p[cur[j]][i][l]}); // cout<<cur[j]<<' '<<p[cur[j]][i][l]<<'\n'; } } } } for(int j=0;j<k;j++) { // cout<<cur[j]<<' '; if(nxt[j]) { cur[j]=(u/k)*k+last*k+j; d[cur[j]]=inf; } else cur[j]=inf; nxt[j]=0; } // cout<<'\n'; } } dijkstra(u); for(int u : del) f[u].clear(); del.clear(); if(d[v]==inf) cout<<-1<<'\n'; else cout<<d[v]<<'\n'; } signed main() { if(fopen("demo.inp","r")) { freopen("demo.inp","r",stdin); freopen("demo.out","w",stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>k>>n>>m>>q; for(int i=1;i<=m;i++) { int u,v,w; cin>>u>>v>>w; g[u].pb({v,w}); } memset(p,0x3f,sizeof(p)); memset(s,0x3f,sizeof(s)); init(); while(q--) { int u,v; cin>>u>>v; get(u,v); } }

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen("demo.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen("demo.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...