[Bzoj 1565] [NOI 2009] Plants vs Zombies (Dinic Max Stream + topological sort)

Source: Internet
Author: User

Title Link: http://www.lydsy.com:808/JudgeOnline/problem.php?id=1565

Orz topic too god ... Knees have been kneeling ....

This problem requires us to establish a complex protective relationship between plants and plants, as well as the loss and reward of eating plants, so we have to use the maximum flow, modeling is amazing, kneeling on your knees ...

First we establish source and sink points, and for each plant, if it can get energy, it has an edge between the source point and it, and the capacity is the amount of energy to get. If it consumes energy, it creates an edge between it and the meeting point, which is the number of energy consumed.

Then for each plant, it is connected to each plant in its protective area, with an infinite capacity.

Then note that the last point of each line and the next line of the first point to connect an edge, the capacity of infinity, so as to ensure that the good picture is unicom.

Considering that there may be some plants that can protect each other, so that zombies are not able to eat them, in this case the plants will be linked to a ring, so then the topological order, the diagram of all the rings are eliminated.

Last dinic run Max stream, answer = All plants not belonging to the ring on the graph weight-maximum flow

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #include <queue> #define Maxe 500500#define maxv 650#define INF 0x3f3f3f3fusing namespace Std;int ans= 0,f[maxv],s,t,n,m;bool USED[MAXV]; Used[i]=true indicates that point I was not removed int INDEGREE[MAXV]; The penetration of each point//start of Graph structurestruct edge{int u,v,cap,next;}    Edges[maxe];int head[maxv],ncount=-1;void addedge (int u,int v,int C) {edges[++ncount].u=u;    Edges[ncount].v=v;    Edges[ncount].cap=c;    Edges[ncount].next=head[u]; Head[u]=ncount;}    void Add (int u,int v,int C) {Addedge (u,v,c);    Addedge (v,u,0); indegree[u]++;}    End of Graph Structure//start of topological sortvoid Toposort () {queue<int>q;    while (!q.empty ()) Q.pop ();    for (int i=s;i<=t;i++) if (!indegree[i]) Q.push (i);        while (!q.empty ()) {int U=q.front ();        Q.pop ();        Used[u]=true;        if (f[u]>0) ans+=f[u]; for (int p=head[U];p!=-1;p=edges[p].next) {int v=edges[p].v;                if (p&1) {indegree[v]--;            if (!indegree[v]) Q.push (v); }}}}//end of topological sort//start of Dinic Max-flow Algorithmint LAYER[MAXV];    BFS-separated layer bool Countlayer () {memset (layer,0,sizeof (layer));    queue<int>q;    while (!q.empty ()) Q.pop ();    Q.push (S);    Layer[s]=1;        while (!q.empty ()) {int U=q.front ();        Q.pop ();            for (int p=head[u];p!=-1;p=edges[p].next) {int v=edges[p].v;                if (!layer[v]&&edges[p].cap&&used[v]) {layer[v]=layer[u]+1;                Q.push (v);            if (v==t) return true; }}} return false;}    int DFS (int u,int flow) {int tmp=flow;    if (u==t) return flow;        for (int p=head[u];p!=-1;p=edges[p].next) {int v=edges[p].v; if (edges[p].cap&&layer[v]==layer[U]+1&AMP;&AMP;TMP) {int Now=dfs (v,min (Tmp,edges[p].cap));            if (!now) layer[v]=0;            Tmp-=now;            Edges[p].cap-=now;        Edges[p^1].cap+=now; }} return flow-tmp;}    int dinic () {int maxflow=0;    while (Countlayer ()) Maxflow+=dfs (S,inf); return maxflow;}    End of Dinic Max-flow Algorithmint main () {memset (head,-1,sizeof (head));    int x,y,r,c;    scanf ("%d%d", &m,&n);    s=0,t=n*m+1;        for (int i=1;i<=m*n;i++) {scanf ("%d", &f[i]);        if (f[i]>0)//Eat the plant can get energy add (s,i,f[i]);        else//Eat the plant to pay the energy add (i,t,-f[i]); scanf ("%d", &y);            Attack range for (int j=1;j<=y;j++) {scanf ("%d%d", &r,&c); Add (R*n+c+1,i,inf);    From the protected plant to the plant even an infinitely large edge} if (i%n) Add (I,i+1,inf);    } toposort ();    cout << ans-dinic () << Endl; return 0;}






[Bzoj 1565] [NOI 2009] Plants vs Zombies (Dinic Max Stream + topological sort)

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.