Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1415 Accepted Submission(s): 698
Problem 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.
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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
题目大意:输入一幅图,图中有n个H和n个m分别代表有n个人和n个住所,人去住所需要花费金钱,移动一格需要一个费用,一格住所只能容纳一个人,问n个人都去到住所最低花费是多少?
求最低费用时,只要讲费用全部变成负数,然后按照原来那样求最大值,得出的结果再变成正数就是最后答案了,求最大值和最小值都是一样道理。
赤裸裸的最大权匹配问题,先建好图,然后一个模板套上去就OK了。
代码如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define MAXN 100
#define INF 99999999
struct node
{
int x, y;
}M[MAXN], H[MAXN];
char map[MAXN][MAXN];
int w[MAXN][MAXN];
int lx[MAXN], ly[MAXN];
bool sx[MAXN], sy[MAXN];
int pre[MAXN], slack[MAXN];
int n, m, mn, hn;
int dis(node a, node b)
{
int dis = abs(a.x-b.x) + abs(a.y-b.y);
return dis;
}
void bulid()
{
int i, j;
mn = hn = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
cin >> map[i][j];
if(map[i][j] == 'm')
{
M[mn].x = i;
M[mn].y = j;
mn++;
}
if(map[i][j] == 'H')
{
H[hn].x = i;
H[hn].y = j;
hn++;
}
}
}
memset(w, 0, sizeof(w));
for(i = 0; i < mn; i++)
for(j = 0; j < hn; j++)
w[i][j] = -dis(M[i], H[j]);
}
int dfs(int u)
{
sx[u] = true;
for(int i = 0; i < hn; i++)
{
if(sy[i]) continue;
int tp = lx[u] + ly[i] - w[u][i];
if(!tp)
{
sy[i] = true;
if(pre[i] == -1 || dfs(pre[i]))
{
pre[i] = u;
return 1;
}
}
else if(slack[i] > tp) slack[i] = tp;
}
return 0;
}
int KM()
{
int i, j, k, d, res = 0;
memset(pre, -1, sizeof(pre));
memset(ly, 0, sizeof(ly));
for(i = 0; i < mn; i++)
{
lx[i] = -INF;
for(j = 0; j < hn; j++)
lx[i] = max(lx[i], w[i][j]);
}
for(k = 0; k < mn; k++)
{
for(i = 0; i < hn; i++)
slack[i] = INF;
while(1)
{
memset(sx, false, sizeof(sx));
memset(sy, false, sizeof(sy));
if(dfs(k)) break;
d = INF;
for(i = 0; i < hn; i++)
if(!sy[i]) d = min(d, slack[i]);
for(i = 0; i < mn; i++)
if(sx[i]) lx[i] -= d;
for(i = 0; i < hn; i++)
if(sy[i]) ly[i] += d;
}
}
res = 0;
for(i = 0; i < hn; i++)
res += w[pre[i]][i];
return -res;
}
int main()
{
int ans;
while(scanf("%d %d", &n, &m) != EOF)
{
if(!n && !m) break;
bulid();
ans = KM();
printf("%d\n", ans);
}
return 0;
}
分享到:
相关推荐
hdu ACM代码 每种算法都有分类 大三了,没有时间弄ACM,这些要删了
HDU的ACM,非常的好 涉及了很多算法,例如二分匹配、博弈、组合、最小生成树、搜索、动态规划、贪心算法
hdu动态规划算法集锦
很实用的算法讲解,hdu的lcy老师讲解,还不错!!
acm 技术大牛 课件 HDU 自学必备课件 全套齐全 (lecture_01)初识ACM (lecture_02)简单数学题 (lecture_03)递推求解 (lecture_04)动态规划(1)_ (lecture_05)计算几何基础_ (lecture_06)母函数 (lecture_...
杭州电子科技大学oj平台上的第1010题,是关于搜索的题目,很不错的题
杭电ACM 培训课件,包括各种常用的算法,并结合例题详细的讲解,对提高算法思想用很大帮助
八数码的A*算法~不是很高效,但是很适合刚刚学这个算法的朋友们
算法-数塔(HDU-2084).rar
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
算法-超级楼梯(HDU-2040)(包含源程序).rar
算法设计与分析实验六:使用动态规划算法解决存钱问题(java实现、hdu1114)(csdn)————程序
HDU的一题........HDU DP动态规
算法-六度分离(HDU-1869)(包含源程序).rar
算法-欧拉回路(HDU-1878)(包含源程序).rar
hdu2101AC代码