博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3046 Pleasant sheep and big big wolf 最小割
阅读量:4879 次
发布时间:2019-06-11

本文共 2927 字,大约阅读时间需要 9 分钟。

题目链接:

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
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
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
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 }

 

转载于:https://www.cnblogs.com/huangxf/p/4301352.html

你可能感兴趣的文章
利用redis完成自动补全搜索功能(一)
查看>>
云计算openstack介绍(1)
查看>>
android学习笔记三
查看>>
产品设计-流程以及各阶段产出
查看>>
文件下载
查看>>
把Paul Pauline pual Paula Paul中的Paul替换成Ringo
查看>>
poj2404中国邮递员
查看>>
扯蛋css
查看>>
[洛谷P4149][IOI2011]Race
查看>>
关于多线程编程的一点思考
查看>>
Jquery好友选择器
查看>>
Kissy && Require
查看>>
JavaScript和DOM的产生与发展
查看>>
java中org.xml.sax不能读取xml回车换行的问题解决(android)
查看>>
ORACLE10.2_X64安装
查看>>
Servlet使用注解标注监听器(Listener)
查看>>
复利计算--web版--总结--软件工程
查看>>
Poj 1002 487-3279(二叉搜索树)
查看>>
Facebook 09年营收7.77亿美元 利润2亿美元
查看>>
Groupon中国网站高朋有望本周重新上线
查看>>