B2OJ_1565_[NOI2009] Plants vs. Zombies _ topological sort + Maximum weight closure
Test instructions: n*m plants, each plant has fractions (which can be negative), and can protect the location of the plant. Can only eat from the right to the left, and can not eat is being protected, you may not eat, to obtain the maximum score.
Analysis: Connect each plant to a plant that can protect it. The source point is connected with a positive point, and a negative weight point is connected to a meeting point.
Considering that plants on a ring are not edible, we can use topological sequencing to determine which ones are edible.
And then ask for the maximum right to close the sub-graph is the answer.
Code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <queue>5 using namespacestd;6 #defineS (n*m+1)7 #defineT (n*m+2)8 #defineP (x, y) (m* (x-1) +y)9 #defineINF 100000000Ten inthead[ the],to[1000010],nxt[1000010],flow[1000010],c[1000010],cnt=1, dep[ the]; One intn,m,can[ the],sum,val[ the]; A voidAddintUintVintf) - { -to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;flow[cnt]=F; theto[++cnt]=u;nxt[cnt]=head[v];head[v]=cnt;flow[cnt]=0; - } - BOOLBFS () - { +Queue <int>Q; -memset (DEP,0,sizeof(DEP)); +Q.push (S);d ep[s]=1; A while(Q.size ()) at { - intx=Q.front (); Q.pop (); - for(intI=head[x];i;i=Nxt[i]) - { - if(!dep[to[i]]&&flow[i]&&Can[to[i]]) - { indep[to[i]]=dep[x]+1; - Q.push (To[i]); to } + } - } the returnDep[t]; * } $ intDfsintXintMF)Panax Notoginseng { - if(x==t)returnMF; the intnf=0; + for(intI=head[x];i;i=Nxt[i]) A { the if(dep[to[i]]==dep[x]+1&&flow[i]&&Can[to[i]]) + { - intTmp=dfs (To[i],min (flow[i],mf-NF)); $nf+=tmp; $flow[i]-=tmp; -flow[i^1]+=tmp; - if(NF==MF) Break; the } - }Wuyidep[x]=0; the returnNF; - } Wu voiddinic () - { About intans=0, F; $ while(BFS ()) - { - while(f=DFS (s,inf)) - { Aans+=F; + } the } -printf"%d", sum-ans); $ } the voidTopsort () the { theQueue <int>Q; the for(intI=1; i<=t;i++) - { in if(!C[i]) Q.push (i); the } the while(Q.size ()) About { the intx=Q.front (); Q.pop (); thecan[x]=1; the if(val[x]>0) sum+=Val[x]; + for(intI=head[x];i;i=Nxt[i]) - { thec[to[i]]--;Bayi if(c[to[i]]==0) Q.push (To[i]); the } the } - } - intMain () the { thescanf"%d%d",&n,&m); the intx,y,z,w; the for(intI=1; i<=n*m;i++) - { thescanf"%d%d",&x,&y); theval[i]=x; the if(x>0) Add (s,i,x), c[s]++;94 ElseAdd (i,t,-x), c[i]++; the if(i%m) Add (i,i+1, INF), c[i]++; the while(y--) the {98scanf"%d%d",&z,&W); AboutAdd (P (z+1, w+1), I,inf); -C[p (z+1, w+1)]++;101 }102 }103 Topsort ();104 dinic (); the }106 107 /***************************************************************108 problem:1580109 user:20170105 the language:c++111 result:accepted the time:320 Ms113 memory:16704 KB the ****************************************************************/
B2OJ_1565_[NOI2009] Plants vs. Zombies _ topological sort + Maximum weight closure sub-graph