hdu 2732&&poj 2711 (最大流)
2013-08-10 18:29
239 查看
非常好的一道拆点题目,终于学会isap了,递归的,这几天要把非递归实现的敲出来,,
题意:给出蜥蜴可以跳的最大距离,从一根柱子跳向可以到达的任意柱子,每个柱子有高度,蜥蜴没跳一次柱子会下降1米,问有多少蜥蜴跳不出所给的图。
建图:源点与每个蜥蜴所在的柱子建边流量为1,应为柱子间可以互相达到的,所以每个柱子应该拆成两个点,每根柱子相当于一条边,流量为柱子的高度,没两个可以互相到达的柱子间建边(一根柱子的终点到另一根柱子的起点)流量为无穷大,然后每根柱子的终点跟汇点建边流量为柱子的高度。
题意:给出蜥蜴可以跳的最大距离,从一根柱子跳向可以到达的任意柱子,每个柱子有高度,蜥蜴没跳一次柱子会下降1米,问有多少蜥蜴跳不出所给的图。
建图:源点与每个蜥蜴所在的柱子建边流量为1,应为柱子间可以互相达到的,所以每个柱子应该拆成两个点,每根柱子相当于一条边,流量为柱子的高度,没两个可以互相到达的柱子间建边(一根柱子的终点到另一根柱子的起点)流量为无穷大,然后每根柱子的终点跟汇点建边流量为柱子的高度。
#include<stdio.h> #include<string.h> #define N 1000 #define inf 0x3fffffff int dis ,gap ,head ,num,n,m,D,ans; int G[25][25],start=801,end=802; struct edge { int st,ed,flow,next; }E[N*20]; void addedge(int x,int y,int w) { E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++; E[num].st=y;E[num].ed=x;E[num].flow=0;E[num].next=head[y];head[y]=num++; } int abs(int a) { if(a>0)return a; return -a; } bool judge(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m) { return false; } else if (!G[x][y]) { return false; } return true; } bool out(int x,int y)//判断该柱子上的蜥蜴是否能跳出去 { if(x-D<0||x+D>=n||y-D<0||y+D>=m) return true; return false; } void makemap(int x,int y) { int nn=x*m+y; addedge(nn,nn+400,G[x][y]);//柱子拆点 if(out(x,y))//如果可以跳出就不跳到别的柱子上了 { addedge(nn+400,end,G[x][y]); return ; } for (int i = -D; i <= D; ++i) for (int j = -(D-abs(i)); j <= (D-abs(i)); ++j) { int xx = x + i, yy = y + j; if (judge(xx, yy) && !(x == xx && y == yy)) addedge(nn+400, xx*m+yy, inf); } } int dfs(int u,int minflow) { if(u==end)return minflow; int i,v,f,min_dis=ans-1,flow=0; for(i=head[u];i!=-1;i=E[i].next) { if(E[i].flow) { v=E[i].ed; if(dis[u]==dis[v]+1) { f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow); E[i].flow-=f; E[i^1].flow+=f; flow+=f; if(flow==minflow)break; if(dis[start]>=ans) return flow; } min_dis=min_dis>dis[v]?dis[v]:min_dis; } } if(flow==0) { if(--gap[dis[u]]==0) dis[start]=ans; dis[u]=min_dis+1; gap[dis[u]]++; } return flow; } int isap() { int maxflow=0; memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); gap[0]=ans;//点的个数 while(dis[start]<ans) maxflow+=dfs(start,inf); return maxflow; } int main() { int i,j,sum,t,op=1; char str[25]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&D); memset(head,-1,sizeof(head)); num=0;ans=2;sum=0; for(i=0;i<n;i++) { scanf("%s",str); for(j=0;str[j];j++) G[i][j]=str[j]-'0'; } m=strlen(str); for(i=0;i<n;i++) for(j=0;j<m;j++) { if(G[i][j]>0) { makemap(i,j); ans+=2; } } for(i=0;i<n;i++) { scanf("%s",str); for(j=0;j<m;j++) if(str[j]=='L') { sum++; addedge(start,i*m+j,1); } } sum-=isap(); printf("Case #%d: ",op++); if(sum==0)//输出还得考虑单复数问题, puts("no lizard was left behind."); else if(sum==1) printf("%d lizard was left behind.\n", sum); else printf("%d lizards were left behind.\n", sum); } return 0; }
相关文章推荐
- HDU 2732&&POJ 2711 Leapin' Lizards 【最大流Dinic】
- hdu 2732 && poj 2711 Leapin' Lizards(dinic && 拆点建图经典)
- POJ 1273 && HDU 1532 Drainage Ditches(最大流-Dinic)
- poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流
- HDU 1081 & POJ 1050 To The Max (最大子矩阵和)
- hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
- poj 1466 && hdu 1068Girls and Boys(最大独立集)
- Poj 3692 & Hdu 2458 (08 合肥Online 二分图 最大团)
- POJ 3281 Dining & HDU 4292 Food【最大流】
- POJ 1964&HDU 1505&HOJ 1644 City Game(最大0,1子矩阵和总结)
- hdu 1055 & poj 2054 Color a Tree 树&贪心 找最大费用点和父节点合并
- POJ 2711 - Leapin' Lizards 拆点构图最大流..
- hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
- HDU 2413 & POJ 3343 Against Mammoths【最大匹配+二分】
- HDU 3934 Summer holiday & Poj 2079 Triangle(凸包最大内接三角形)
- poj 1273 & hdu 1532 Drainage Ditches(最大流 )EK,dinic模板
- poj 1469&&hdu 1803 Courses(最大匹配数)
- POJ 3498 && HDU 2334 March of the Penguins(最大流-Dinic)