hdu 3046 Pleasant sheep and big big wolf 最小割

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046

In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his 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 to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.

题目描述:在一个单位方格边长为1的矩阵中藏着灰太狼和它的同伴,等待着喜羊羊和它的同伴,为了不让喜羊羊和同伴被抓住,我们可以在矩形草坪中设置单位长度为1的栅栏,求最短的栅栏长度。

算法分析:最小割模型。新增源点和汇点分别为from和to,源点到喜羊羊和同伴连边,权值为inf,每只狼到汇点连边,权值为inf,然后每个方格到相邻格子连边,权值为1即可。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #define inf 0x7fffffff
  9 using namespace std;
 10 const int maxn=200*200+10;
 11 int n,m;
 12 struct node
 13 {
 14     int u,flow;
 15     int next;
 16 }edge[maxn*4];
 17 int head[maxn],edgenum;
 18 int from,to;
 19
 20 void add(int u,int v,int flow)
 21 {
 22     edge[edgenum].u=v ;edge[edgenum].flow=flow;
 23     edge[edgenum].next=head[u];
 24     head[u]=edgenum++;
 25
 26     edge[edgenum].u=u ;edge[edgenum].flow=0;
 27     edge[edgenum].next=head[v];
 28     head[v]=edgenum++;
 29 }
 30
 31 int d[maxn];
 32 int bfs()
 33 {
 34     memset(d,0,sizeof(d));
 35     d[from]=1;
 36     queue<int> Q;
 37     Q.push(from);
 38     while (!Q.empty())
 39     {
 40         int u=Q.front() ;Q.pop() ;
 41         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 42         {
 43             int v=edge[i].u;
 44             if (!d[v] && edge[i].flow>0)
 45             {
 46                 d[v]=d[u]+1;
 47                 Q.push(v);
 48                 if (v==to) return 1;
 49             }
 50         }
 51     }
 52     return 0;
 53 }
 54
 55 int dfs(int u,int flow)
 56 {
 57     if (u==to || flow==0) return flow;
 58     int cap=flow;
 59     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 60     {
 61         int v=edge[i].u;
 62         if (d[v]==d[u]+1 && edge[i].flow>0)
 63         {
 64             int x=dfs(v,min(cap,edge[i].flow));
 65             cap -= x;
 66             edge[i].flow -= x;
 67             edge[i^1].flow += x;
 68             if (cap==0) return flow;
 69         }
 70     }
 71     return flow-cap;
 72 }
 73 int dinic()
 74 {
 75     int sum=0;
 76     while (bfs()) sum += dfs(from,inf);
 77     return sum;
 78 }
 79 int main()
 80 {
 81     int ncase=1;
 82     while (scanf("%d%d",&n,&m)!=EOF)
 83     {
 84         memset(head,-1,sizeof(head));
 85         edgenum=0;
 86         from=n*m+1;
 87         to=from+1;
 88         int a;
 89         for (int i=1 ;i<=n ;i++)
 90         {
 91             for (int j=1 ;j<=m ;j++)
 92             {
 93                 scanf("%d",&a);
 94                 if (a==1)
 95                 {
 96                     add(from,(i-1)*m+j,inf);
 97                 }
 98                 else if (a==2)
 99                 {
100                     add((i-1)*m+j,to,inf);
101                 }
102                 if (i-1>=1) add((i-1)*m+j,(i-2)*m+j,1);
103                 if (i+1<=n) add((i-1)*m+j,i*m+j,1);
104                 if (j-1>=1) add((i-1)*m+j,(i-1)*m+j-1,1);
105                 if (j+1<=m) add((i-1)*m+j,(i-1)*m+j+1,1);
106             }
107         }
108         printf("Case %d:\n%d\n",ncase++,dinic());
109     }
110     return 0;
111 }
时间: 2024-10-05 17:28:43

hdu 3046 Pleasant sheep and big big wolf 最小割的相关文章

HDU 3046 Pleasant sheep and big big wolf(最小割)

HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊,2是狼,问最少要多少围墙才能把狼全部围住,每有到达羊的路径 思路:有羊和狼,要分成两个集合互不可达,显然的最小割,建图源点连狼,容量无穷,羊连汇点,容量无穷,然后相邻格子连边,容量为1 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm

HDU 3046 Pleasant sheep and big big wolf

Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 304664-bit integer IO format: %I64d      Java class name: Main In ZJNU, there is a well-known prairie. And it attracts pleasant

hdoj 3046 Pleasant sheep and big big wolf 【最小割】

Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2570    Accepted Submission(s): 1056 Problem Description In ZJNU, there is a well-known prairie. And it attracts

Pleasant sheep and big big wolf (hdu 3046 最小割)

Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2371    Accepted Submission(s): 988 Problem Description In ZJNU, there is a well-known prairie. And it attracts p

Pleasant sheep and big big wolf

点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼,每个格子只能存在0或1只动物.现在要用栅栏将所有的狼和羊分开,问怎么放,栅栏数放的最少,求出个数? 解析:将狼群看作一个集合,羊群看作一个集合.然后设置源点和汇点,将两点至存在动物的点的距离赋值为1,构图,由于求得是栅栏数,从存在动物的位置向四周发散点赋值为1,即该方向放置一个栅栏.然后可以发现变成了求最小割,即求出最大流.需要注意的是,由于数据比较大,200 * 200.如果设置源点和汇点相差较大(即s = 0,e = n *

HDU 2435 There is a war (网络流-最小割)

There is a war Problem Description There is a sea. There are N islands in the sea. There are some directional bridges connecting these islands. There is a country called Country One located in Island 1. There is another country called Country Another

hdu 5294 Tricks Device 最短路建图+最小割

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 375    Accepted Submission(s): 98 Problem Description Innocent Wu follows Dumb Zh

HDU 1565 &amp;&amp; HDU 1569 方格取数 (网络流之最小割)

题目地址:HDU 1565       HDU 1569 刚开始接触最小割,就已经感受到了最小割的博大精深... 这建图思路倒是好想..因为好多这种关于不相邻的这种网络流都是基本都是这样建图.但是感觉毫无道理可言...看了题解后才明白这样做的意义. 下面是题解中的说法. 大概是这样分析的,题义是要我们求在一个方格内取出N个点,使得这N个独立的(不相邻)点集的和最大.我们可以将问题转化为最小割来求解.首先,我们将方格进行黑白相间的染色,然后再将任意一种颜色(黑色)作为源点,一种颜色(白色)作为汇点

HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)

题目地址:HDU4859 做了做杭电多校,知识点会的太少了,还是将重点放在刷专题补知识点上吧,明年的多校才是重点. 这题题目求的最长周长,可以试想一下,这里的海岸线一定是在"."和"D"之间的,也就是说求最多的相邻的"."和"D"的配对对数.可以先转化成最小割求最小配对对数,因为总对数是一定的,只需要减去就行. 要先对周围填充上一圈的"D",然后变成了一个(n+2)*(m+2)的矩形.因为要求的都是相邻的匹