POJ 2195 Going Home
来源:互联网 发布:软件开发面试职业规划 编辑:程序博客网 时间:2024/06/05 06:57
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15341 Accepted: 7849
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
KM算法模板题
/***************************************************************> File Name: 2195.cpp> Author: SDUT_GYX> Mail: 2272902662@qq.com> Created Time: 2013/5/30 23:17:30 **************************************************************/#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>#include <cstdlib>#include <iomanip>#include <string>#include <vector>#include <map>#include <cmath>#include <stack>#define LL long long#define N 110#define inf 0x7fffffusing namespace std;struct num{int x,y;}peo[N],room[N];char s1[200];int n,m;int a[N][N],lx[N],ly[N],linky[N],slacks[N];bool visitx[N],visity[N];int main(){//freopen("data1.in","r",stdin);void KM();while(scanf("%d %d",&n,&m)!=EOF){if(n==0&&m==0){break;}int n1=0,n2=0;for(int i=0; i<=n-1; i++){scanf("%s",s1);for(int j=0; j<=m-1; j++){char c=s1[j];if(c=='m'){peo[n1].x = i;peo[n1++].y = j;}else if(c=='H'){room[n2].x = i;room[n2++].y = j;}}}n= n1;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int x1 = peo[i-1].x;int y1 = peo[i-1].y;int x2 = room[j-1].x;int y2 = room[j-1].y;a[i][j] = -1*( abs(x2 - x1) +abs(y2 - y1) );}}KM();int s=0;for(int i=1;i<=n;i++){int x = linky[i];int y = i;s+= -1*a[x][y];}printf("%d\n",s);}return 0;}bool find(int k){visitx[k] = true;for(int i=1;i<=n;i++){if(visity[i]){continue;}int t = lx[k] + ly[i] - a[k][i];if(t==0){visity[i] = true;if(!linky[i]||find(linky[i])){linky[i] = k;return true;}}else {slacks[i] = min(slacks[i],t);}}return false;}void KM(){for(int i=1;i<=n;i++){lx[i] = -inf;}memset(ly,0,sizeof(ly));memset(linky,0,sizeof(linky));for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){lx[i] = max(lx[i],a[i][j]);}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){slacks[j] = inf;}while(true){ memset(visitx,false,sizeof(visitx)); memset(visity,false,sizeof(visity));if(find(i)){break;}int d=inf;for(int j=1;j<=n;j++){if(!visity[j]){d=min(d,slacks[j]);}}for(int j=1;j<=n;j++){if(visitx[j]){lx[j] -= d;}}for(int j=1;j<=n;j++){if(visity[j]){ly[j] +=d;}else {slacks[j] -=d;}}}}}
- 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语言 内存对齐的理解
- git参考手册与常用指令说明
- malloc/free与new/delete的相同点和不同点
- Ftp命令格式(rfc 959)
- o-1背包问题
- POJ 2195 Going Home
- Linux环境下的Socket编程
- hdu 2510 符号三角形 很好的搜索题 要经常看
- linux下c语言select函数用法
- http://www.cnblogs.com/bluestorm/archive/2011/11/05/2298125.html
- ora-03113
- 为什么现在要谈大数据?
- 单链表逆置
- python 网络编程 小知识积累