Hdoj--3046--pleasant sheep and Big Big Wolf (min cut Classic)

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): 2699 Accepted Submission (s): 1114


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


Test instructions: n*m in Figure 1 means sheep, 2 means wolves, wolf and sheep isolation, need to build a fence, each fence requires 1 of the cost, ask the minimum cost of what is required

Did not learn the smallest cut before I really do not understand Ah, the establishment of super-source points and super sinks, super source point even all the sheep, super sinks even all the wolves, Benquan are INF said that the point can not be deleted, wolves and sheep can be built around the edge, the right side of 1, is also spent for 1,0 can be used as the middle node The problem becomes the cost of blocking all the wolves.

#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 source, sink;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;    Source = 0, sink = 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)//?            Addedge (source, point (I, J), INF);//?????                else if (a = = 2)//?        Addedge (Point (I, J), Sink, INF);//?????    }}}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 (source, sink)); } return 0;}


Hdoj--3046--pleasant sheep and Big Big Wolf (min cut Classic)

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.