Hdoj 3046 pleasant sheep and Big big Wolf "min cut"

Source: Internet
Author: User



Pleasant sheep and Big big wolfTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2570 Accepted Submission (s): 1056


Problem DescriptionIn Zjnu, there is a well-known prairie. And it attracts pleasant sheep and his companions to has a holiday. Big Big Wolf and his families know on this, and quietly hid in the big lawn. As Zjnu ACM/ICPC team, we have a obligation to protect pleasant sheep and he companions to free from being disturbed by Big Big Wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask for the minimum fences to let pleasant sheep and he companions to free from being disturbed by Big BIG W Olf and his companions.


Inputthere is many cases.
For every case:

N and M (n,m<=200)
Then N*m matrix:
0 is empty, and 1 are pleasant sheep and his companions, 2 are big Big Wolf and his companions.

Outputfor every case:

First line output ' case p: ', p is the p-th case;
The second line is the answer.

Sample Input
4 61 0 0 1 0 00 1 1 0 0 02 0 0 0 0 00 2 0 1 1 0

Sample Output
Case 1:4


Build a plan to dare to think, dare to write. WA is much more, perhaps understanding deeper. WA two times still a bit of harvest.


Test instructions: give you a n*m map with only 3 numbers, 0, 1, 2, respectively. 0 means empty here, 1 means there are 1 sheep here, and 2 means there is a wolf here. To protect the sheep from being eaten by wolves, we need to build fences to block some of the paths. The length of a fence is 1, which corresponds to the path length between two adjacent points in the graph. Now ask you the minimum length of the fence (or, in other words, the number of points blocked).



Build: Set the super source point sink, the super sink point source.

1,sink to all the sheep, the capacity of the INF, indicating that the capacity can not be blocked here (if there is a fixed value, the minimum cut edge set may have sink to the edge of the sheep, this is obviously wrong).

2, adjacent points are built with each other, with a capacity of 1. Represents the cost of blocking the edge.

3, all wolves build edge to source, capacity is INF. Indicates that traffic cannot be blocked here.

After the diagram is completed, if there is traffic flowing into the meeting point, it means that the sheep will be eaten, so our aim is to block all viable flows of the sink->source at the lowest possible cost. This problem becomes the smallest cut to solve the sink->source.


#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector># Include <algorithm> #define MAXN 40000+10#define maxm 1000000+10#define INF 0x3f3f3f3fusing namespace Std;struct edge{int from, to, cap, flow, next;}; Edge Edge[maxm];int HEAD[MAXN], edgenum;int dist[maxn];int cur[maxn];bool vis[maxn];int N, M;int sink, source;void init ()    {edgenum = 0; Memset (Head,-1, sizeof (head));} int point (int x, int y) {return (x-1) * M + y;}    void Addedge (int u, int v, int w) {Edge E1 = {u, V, W, 0, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;} BOOL Judge (int x, int y) {return x >= 1 && x <= N && y >= 1 && y <= M;}    void Getmap () {int A;    Sink = 0, Source = N * M + 1;    int move[4][2] = {0, 1, 0,-1, 1, 0, -1,0};    for (int i = 1, i <= N; i++) {for (int j = 1; J <= M; j + +) {        scanf ("%d", &a);                for (int p = 0; p < 4; p++) {int x = i + move[p][0];                int y = j + move[p][1];            if (judge (x, Y)) Addedge (Point (I, J), point (x, y), 1);                } if (a = = 1)//Goat Addedge (sink, point (I, J), INF);//Even the super source, else if (a = = 2)//Wolf    Addedge (Point (I, J), source, INF);//Even Super sinks}}}bool BFS (int s, int t) {queue<int> Q;    memset (Dist,-1, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i];                if (!vis[e.to] && e.cap > E.flow) {dist[e.to] = Dist[u] + 1;                if (e.to = = t) return true;                Vis[e.to] = true;            Q.push (e.to);    }    }} return false;}    int DFS (int x, int a, int t) {if (x = = T | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; i =-1; i = Edge[i].next) {Edge &e = Edge[i]; if (dist[e.to] = = Dist[x] + 1 && (f = DFS (e.to, Min (A, e.cap-e.flow), T)) > 0) {edge[i].flow            + = f;            Edge[i^1].flow-= f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;}    int Maxflow (int s, int t) {int flow = 0;        while (BFS (s, t)) {memcpy (cur, head, sizeof (head));    Flow + = DFS (s, INF, T); } return flow;}    int main () {int k = 1;        while (scanf ("%d%d", &n, &m)! = EOF) {init ();        Getmap ();    printf ("Case%d:\n%d\n", k++, Maxflow (sink, source)); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 3046 pleasant sheep and Big big Wolf "min cut"

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.