• 牛客NC51316 Going Home (二分图最大权匹配)


    链接:https://ac.nowcoder.com/acm/problem/51316
    来源:牛客网

    题目描述
    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.
    输入描述:
    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.
    输出描述:
    For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
    示例1
    输入

    复制
    2 2
    .m
    H.
    5 5
    HH..m
    .....
    .....
    .....
    mm..H
    7 8
    ...H....
    ...H....
    ...H....
    mmmHmmmm
    ...H....
    ...H....
    ...H....
    0 0
    输出

    复制
    2
    10
    28

    二分图最大权匹配。左部点为人,右部点为房子,由于题目中说人和房子数组相等,因此是完备匹配,可以直接跑km(当然也可以费用流)。对于每个人,求出他到所有房子的距离存入w数组(此处详见代码,有坑点),因为要求最小距离和,因此这里的距离要存负数再跑km,最后求出来的答案取相反数即可。

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int N = 205;
    int n, m, man, house, mx[10005], my[10005], hx[10005], hy[10005];
    char mp[N][N];
    //由于h和m个数相等,可以跑km
    int w[N][N];
    int la[N], lb[N];
    bool va[N], vb[N];
    int match[N];
    int delta, upd[N];
    bool dfs(int x, int ver) {
    	va[x] = 1;
    	int mx;
    	if(ver == 0) mx = house;
    	else mx = man;
    	//其实此处man和house相等
    	for(int y = 1; y <= mx; y++) {
    		if(!vb[y]) {
    			if(la[x] + lb[y] - w[x][y] == 0) {
    				vb[y] = 1;
    				if(!match[y] || dfs(match[y], ver ^ 1)) {
    					match[y] = x;
    					return 1;
    				}
    			}
    			else upd[y] = min(upd[y], la[x] + lb[y] - w[x][y]);
    		}
    	}
    	return false;
    }
    int KM()
    {
    	for(int i = 1; i <= man; i++) {
    		la[i] = -(1 << 30);
    		lb[i] = 0;
    		for(int j = 1; j <= house; j++) {
    			la[i] = max(la[i], w[i][j]);
    		}
    	}
    	for(int i = 1; i <= man; i++) {
    		while(true) {
    			memset(va, 0, sizeof(va));
    			memset(vb, 0, sizeof(vb));
    			for(int j = 1; j <= house; j++) {
    				upd[j] = 1000000000;
    			}
    			if(dfs(i, 0)) break;
    			for(int j = 1; j <= house; j++) {
    				if(!vb[j]) delta = min(delta, upd[j]);
    			}
    			for(int j = 1; j <= house; j++) {
    				if(va[j]) la[j] -= delta;
    				if(vb[j]) lb[j] += delta;
    			}
    		}
    	}
    	int ans = 0;
    	for(int i = 1; i <= man; i++) {
    		ans += w[match[i]][i];
    	}
    	return -ans;
    }
    int main() {
    	//freopen("data.txt", "r", stdin);
    	while(cin >> n >> m && n) {
    		man = house = 0;
    		delta = 1000000000;
    		memset(match, 0, sizeof(match));
    		for(int i = 1; i <= n; i++) {
    			scanf("%s", mp[i] + 1);
    		}
    		for(int i = 1; i <= n; i++) {
    			for(int j = 1; j <= m; j++) {
    				if(mp[i][j] == 'm') {
    					man++;
    					mx[man] = i, my[man] = j;
    				}
    				else if(mp[i][j] == 'H') {
    					house++;
    					hx[house] = i, hy[house] = j;
    				}
    			} 
    		}
    		for(int i = 1; i <= man; i++) {
    			for(int j = 1; j <= house; j++) {
    				int d = abs(hx[j] - mx[i]) + abs(hy[j] - my[i]);
    				w[i][j] = -d;
    				//注意这里不要写w[i][j] = w[j][i] = -d;
    			}
    		}
    		cout << KM() << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Redis过期机制
    vim使用
    ex command in Linux with examples
    【转】Linux 文档编辑 : ex 命令详解
    vscode go语言环境搭建
    golang slice a 的地址和a[0]的地址不一样
    文件加锁,作用是用来做什么?以及使用细节
    文件锁
    go创建指定大小的文件,获取文件大小
    go 实现内存映射和进程间通信
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/14411747.html
Copyright © 2020-2023  润新知