首页 > 代码库 > 最优最小权匹配
最优最小权匹配
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 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
Sample Output
2 10 28通过这个题让我加深了对二分匹配最大权匹配中扩大相等子图的理解。真的是个好题目呀。。。。。。这个题就是反其道而行之。。。#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; }
最优最小权匹配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。