Going Home(最小费用最大流模板)
来源:互联网 发布:win7关闭端口批处理 编辑:程序博客网 时间:2024/05/08 08:38
Going Home
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
2 2.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
#include<cstring>#include<iostream>#include<algorithm>#include<cstdio>using namespace std;#define INF 0x3f3f3f3f#define maxn 400000struct node{ int x,y;} A[105],B[105];int n,m;char g[105][105];int fir[maxn],nex[maxn],u[maxn],v[maxn],w[maxn],cap[maxn],flow[maxn];int pre[maxn],vis[maxn],d[maxn],que[2*maxn];int st,ed;int e_max;void init(){ memset(flow,0,sizeof flow); memset(fir,-1,sizeof fir); e_max=0;}void add_edge(int s,int t,int c,int cus){ int e=e_max++; u[e]=s; v[e]=t; cap[e]=c; w[e]=cus; nex[e]=fir[s]; fir[s]=e; e=e_max++; u[e]=t; v[e]=s; cap[e]=0; w[e]=-cus; nex[e]=fir[t]; fir[t]=e;}bool spfa(){ for(int i=0; i<=ed; i++) { vis[i]=0; d[i]=INF; pre[i]=-1; } d[st]=0; int f,r; que[f=r=0]=st; while(f<=r) { int k=que[f++]; vis[k]=0; // cout<<k<<endl; for(int i=fir[k]; ~i; i=nex[i]) { int e=v[i]; if(flow[i]<cap[i]&&d[k]+w[i]<d[e]) { pre[e]=i; d[e]=d[k]+w[i]; if(!vis[e]) { vis[e]=1; que[++r]=e; } } } } if(pre[ed]==-1) return false; return true;}int solve(){ int ans=0; while(spfa()) { int sum=0; int f=pre[ed]; int minn=INF; while(f!=-1) { if(u[f]!=st&&v[f]!=ed) minn=min(minn,cap[f]-flow[f]); f=pre[u[f]]; } f=pre[ed]; while(f!=-1) { sum+=w[f]*minn; flow[f]+=minn; flow[1^f]-=minn; f=pre[u[f]]; } ans+=sum; } return ans;}void build_G(){ int num1=1,num2=1; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(g[i][j]=='m') { A[num1].x=i; A[num1++].y=j; } else if(g[i][j]=='H') { B[num2].x=i; B[num2++].y=j; } } } st=0,ed=num1+num2; for(int i=1; i<num1; i++) { add_edge(st,i,1,0); add_edge(i+num1,ed,1,0); for(int j=1; j<num2; j++) add_edge(i,num1+j,1,abs(A[i].x-B[j].x)+abs(A[i].y-B[j].y)); }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { if(!n&&!m) break; for(int i=0; i<n; i++) scanf("%s",g[i]); init(); build_G(); int ans=solve(); printf("%d\n",ans); } return 0;}
0 0
- Going Home(最小费用最大流模板)
- POJ 2195 Going Home -- 最小费用最大流模板
- HDU 1533--Going Home【最小费用最大流 && 模板】
- poj-2195-Going Home(最小费用最大流模板题)
- 【POJ2195】Going Home-最小费用最大流模板题
- POJ 2195 Going Home 最小费用最大流(模板题)
- POJ2195 Going Home 最小费用最大流
- poj2195 Going Home,最小费用最大流
- poj2195--Going Home(最小费用最大流)
- poj2195 Going Home 最小费用最大流
- 【最小费用最大流】【HDU1533】【Going Home】
- Going Home--最小费用最大流
- Going Home hdu 1533 最小费用最大流,费用流好用模板
- HDU 1533 Going Home 最小费用最大流(入门)(模板)或者 二分匹配
- POJ 2195 - Going Home 更新最小费用最大流模板..整合成结构体...
- Going Home----最小费用流
- Going Home(最小费用流)
- hdu 1533 Going Home【最大流最小费用流】
- Android端使用retrofit上传多张图片,后端使用Php接收图片
- 无聊时总结总结算法之06归并排序
- Android 启动模式和启动标志的关系以及各种适应的使用场合
- 「深入Java」Annotation注解
- HttpServletRequest
- Going Home(最小费用最大流模板)
- CSS3 去除苹果浏览器按钮input[type="submit"]和input[type="reset"]的默认样式
- 2.7-3 如何将Eclipse 项目导入Android studio中
- 算法相关
- Java排序算法:希尔排序
- 「译」Android文本测量
- VM 中Centos7 增加/目录容量
- CALayer 锚点
- Date和String类型相互转化