[bzoj1565] [NOI2009] Plants vs Zombies _ Network Flow _ topological sorting

Source: Internet
Author: User

Plants vs Zombies bzoj1565

The main idea is to give you a grid chart with some plants planted on it. You start attacking from the far right of the grid. Each plant can provide energy to zombies or consume zombies ' energy. Each plant can protect a plant within a particular grid, and if a plant is protected, then if the zombie wants to eat the plant it must first eat the plants that protect it. Q: The maximum amount of energy a zombie can get.

Note: The 1<=n (width of the grid) <=20,1<=m (the length of the mesh) <=30,-20,000<= the cost and revenue <=20,000.

Idea: Pre-title ([NOI2006] maximum profit). This problem is similar to the biggest profit, if not careful observation may even think that tmccf out a problem? However, we find that this algorithm is completely wrong, because if a plant protects B plants, b plants also protect a plant, then these two plants are invincible. The definition of invincibility is that both plants will have no problem from the start of the attack to the end of the attack. That is, if there is a ring, all the plants within the entire ring are absolutely invincible. After .... TM, what's the whole JB? Cough, these invincible little things do not have any use, delete it. How to delete it? Topological sequencing Ah! Use Toposort to delete a plant that is unlikely to provide a benefit or a price, and then bare the maximum right to close the sub-graph.

Finally, attach the ugly code ...

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Queue> #define INF 1000000000#define ll long Long#define p (x, y) (x-1) *m+yusing namespace Std;int n,m,t;int tot,top,cnt, Ans;int v[605],cur[605],h[605],d[605],q[605];bool del[605];struct edge{int to,next,v;} E[400005],ed[800005];int last[605],last2[605];void Insert (int u,int v) {D[v]++;e[++cnt].to=v;e[cnt].next=last[u]; last[u]=cnt;} void Insert2 (int u,int v,int W) {ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;ed[cnt].v=w;ed[++cnt].to=u;ed[ cnt].next=last2[v];last2[v]=cnt;ed[cnt].v=0;} void Dfs (int x) {del[x]=1;for (int i=last[x];i;i=e[i].next) if (!del[e[i].to]) DFS (e[i].to); void Topsort () {for (int i=1;i<=n*m;i++) if (!d[i]) Q[++top]=i;else del[i]=1;while (top) {int now=q[top];d el[now]=0; top--;for (int i=last[now];i;i=e[i].next) {d[e[i].to]--;if (!d[e[i].to]) q[++top]=e[i].to;}} for (int i=1;i<=n*m;i++) if (Del[i]) DFS (i);} void Rebuild () {cnt=1; t=n*m+1;for (int x=1;x<=n*m;x++) if (!del[x]) {iF (v[x]>0) Tot+=v[x],insert2 (x,t,v[x]), Else Insert2 (0,x,-v[x]); for (int i=last[x];i;i=e[i].next) if (!del[e[i].to]  ) Insert2 (X,e[i].to,inf);} }bool BFS () {int head=0,tail=1;for (int i=0;i<=t;i++) H[i]=-1;h[0]=0;q[0]=0;while (head!=tail) {int Now=q[head];head ++;for (int i=last2[now];i;i=ed[i].next) if (ed[i].v&&h[ed[i].to]==-1) {h[ed[i].to]=h[now]+1;q[tail++]=ed[i ].to;}} return h[t]!=-1;} int dfs (int x,int f) {if (x==t) return f;int w,used=0;for (int i=cur[x];i;i=ed[i].next) if (h[ed[i].to]==h[x]+1) {W=dfs (ed[ I].to,min (ed[i].v,f-used)); Ed[i].v-=w;ed[i^1].v+=w;if (ED[I].V) cur[x]=i;used+=w;if (used==f) return F; if (!used) H[x]=-1;return used;} void Dinic () {while (BFS ()) {for (int i=0;i<=t;i++) Cur[i]=last2[i];ans+=dfs (0,inf);}} int main () {scanf ("%d%d", &n,&m), for (Int. i=1;i<=n;i++) for (int j=1;j<=m;j++) {scanf ("%d", &v[p (I,J)] int x;scanf ("%d", &x), while (x--) {int a,b;scanf ("%d%d", &a,&b), A++;b++;insert (P (i,j), p (A, b));}} for (int i=1;i<=n;i++) for (int j=m;j>1;j--) insert (P (i,j), p(i,j-1)); Topsort (); rebuild ();d inic ();p rintf ("%d", Tot-ans); return 0;}

Summary: Network flow True TM interesting

[bzoj1565] [NOI2009] Plants vs Zombies _ Network Flow _ topological sorting

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.