Bzoj 1565 [NOI2009] Plants vs Zombies

Source: Internet
Author: User

Topology sort + min cut.

Each plant has some plants it protects, and the equivalent of selecting some points is a prerequisite for some other points, the maximum right to close the sub-graph problem.

Found in the map has a ring, so the point in the ring can not be selected, the precondition is the point of the ring is also not selectable, so all the side of the reverse topology, the topology of the point can not be selected.

It would be nice to ignore the optional point after the bare maximum weight closure sub-graph.

The solving problem of a maximum weight closed sub-graph

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intdian=605;8 Const intbian=1000005;9 Const intinf=0x3f3f3f3f;Ten intH[dian],nxt[bian],ver[bian],val[bian],ch[dian],cr[dian]; One intQz[dian],num[dian],dbh[dian][dian],inch[Dian],v[dian]; A intN,m,tot,aa,bb,cc,ans; - ints,t; - intBhintAintb) { the     return(A-1) *m+b; - } - voidADD1 (intAintb) { -Tot++;ver[tot]=b;nxt[tot]=h[a];h[a]=tot;inch[b]++; + } - voidToposort () { +queue<int>Q; A      for(intI=1; i<=n*m;i++) at         if(!inch[i]) - Q.push (i); -      while(!Q.empty ()) { -         intx=Q.front (); - Q.pop (); -v[x]=1; in          for(intI=h[x];i;i=Nxt[i]) { -             inty=Ver[i]; to             inch[y]--; +             if(!inch[y]) - Q.push (y); the         } *     } $ }Panax Notoginseng voidAddintAintBintc) { -tot++;ver[tot]=b;val[tot]=c;nxt[tot]=h[a];h[a]=tot; thetot++;ver[tot]=a;val[tot]=0; nxt[tot]=h[b];h[b]=tot; + } A BOOLTell () { thememset (ch,-1,sizeof(CH)); +queue<int>Q; - Q.push (S); $ch[s]=0; $      while(!Q.empty ()) { -         intt=Q.front (); - Q.pop (); the          for(intI=h[t];i;i=Nxt[i]) -             if(ch[ver[i]]==-1&&Val[i]) {Wuyich[ver[i]]=ch[t]+1; the Q.push (Ver[i]); -             } Wu     } -     returnch[t]!=-1; About } $ intZengintAintb) { -     if(a==T) -         returnb; -     intR=0; A      for(intI=cr[a];i&&b>r;i=Nxt[i]) +         if(ch[ver[i]]==ch[a]+1&&Val[i]) { the             intT=zeng (Ver[i],min (b-r,val[i])); -val[i]-=t,r+=t,val[i^1]+=T; $             if(Val[i]) thecr[a]=i; the         } the     if(!R) thech[a]=-1; -     returnR; in } the intDinic () { the     intR=0, T; About      while(Tell ()) { the          for(intI=1; i<=n*m+2; i++) thecr[i]=H[i]; the          while(t=Zeng (S,inf)) +r+=T; -     } the     returnR;Bayi } the intMain () { thememset (H,0,sizeof(h)); -memset (NXT,0,sizeof(NXT)); -memset (NUM,0,sizeof(num)); theMemsetinch,0,sizeof(inch)); thememset (V,0,sizeof(v)); thetot=0, ans=0; thescanf"%d%d",&n,&m); -s=n*m+1, t=n*m+2; the      for(intI=1; i<=n;i++) the          for(intj=1; j<=m;j++){ thescanf"%d",&aa);94QZ[BH (I,J)]=AA; thescanf"%d",&aa); the              for(intk=1; k<=aa;k++){ thescanf"%d%d",&bb,&cc);98NUM[BH (I,J)]++; AboutDBH[BH (i,j)][num[bh (i,j)]]=bh (bb+1, cc+1); -ADD1 (BH (i,j), BH (bb+1, cc+1));101             }102         }103      for(intI=1; i<=n;i++)104          for(intj=2; j<=m;j++) theADD1 (BH (i,j), BH (i,j-1));106 Toposort ();107memset (H,0,sizeof(h));108memset (NXT,0,sizeof(NXT));109tot=1; the      for(intI=1; i<=n;i++)111          for(intj=1; j<=m;j++) the             if(V[BH (i,j)]) {113                 if(QZ[BH (I,J)]>0){ theans+=QZ[BH (i,j)]; the Add (S,BH (i,j), QZ[BH (I,J)]); the                 }117                 Else if(QZ[BH (I,J)]<0)118Add (BH (i,j), t,-QZ[BH (I,J)]);119                  for(intk=1; K<=num[bh (I,J)];k++) -                     if(V[DBH[BH (i,j)][k])121 Add (DBH[BH (i,j)][k],bh (i,j), INF);122             }123      for(intI=1; i<=n;i++)124          for(intj=1; j<m;j++) the             if(V[BH (I,J)]&&v[bh (i,j+1)])126Add (BH (i,j), BH (i,j+1), INF);127printf"%d", ans-dinic ()); -     return 0;129}

Bzoj 1565 [NOI2009] Plants vs Zombies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.