Submission #77825

#TimeUsernameProblemLanguageResultExecution timeMemory
77825tmwilliamlin168Wild Boar (JOI18_wild_boar)C++14
0 / 100
3 ms736 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define a array const int mxN=2e3, mxL=1e5; const ll INF=1e18; int n, m, t, l, x[mxL], eu, ev[2*mxN], ew[mxN], nx[4*mxN], le[mxN], pv[4*mxN]; ll d[4*mxN]; a<ll, 3> st[2*(mxL-1)][4]; priority_queue<a<ll, 2>, vector<a<ll, 2>>, greater<a<ll, 2>>> pq; a<ll, 3> dij(int s, int t, int ia, int ib) { memset(d, 0x3f, 8*4*m); while(pq.size()) pq.pop(); // cout << "iab " << ia << " " << ib << endl; if(ia==-1) { s=2*le[s]; d[s]=0; pq.push({0, s}); pv[s]=-1; } else { ia=2*ia+(ev[2*ia]==s); for(int i=0, j; i<2; ++i) { if((j=nx[2*ia+i])==-1) continue; d[j]=0; pq.push({0, j}); pv[j]=-1; // cout << "ie " << j << " " << 2*ia+i << endl; } } while(pq.size()) { a<ll, 2> u=pq.top(); pq.pop(); if(u[0]>d[u[1]]) continue; int e=u[1]/2; // if(s==12&&t==3) // cout << u[1] << " " << u[0] << " " << ev[e^1] << " " << ev[e] << endl; if(ev[e]!=t||e/2==ib) { for(int i=0; i<2; ++i) { int v=nx[(e^1)*2+i]; if(v==-1||d[v]<=u[0]+ew[e/2]) continue; d[v]=u[0]+ew[e/2]; pq.push({d[v], v}); pv[v]=u[1]; } } else { int v=u[1]; for(; pv[v]!=-1; v=pv[v]); return {u[0]+ew[e/2], v/4, e/2}; } if(nx[u[1]]!=-1&&d[nx[u[1]]]>u[0]) { d[nx[u[1]]]=u[0]; pq.push({u[0], nx[u[1]]}); pv[nx[u[1]]]=pv[u[1]]; } } return {INF, -1, -1}; } void uc(int i) { int x1=x[i], x2=x[i+1]; i+=l-1; st[i][0]=dij(x1, x2, -1, -1); st[i][1]=dij(x1, x2, st[i][0][1], st[i][0][2]); st[i][2]=dij(x1, x2, st[i][0][1], st[i][1][2]); st[i][3]=dij(x1, x2, st[i][1][1], st[i][0][2]); // for(int j=0; j<4; ++j) { // cout << "o " << i << " " << j << endl; // for(int k=0; k<3; ++k) // cout << st[i][j][k] << " "; // cout << endl; // } while(i/=2) { st[i][0]={0, -1, -1}; auto b=[&](const int &j) { st[i][j]={INF, -1, -1}; for(int k=0; k<4; ++k) for(int l=0; l<4; ++l) if(st[2*i][k][1]!=st[i][j&1?0:3][1]&&st[2*i+1][l][2]!=st[i][j&2?0:3][2]&&st[2*i][k][2]!=st[2*i+1][l][1]) st[i][j]=min(a<ll, 3>{st[2*i][k][0]+st[2*i+1][l][0], st[2*i][k][1], st[2*i+1][l][2]}, st[i][j]); }; b(3); for(int j=0; j<3; ++j) b(j); // for(int j=0; j<4; ++j) { // cout << "I " << i << " " << j << endl; // for(int k=0; k<3; ++k) // cout << st[i][j][k] << " "; // cout << endl; // } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> t >> l; memset(le, -1, 4*n); memset(nx, -1, 4*4*m); for(int i=0; i<2*m; ++i) { if(i&1) { ev[i]=eu; eu=ev[i^1]; } else cin >> eu >> ev[i] >> ew[i/2], --eu, --ev[i]; if(le[eu]!=-1) { nx[2*i]=2*le[eu]; nx[2*le[eu]+1]=2*i+1; } else nx[2*i]=-1; le[eu]=i; // cout << "e " << i << " " << eu << " " << ev[i] << endl; } for(int i=0; i<l; ++i) { cin >> x[i], --x[i]; if(i) uc(i-1); } while(t--) { int pk, qk; cin >> pk >> qk, --pk, --qk; x[pk]=qk; if(pk) uc(pk-1); if(pk<l-1) uc(pk); ll ans=min({st[1][0][0], st[1][1][0], st[1][2][0], st[1][3][0]}); cout << (ans>=INF?-1:ans) << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...