Tarjan, the point at which the 0 is found (possibly the end of a chain or a point), the point in which the degree is 0 must be controlled.
The minimum value of the point that can be controlled in each strongly connected component is the minimum value of the point after the indent point, and the minimum value of the point at which the entry is 0 is the minimum cost.
Also note the smallest number in each of the strongly connected components, the smallest of which is the first point that cannot be controlled.
#include <iostream>#include<cstdio>#include<cstring>#defineN 3010using namespacestd;structedge{intTo,next;} E[n*2],d[n*2];intHead[n],h[n];intW[n],w[n];BOOLInstack[n];intLow[n],dfn[n],stack[n];intCost[n],min[n];intBelong[n],rudu[n];intIndex,scc,cnt,top,r;intn,p,x,y,u,v;voidInsintUintv) {e[++cnt].to = v; E[cnt].next = Head[u]; Head[u] =CNT;}voidTarjan (intk) {Dfn[k]= Low[k] = + +Index; stack[++top] =K; INSTACK[K]=true; for(intI=head[k];i;i =E[i].next) { intv =e[i].to; if(Instack[v]) low[k]=min (low[k],dfn[v]); Else if(!Dfn[v]) {Tarjan (v); LOW[K]=min (low[k],low[v]); } } if(Dfn[k] = =Low[k]) {SCC++; intnow =0; while(Now! =k) { now= stack[top--]; Belong[now]=SCC; Instack[now]=false; if(W[now]) {W[SCC]=min (W[scc],w[now]); } MIN[SCC]=min (min[scc],now); } }}intMain () {memset (W,127,sizeof(W)); memset (MIN,127,sizeof(MIN)); scanf ("%d%d",&n,&p); for(intI=1; i<=p;i++) {scanf ("%d%d",&x,&y); W[X]=y; } scanf ("%d",&R); for(intI=1; i<=r;i++) {scanf ("%d%d",&u,&v); Ins (u,v); } for(intI=1; i<=n;i++) if(!Dfn[i]) Tarjan (i); CNT=0; for(intI=1; i<=n;i++) { for(intj = Head[i];j;j =E[j].next) { intv =e[j].to; if(Belong[i]! =Belong[v]) {d[++cnt].to =Belong[v]; D[cnt].next=H[belong[i]]; H[belong[i]]= ++CNT; RUDU[BELONG[V]]++; } } } intPd=0, failmin=6666666, ans =0; for(intI=1; i<=scc;i++) { if(Rudu[i] = =0) { if(W[i] = =2139062143) {PD=1; Failmin=min (failmin,min[i]); } ans+=W[i]; } } if(PD = =1) {printf ("no\n"); printf ("%d", failmin); return 0; } printf ("yes\n%d", ans);}
"CodeVS4093" EZ's Spy Network