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. 使用myeclipse建立maven项目(重要)

    maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 工具/原料   myeclipse maven 方法/步骤   ...

  2. Python学习笔记3—字符串

    原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...

  3. 【Todo】Nginx架构学习

    要进行Web服务,绕不开的就是Nginx.这已经是大型网站的标配.对Nginx进行一定程度的深入学习. http://www.ituring.com.cn/article/4436 http://bl ...

  4. Android ViewPager更新数据

    ViewPager也是一个常用的组件 与ListView类似 当绑定数据后 想刷新数据 需要在适配器中添加如下方法 protected PagerAdapter galleryAdapter = ne ...

  5. 网络粘贴---Xcode中可用到的快捷键

    快捷键: 1.StoryBoard技巧 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然 ...

  6. MyBatis——优化MyBatis配置文件中的配置

    原文:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写 ...

  7. PHP的UTF-8中文转拼音处理类(性能已优化至极致)

    <?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...

  8. 转:Effective c + + notes

    补充自己的. 转自:http://blog.csdn.net/ysu108/article/details/9853963#t0 Effective C++ 笔记 目录(?)[-] 第一章 从C转向C ...

  9. HTML标签的默认样式

    body    有默认的内外边距(margin:0;padding:0); p         有默认的外边距(margin:0;)

  10. 多线程调用HttpWebRequest并发连接限制

    .net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows  7 下默认是2,在服务器操作 ...