HDUOJ 2195 二分图最优匹配
来源:互联网 发布:穿越火线为什么优化差 编辑:程序博客网 时间:2024/06/03 13:04
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17793 Accepted: 9070
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
题意:有n个人n个房子,开始时每个人都不在房子内,让每个人走一步需要消耗1元,每个房子里只能装一个人,人可以站在房子所在的点但不进入房子,也可以很多人站在一个点,要求让所有人都进到房子里的最小花费。
思路:求出每个人到每个房子的花费,令X集合为所有人的集合,Y集合为所有房子的集合,原问题就转化成了二分图的最优匹配。(坑爹的变量名居然和math库冲突了。。。)
代码:
#include<iostream>#include<cstdio>//#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<ctype.h>#include<bitset>#include<map># pragma comment (linker,"/STACK:16777216")using namespace std;const int MAXN = 205;const int INF = 2100000000;const double esp = 1e-9;class Point{ public: int row, col;};Point man[MAXN];Point house[MAXN];int xn, yn;int n, m;int mat[MAXN][MAXN];bool visx[MAXN], visy[MAXN];int slack[MAXN];int link[MAXN];int lx[MAXN], ly[MAXN];bool find(int x){ visx[x] = 1; for(int i = 1; i < yn; i++) { if(!visy[i] && mat[x][i] != 0) { int temp = lx[x] + ly[i] - mat[x][i]; if(temp == 0) { visy[i] = 1; if(!link[i] || find(link[i])) { link[i] = x; return 1; } } else { slack[i] = min(slack[i], temp); } } } return 0;}void km(){ memset(ly, 0, sizeof(ly)); for(int i = 1; i < xn; i++) { lx[i] = -INF; for(int j = 1; j < yn; j++) { lx[i] = max(lx[i], mat[i][j]); } } for(int i = 1; i < xn; i++) { memset(slack, 127, sizeof(slack)); while(1) { memset(visy, 0, sizeof(visy)); memset(visx, 0, sizeof(visx)); if(find(i)) break; int d = INF; for(int j = 1; j < yn; j++) if(!visy[j]) d = min(d, slack[j]); for(int j = 1; j < xn; j++) { if(visx[j]) lx[j] -= d; } for(int j = 1; j < yn; j++) { if(visy[j]) ly[j] += d; else { slack[j] -= d; } } } }}int main(){ //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin); while(cin >> n >> m && n) { xn = yn = 1; memset(link, 0, sizeof(link)); memset(mat, -127, sizeof(mat)); char temp; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> temp; if(temp == 'm') man[xn].row = i, man[xn].col = j, xn++; else if(temp == 'H') house[yn].row = i, house[yn].col = j, yn++; } } for(int i = 1; i < xn; i++) { for(int j = 1; j < yn; j++) { mat[i][j] = -(abs(man[i].row - house[j].row) + abs(man[i].col - house[j].col)); } } km(); int ans = 0; for(int i = 1; i < yn; i++) { if(link[i] != 0) { ans += mat[link[i]][i]; } } cout << -ans << endl; } return 0;}
0 0
- HDUOJ 2195 二分图最优匹配
- hdu2255 (二分图最优匹配)
- 二分图最优完美匹配
- 二分图最优匹配 模板
- poj 2195 二分图最优匹配 KM算法求最小值
- POJ 2195 Going Home(二分图最优匹配)
- poj 2195 - Going Home 二分图最优匹配 ek
- POJ 2195 Going Home(二分图最优匹配)
- poj 2195 Going Home(二分图最优匹配KM算法)
- POJ 2195 Going Home(二分图最优匹配)
- poj 2195 二分图最优匹配KM算法 模板题
- poj 2594(二分图最优匹配)
- HDU-2853 Assignment【二分图最优匹配】
- hdu 2255 二分图—最优匹配
- KM算法求二分图最优匹配
- 二分图的最优匹配 going home
- ACM-> 二分图的最优匹配
- hdu1533 going home 二分图最优匹配
- 一些杂想
- javascript笔记 --基础
- 方法重写与重载
- 最大流Dinic算法实现——以《算法竞赛入门经典——训练指南》为例
- 数据库学习资料积累
- HDUOJ 2195 二分图最优匹配
- 全排列
- 素数判定 & 素数筛法 & poj_3292_Semi-prime H-numbers
- 页面优化——js异步加载
- 成为优秀程序员的十个有效方法
- PAT Basic 1008. 数组元素循环右移问题 (20)
- Linux 文件和目录的操作,权限相关命令
- 使用 python Matplotlib 库绘图 .
- poj 2635