HDU 1533 KM算法(权值最小的最佳匹配)
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3299 Accepted Submission(s): 1674
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.
#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算法(权值最小的最佳匹配)的更多相关文章
- hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)
/* 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! tm太坑了... 1,如果这个无向图开始就是一个非连通图,直接输出0 2,重边(两个节点存在多条边, 权值不一样) 3,如果找到 ...
- hdu 3435(KM算法最优匹配)
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- HDU 1533:Going Home(KM算法求二分图最小权匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description On a grid map there ...
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 4862 KM算法 最小K路径覆盖的模型
http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过全部的点一次而且只一次. 建图是问题: 我自己最初就把n*m 个点分别放入 ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- HDU 2255 KM算法 二分图最大权值匹配
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- thinkphp中ajaxReturn方法实现ajax效果
前台代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 定义函数def
- java源程序---可执行文件(.exe)----安装包
众所周知,java源程序都可以导出为(executable jar file)可执行的.jar文件,但是该.jar文件需要在jre环境下才能执行. 那么如何能在没有装jre的电脑上运行呢?(那就要顺带 ...
- jQuery数组的遍历 function的加载
加载函数时会被覆盖在jQuery中给提供的方案有三种形式 js中只能绑定一个方法 如果多次绑定后者会覆盖前者 最常用的一种 在jQuery中数组的遍历 使用map遍历数组 会返回一个新的数组 如果 ...
- One Class SVM, SVDD(Support Vector Domain Description)(转)
今天给大家介绍一下one class classification以及用SVDD(support vector domain description)做one class classification ...
- VB6 GDI+ 入门教程[1] GDI+介绍
http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[1] GDI+介绍 2009 年 6 月 18 日 17 ...
- 使用 Subversion (SVN) 的方式来访问 Github(转)
[转至]:http://www.open-open.com/lib/view/open1340760267717.html
- Oct22 实例测试
实例: http://www.runoob.com/jsp/jsp-form-processing.html http://www.runoob.com/jsp/jsp-writing-filters ...
- 能源项目xml文件标签释义--CommonsMultipartResolver
<!-- 文件上传表单的视图解析器 --><bean id="multipartResolver" class="org.springframework ...
- 八大排序算法的 Python 实现
转载: 八大排序算法的 Python 实现 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个 ...