最优最小权匹配
来源:互联网 发布:湖南省2016年经济数据 编辑:程序博客网 时间:2024/05/18 01:14
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3041 Accepted Submission(s): 1541
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.
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028通过这个题让我加深了对二分匹配最大权匹配中扩大相等子图的理解。真的是个好题目呀。。。。。。这个题就是反其道而行之。。。#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <climits>#include <cmath>using namespace std;const int N = 105;struct node{ int x, y;};vector<node>man;vector<node>house;int weight[N][N];int lx[N], ly[N];int slack[N];int match[N];bool visx[N], visy[N];int n, m;bool dfs(const int& x){ visx[x] = 1; for (int i = 0; i < m; i ++) { if (visy[i]) continue; int t = weight[x][i]-lx[x]-ly[i]; if (t == 0) { visy[i] = 1; if (match[i] == -1 || dfs(match[i])) { match[i] = x; return true; } } else { slack[i] = min(slack[i], t); } } return false;}void KM(){ int temp; memset(ly, 0, sizeof(ly)); for (int i = 0; i < n; i ++) { lx[i] = INT_MAX; for (int j = 0; j < m; j ++) { lx[i] = min(lx[i], weight[i][j]); } } for (int i = 0; i < n; i ++) { for (int j = 0; j < m; j ++) slack[j] = INT_MAX; while (1) { memset(visx, 0, sizeof(visx)); memset(visy, 0, sizeof(visy)); if (dfs(i)) break; temp = INT_MAX; for (int j = 0; j < m; j ++) { if (!visy[j]) temp = min(temp, slack[j]); } for (int j = 0; j < n; j ++) { if (visx[j]) lx[j] += temp; } for (int j = 0; j < m; j ++) { if (visy[j]) ly[j] -= temp; else slack[j] -= temp; } } }}int main(){ while (scanf("%d%d", &n, &m), n+m) { man.clear(), house.clear(); node temp; for (int i = 1; i <= n; i ++) { getchar(); for (int j = 1; j <= m; j ++) { char ch = getchar(); if (ch == 'm') { temp.x = i, temp.y = j, man.push_back(temp); } else if (ch == 'H') { temp.x = i, temp.y = j, house.push_back(temp); } } } n = man.size(); m = house.size(); for (int i = 0; i < n; i ++) { for (int j = 0; j < m; j ++) { weight[i][j] = abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y); } } memset(match, -1, sizeof(match)); KM(); int ans = 0; for (int i = 0; i < m; i ++) { if (match[i] > -1) ans += weight[match[i]][i]; } printf("%d\n", ans); } return 0;}
0 0
- 最优最小权匹配
- POJ 2400 KM算法 最小权匹配 回溯输出所有最优匹配方案
- 最小费最大流,最小权最大匹配,拆点法(最优巴士路线设计 uva 1349)
- 最优权匹配问题
- KM算法 最优匹配(最大权匹配) hdu 2255 奔小康赚大钱 最小权匹配 poj 2195 Going Home
- 二分图及其匹配算法——最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图——最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配(二分图学习)
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配(转)
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- [转载]二分图大讲堂——彻底搞定最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖、带权最优匹配
- 关于opencv2.4.9和VS2013在windows7下无法打开摄像头的问题的扯淡解决办法
- hdu1800(哈希或map)
- 黑马程序员——Java基础——IO流
- 数据结构(c++版)第五章 树和二叉树
- 非递归遍历 二叉树 前序 中序 后序 通用模板
- 最优最小权匹配
- 调用系统相册 发现 状态栏消失
- CCS新建Cortex-M4芯片工程详解及说明
- Hibernate中的left outer join
- 前缀、中缀、后缀表达式
- 将32位代码向64位平台移植的注意事项
- 基于ASP.NET的网站发布、打包和安装
- Apache Shiro 使用手册(二)Shiro 认证
- 二叉查找树(二叉排序树)