HDU 3046 Pleasant sheep and big big wolf 最小割==最大流(喜羊羊)
2013-08-02 17:30
232 查看
点击打开链接
Total Submission(s): 1711 Accepted Submission(s): 739
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
There are many cases.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
[align=left]Output[/align]
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
[align=left]Sample Input[/align]
4 6
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
[align=left]Sample Output[/align]
Case 1:
4
[align=left]Source[/align]
2009 Multi-University Training Contest 14 - Host by ZJNU
[align=left]Recommend[/align]
gaojie
题意是说有狼和羊在一个图上,让你求最少栏杆使狼和羊分开。既求最小割,也是最大流。此题难在建图上,将狼和源点相连,容量为inf,羊和汇点相连容量为inf,点和周围四个点相连(周围有四个点的,即要判断)相连,容量是1.然后求最大流即可。
Pleasant sheep and big big wolf
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1711 Accepted Submission(s): 739
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
There are many cases.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
[align=left]Output[/align]
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
[align=left]Sample Input[/align]
4 6
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
[align=left]Sample Output[/align]
Case 1:
4
[align=left]Source[/align]
2009 Multi-University Training Contest 14 - Host by ZJNU
[align=left]Recommend[/align]
gaojie
题意是说有狼和羊在一个图上,让你求最少栏杆使狼和羊分开。既求最小割,也是最大流。此题难在建图上,将狼和源点相连,容量为inf,羊和汇点相连容量为inf,点和周围四个点相连(周围有四个点的,即要判断)相连,容量是1.然后求最大流即可。
#include<stdio.h> #include<string.h> #define inf 99999999 #define MIN(a,b) a>b?b:a; struct E { int v,w,next; E() {} E(int v,int w,int next):v(v),w(w),next(next) {} } edg[500007]; int dis[50007],gap[50007],list[50007],nodes; int n,m; int map[207][207]; int sourse,sink,nn,node,e; void addedge(int u,int v,int w) { edg[nodes]=E(v,w,list[u]); list[u]=nodes++; edg[nodes]=E(u,0,list[v]); list[v]=nodes++; } int dfs(int src,int aug) { if(src==sink)return aug; int left=aug,mindis=nn; for(int j=list[src]; j!=-1; j=edg[j].next) { int v=edg[j].v; if(edg[j].w) { if(dis[v]+1==dis[src]) { int minn=MIN(left,edg[j].w); minn=dfs(v,minn); edg[j].w-=minn; edg[j^1].w+=minn; left-=minn; if(dis[sourse]>=nn)return aug-left; if(left==0)break; } if(dis[v]<mindis) mindis=dis[v]; } } if(left==aug) { if(!(--gap[dis[src]]))dis[sourse]=nn; dis[src]=mindis+1; gap[dis[src]]++; } return aug-left; } int sap(int s,int e) { int ans=0; nn=e+1; memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); gap[0]=nn; sourse=s; sink=e; while(dis[sourse]<nn) ans+=dfs(sourse,inf); return ans; } int main() { int cas=1,e; while(scanf("%d%d",&n,&m)!=EOF) { memset(list,-1,sizeof(list)); nodes=0; int p,s=0,sum=0,maxx=0; sourse=0; e=n*m+1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&map[i][j]); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int ans=(i-1)*m+j; if(i<n)addedge(ans,ans+m,1);//和下面的点相连 if(i>1)addedge(ans,ans-m,1);//和上面的点相连 if(j<m)addedge(ans,ans+1,1);//和右面的点相连 if(j>1)addedge(ans,ans-1,1);//和左面的点相连 if(map[i][j]==1)addedge(ans,e,inf); if(map[i][j]==2)addedge(0,ans,inf); } int anss=sap(0,e); printf("Case %d:\n",cas++); printf("%d\n",anss); } return 0; }
相关文章推荐
- hdu 3046 喜羊羊和灰太狼(最大流最小割初入)
- HDU 1151Air Raid 最小路径覆盖=n-最大匹配量 (第二道二分匹配)
- hdu 4067 最小费用最大流
- HDU 4971 A simple brute force problem.(最小割,最大权闭合图)
- HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)
- hdu 3879(最小割模型求解最大权闭合图)
- HDU 3917 Road constructions(最小割---最大权闭合)
- 树形DP 或 最小顶点覆盖=最大匹配(双向图)(HDU 1053)
- hdu1350——Taxi Cab Scheme——————【最小路径覆盖、最大匹配转化】
- hdu4289——Control(最大流最小割+SAP)
- hdu 3996 Gold Mine【最大权闭包-----最小割最大流Dinic】
- HDU 2063 过山车(匈牙利算法 二分图的最小顶点覆盖 二分图最大匹配)
- 二分图 最小覆盖数 = 最大匹配数、最大独立集 = 总数-最小覆盖集 证明 hdu 1068
- hdu 3374 String Problem(KMP+字符串最小最大表示)
- hdu 6118 度度熊的交易计划 (最小费用最大流
- hdu 1853 Cyclic Tour 求图中不相交的l个环的最小权值:首先变成完全图(i->j=inf),然后取反,求最大权值,最后判断是否所有点都选到
- HDU-1150(求最小覆盖顶点数=二分最大匹配数)
- HDU 3917 Road constructions(最小割---最大权闭合)
- HDU 1151Air Raid 最小路径覆盖=n-最大匹配量 (第二道二分匹配)
- hdu 3251 Being a Hero(最小割,最大权闭包+输出割边)