Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3299    Accepted Submission(s): 1674

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
2
10
28
 
Source
 
 
题目意思:
给一个n*m的地图,'m'表示人,'H'表示房子,人每次能向上下左右走到邻近的单位,每次走一次总费用+1,每个房子只能容纳一个人,求当所有人走到房子后总费用最小为多少。
 
思路:
人放左边,房子放右边,之间的边权值表示人x[i]走到房子y[j]处花费的费用。权值弄成负数,KM找最佳匹配,答案在负一下即可。
 
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 105
#define inf 999999999 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} struct KM {
int n, m;
int g[N][N];
int Lx[N], Ly[N], slack[N];
int left[N], right[N];
bool S[N], T[N]; void init(int n, int m) {
this->n = n;
this->m = m;
memset(g, , sizeof(g));
} void add_Edge(int u, int v, int val) {
g[u][v] += val;
} bool dfs(int i) {
S[i] = true;
for (int j = ; j < m; j++) {
if (T[j]) continue;
int tmp = Lx[i] + Ly[j] - g[i][j];
if (!tmp) {
T[j] = true;
if (left[j] == - || dfs(left[j])) {
left[j] = i;
right[i] = j;
return true;
}
} else slack[j] = min(slack[j], tmp);
}
return false;
} void update() {
int a = inf;
for (int i = ; i < m; i++)
if (!T[i]) a = min(a, slack[i]);
for (int i = ; i < n; i++)
if (S[i]) Lx[i] -= a;
for (int i = ; i < m; i++)
if (T[i]) Ly[i] += a;
} int km() {
memset(left, -, sizeof(left));
memset(right, -, sizeof(right));
memset(Ly, , sizeof(Ly));
for (int i = ; i < n; i++) {
Lx[i] = -inf;
for (int j = ; j < m; j++)
Lx[i] = max(Lx[i], g[i][j]);
}
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) slack[j] = inf;
while () {
memset(S, false, sizeof(S));
memset(T, false, sizeof(T));
if (dfs(i)) break;
else update();
}
}
int ans = ;
for (int i = ; i < n; i++) {
ans += g[i][right[i]];
}
return ans;
}
}kmm; int n, m;
char map[N][N];
int g[N][N]; struct node{
int x, y;
node(){}
node(int a,int b){
x=a;
y=b;
}
}a[N], b[N]; main()
{
int i, j, k;
int nx, ny;
while(scanf("%d %d",&n,&m)==){
if(n==&&m==) break; for(i=;i<n;i++) scanf("%s",map[i]);
nx=ny=;
memset(g,,sizeof(g));
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map[i][j]=='m') a[nx++]=node(i,j);
if(map[i][j]=='H') b[ny++]=node(i,j);
}
}
kmm.init(nx,ny);
for(i=;i<nx;i++){
for(j=;j<ny;j++){
kmm.add_Edge(i,j,-(abs(a[i].x-b[j].x)+abs(a[i].y-b[j].y)));
}
}
printf("%d\n",-kmm.km());
}
}

HDU 1533 KM算法(权值最小的最佳匹配)的更多相关文章

  1. hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)

    /* 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! tm太坑了... 1,如果这个无向图开始就是一个非连通图,直接输出0 2,重边(两个节点存在多条边, 权值不一样) 3,如果找到 ...

  2. hdu 3435(KM算法最优匹配)

    A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  4. HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description   On a grid map there ...

  5. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. hdu 4862 KM算法 最小K路径覆盖的模型

    http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过全部的点一次而且只一次. 建图是问题: 我自己最初就把n*m 个点分别放入 ...

  7. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  8. HDU 2255 KM算法 二分图最大权值匹配

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. UML建模

    1.包含<<include>> 包含是指当多个用例中存在相同的事件流时,可以把这些公共事件流抽象成公共用例,这个公共用例称之为抽象用例(跟类的概念有点相像,类是多个对象的抽象定 ...

  2. 让你分分钟读懂CPU架构及芯片厂商

    CPU架构是CPU厂商给属于同一系列的CPU产品定的一个规范,主要目的是为了区分 不同类型CPU的重要标示.目前市面上的CPU指令集分类主要分有两大阵营,一个是intel.AMD为首的复杂指令集CPU ...

  3. linux 静态库、共享库

    http://blog.chinaunix.net/uid-26833883-id-3219335.html http://blog.chinaunix.net/uid-23069658-id-314 ...

  4. 测序原理 - PacBio技术资料

    手头有一套完整的PacBio技术资料,会慢慢的总结到博客上. 写在前面:PacBio公司主要有两个测序平台一个是RS,一个是最新的Sequel,下面如果没有指明则是在讲RS平台. SMRT测序技术总览 ...

  5. 机器学习与R语言

    此书网上有英文电子版:Machine Learning with R - Second Edition [eBook].pdf(附带源码) 评价本书:入门级的好书,介绍了多种机器学习方法,全部用R相关 ...

  6. dede 调用四级导航

    一.修改文件:\include\taglib目录下的channel.lib.php,请将以下代码全部复制替换上述文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  7. 自定义ExpressionBuilder

    ExpressionBuilder的常见说明见https://msdn.microsoft.com/zh-cn/library/System.Web.Compilation.ExpressionBui ...

  8. html5中的表单

    <form id="aForm" action="reg.php"> <p>请填写表单内容以完成注册!</p> <fi ...

  9. 如何理解和熟练使用JS 中的call apply

    有时候看一两个关于apply或call的小例子,感觉能够理解一点点但是下次碰到又要纠结半天才能转过弯来-而且不知道怎么应用到实际工作当中去- call 和 apply 都是为了改变某个函数运行时的 c ...

  10. oncontextmenu

    一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键:在JS中设置oncontextmenu='return true'用window.document.on ...