HDU 1533 KM算法(权值最小的最佳匹配)

时间:2022-09-18 23:58:15

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. 
HDU 1533 KM算法(权值最小的最佳匹配)
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&&num;39&semi;s Bridges&lpar;无向图边双连通分量,找出权值最小的桥&rpar;

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

  2. hdu 3435&lpar;KM算法最优匹配&rpar;

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

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

    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. &lbrack;ACM&rsqb; 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&lpar;KM算法&vert;&vert;最小费用最大流&rpar;

    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&lpar;KM算法&vert;&vert;最小费用最大流&lpar;第二种超级巧妙&rpar;&rpar;

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

随机推荐

  1. C&num;序列化及反序列化Json对象通用类JsonHelper

    当今的程序界Json大行其道.因为Json对象具有简短高效等优势,广受广大C#码农喜爱.这里发一个序列化及反序列化Json对象通用类库,希望对大家有用. public class JsonHelper ...

  2. RichTextBox文字处理控件属性介绍

    RichTextBox控件是一种既能够输入文本. 又能够修改文本的文字处理控件, 与TextBox控件比较, RichTextBox控件的文字处理功用更加丰厚, 不只能够设定文字的色彩. 字体, 还具 ...

  3. HDU1028Ignatius and the Princess III&lpar;母函数&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 母函数: 例1:若有1克.2克.3克.4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案? 如何解决这 ...

  4. BrnShop开源网上商城第五讲:自定义视图引擎

    今天这篇博文主要讲解自定义视图引擎,大家都知道在asp.net mvc框架中默认自带一个Razor视图引擎,除此之外我们也可以自定义自己的视图引擎,只需要实现IViewEngine接口,接口定义如下: ...

  5. jquery与ajax的应用

    1.编写第一个Ajax的例子,先来看一下传统的JavaScript实现的ajax例子. 首先在前台页面中书写HTML代码. <input type="button" valu ...

  6. 四种常见的提示弹出框(success,warning,error,loading)原生JavaScript和jQuery分别实现

    原文:四种常见的提示弹出框(success,warning,error,loading)原生JavaScript和jQuery分别实现 虽然说现在官方的自带插件已经有很多了,但是有时候往往不能满足我们 ...

  7. gdb windbg and od use

    gdb aslr -- 显示/设置 gdb 的 ASLR asmsearch -- Search for ASM instructions in memory asmsearch "int ...

  8. 2018&period;09&period;16 bzoj1086&colon; &lbrack;SCOI2005&rsqb;王室联邦(贪心)

    传送门 就是给树分块. 对于一个节点. 如果它的几棵子树加起来超过了下限,就把它们分成一块. 这样每次可能会剩下几个节点. 把它们都加入栈中最顶上那一块就行了. 代码: #include<bit ...

  9. 如何转义CSV文件中的逗号

    CSV全称是:Comma Separated Values 或者 Character Separated Values. 尽管第一种说法更常见,但我觉得还是第二种说法更确切一些,因为你可以使用其它字符 ...

  10. Sass 基础&lpar;七&rpar;

    Sass Maps 的函数-map-remove($map,$key),keywords($args) map-remove($map,$key) map-remove($map,$key)函数是用来 ...