POJ 2195 Going Home
来源:互联网 发布:js classname事件 编辑:程序博客网 时间:2024/06/04 19:06
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15130 Accepted: 7740
Description
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
Source
Pacific Northwest 2004
考察点: 最小费用最大流
#include <stdio.h>#include <string.h>#include <math.h>struct num{ int x,y;}h[200],m[200];int cost[300][300],cap[300][300];int queue[1000000],status[300],dis[300],pre[300];int INF=0x7fffffff,sta,end;int main(){ int EK(); int i,j,n,u,s,t,top1,top2,x1,y1,x2,y2; char c; while(scanf("%d %d%*c",&n,&u)!=EOF) { if(n==0&&u==0) { break; } top1=top2=0; for(i=1;i<=n;i++) { for(j=1;j<=u;j++) { scanf("%c",&c); if(c=='H') { h[top1].x=i; h[top1++].y= j; }else if(c=='m') { m[top2].x= i; m[top2++].y= j; } } getchar(); } if(top1==0) { printf("0\n"); continue; } memset(cap,0,sizeof(cap)); memset(cost,0,sizeof(cost)); n=top1; for(i=1;i<=n;i++) { cap[0][i]=1; } for(i=1;i<=n;i++) { x1=m[i-1].x; y1=m[i-1].y; for(j=1;j<=n;j++) { x2=h[j-1].x; y2=h[j-1].y; x2= x2- x1; if(x2<0) { x2=-x2; } y2=y2-y1; if(y2<0) { y2=-y2; } cap[i][n+j]=1; cost[i][n+j]=(x2+y2); cost[n+j][i]=-(x2+y2); } } for(i=1;i<=n;i++) { cap[n+i][n+n+1]=1; } s=0; sta=0; end=2*n+1; s=EK(); printf("%d\n",s); } return 0;}int bfs(){ int i,j,base,top,x; base=top=0; for(i=0;i<=end;i++) { dis[i]=INF; } memset(status,0,sizeof(status)); queue[top++]=0; status[0]=1; dis[0]=0; while(base<top) { x=queue[base++]; status[x]=0; for(i=1;i<=end;i++) { if(x!=i&&cap[x][i]) { if(dis[i]>(dis[x]+cost[x][i])) { dis[i]=dis[x]+cost[x][i]; pre[i]=x; if(!status[i]) { status[i]=1; queue[top++]=i; } } } } } if(dis[end]==INF) { return 0; }else { return 1; }}int EK(){ int i,j,s=0,k,min=INF; while(1) { k=bfs(); if(k==0) { break; } for(i=end; i!=0; i=pre[i]) { j=pre[i]; if(min>cap[j][i]) { min=cap[j][i]; } } for(i=end; i!=0; i=pre[i]) { j=pre[i]; s+=(cost[j][i]*min); cap[j][i]-=min; cap[i][j]+=min; } } return s;}
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ-2195-Going Home
- poj 2195 Going Home
- POJ 2195 Going Home
- poj 2195 Going Home
- POJ 2195 Going Home
- poj 2195 Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ-2195-Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ 2195 Going Home
- POJ 2195--Going Home
- C# 文件下载四方法
- 过程和函数
- 通用的多线程处理模型
- SQL语句---from 和where 查询条件的优先级
- 屏幕取词技术实现原理与关键源码
- POJ 2195 Going Home
- uva10827 - Maximum sum on a torus(圆环上的最大和)
- 黑马程序员_异常
- T-SQL逻辑查询处理的各个阶段
- C++ queue应用之电路布线(迷宫的最短路径)
- java语言程序设计 14.2 comparable
- mysql数据库复习加强
- 微软2013年暑期实习招聘笔试回忆[转载]
- 正则表达式(二)