Bzoj_1179_[apio2009]atm_tarjan+spfa
Test instructions: http://www.lydsy.com/JudgeOnline/problem.php?id=1179
Analysis:
Obviously there is no way to direct the longest road, then shrink the point to run.
The bar connects to meeting point.
Code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 #defineN 5000506 inthead[n],to[n<<1],nxt[n<<1],val[n],can[n],cancan[n];7 intn,m,bel[n],dfn[n],low[n],st[n],top,scc,tot,cnt,s,t;8 intIns[n],sum[n],x[n],y[n],q[n],l,r,dis[n],inq[n];9InlinevoidAddintUintv) {Tento[++cnt]=v;nxt[cnt]=head[u];head[u]=CNT; One } A voidTarjan (intx) { -st[top++]=x;ins[x]=1;d fn[x]=low[x]=++tot; - for(intI=head[x];i;i=Nxt[i]) { the if(!Dfn[to[i]]) { - Tarjan (To[i]); -low[x]=min (Low[x],low[to[i]]); -}Else if(Ins[to[i]]) low[x]=min (Low[x],dfn[to[i]]); + } - if(dfn[x]==Low[x]) { + intt=st[--top];ins[t]=0; Abel[t]=++SCC; atsum[scc]+=Val[t]; -cancan[scc]|=Can[t]; - while(t!=x) { -t=st[--top];ins[t]=0; -bel[t]=SCC; -sum[scc]+=Val[t]; incancan[scc]|=Can[t]; - } to } + } - intMain () { thescanf"%d%d",&n,&m); * intx, y; $ for(intI=1; i<=m;i++){Panax Notoginsengscanf"%d%d",&x,&y); - Add (x, y); thex[i]=x,y[i]=y; + } A for(intI=1; i<=n;i++){ thescanf"%d",&Val[i]); + } -scanf"%d%d",&s,&x); $t=n+1; $ for(intI=1; i<=x;i++){ -scanf"%d",&y); -can[y]=1; the } - for(intI=1; i<=n;i++){Wuyi if(!Dfn[i]) Tarjan (i); the } -Memset (Head,0,sizeof(head)); Cnt=0; Wu for(intI=1; i<=m;i++){ - if(bel[x[i]]!=Bel[y[i]]) Add (Bel[x[i]],bel[y[i]]); About } $ for(intI=1; i<=scc;i++)if(Cancan[i]) Add (i,t); -s=Bel[s]; -q[r++]=s;inq[s]=1;d is[s]=Sum[s]; - while(l^R) { A intx=q[l++];inq[x]=0;if(l==n+Ten) l=0; + for(intI=head[x];i;i=Nxt[i]) { the if(dis[to[i]]<dis[x]+Sum[to[i]]) { -dis[to[i]]=dis[x]+Sum[to[i]]; $ if(!Inq[to[i]]) { theinq[to[i]]=1; Q[r++]=to[i];if(r==n+Ten) r=0; the } the } the } - } inprintf"%d", Dis[t]); the}
Bzoj_1179_[apio2009]atm_tarjan+spfa