带权二分图匹配(KM算法模板)

 

 

带权二分图匹配, KM算法

 

hdu2255      http://acm.hdu.edu.cn/showproblem.php?pid=2255

这是个裸模板题

 

 

 

AC代码:

 /*************************************************
       Author       :    NIYOUDUOGAO
       Last modified:    2018-08-04 09:33
       Email        :    pjm000@163.com
       Filename     :    t.cpp
 *************************************************/
#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 305;
int n;
int mp[N][N];
int match[N];
int exright[N], exleft[N];
int visright[N], visleft[N];
int slack[N];
int dfs(int left) {
    visleft[left] = 1;
    for (int i = 0; i < n; i++) {
        if (visright[i]) continue;
        int tmp = exleft[left] + exright[i] - mp[left][i];
        if (tmp == 0) {
            visright[i] = 1;
            if (match[i] == -1 || dfs(match[i])) {
                match[i] = left;
                return 1;
            }
        } else {
            slack[i] = min(slack[i], tmp);
        }
    }
    return 0;
}
int km() {
    mset(match, -1); mset(exright, 0);
    for (int i = 0; i < n; i++) {
        exleft[i] = mp[i][0];
        for (int j = 1; j < n; j++) {
            exleft[i] = max(exleft[i], mp[i][j]);
        }
    }
    for (int i = 0; i < n; i++) {
        mset(slack, INF);
        while (1) {
            mset(visright, 0); mset(visleft, 0);
            if (dfs(i)) {
                break;
            }
            int d = INF;
            for (int j = 0; j < n; j++) {
                if (!visright[j]) {
                    d = min(d, slack[j]);
                }
            }
            for (int j = 0; j < n; j++) {
                if (visleft[j]) {
                    exleft[j] -= d;
                }
                if (visright[j]) {
                    exright[j] += d;
                } else {
                    slack[j] -= d;
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
        res += mp[match[i]][i];
    }
    return res;
}
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    while (cin >> n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> mp[i][j];
            }
        }
        int res = km();
        cout << res << endl;
    }
    return 0;
}

 

 

 

 

 

链接:https://www.nowcoder.com/acm/contest/143/E
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Nowcoder University has 4n students and n dormitories ( Four students per dormitory). Students numbered from 1 to 4n.

And in the first year, the i-th dormitory 's students are (x1[i],x2[i],x3[i],x4[i]), now in the second year, Students need to decide who to live with.

In the second year, you get n tables such as (y1,y2,y3,y4) denote these four students want to live together.

Now you need to decide which dormitory everyone lives in to minimize the number of students who change dormitory.

输入描述:

The first line has one integer n.

Then there are n lines, each line has four integers (x1,x2,x3,x4) denote these four students live together in the first year

Then there are n lines, each line has four integers (y1,y2,y3,y4) denote these four students want to live together in the second year

输出描述:

Output the least number of students need to change dormitory.

示例1

输入

复制

2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5

输出

复制

2

说明

Just swap 4 and 5

备注:

1<=n<=100

1<=x1,x2,x3,x4,y1,y2,y3,y4<=4n

It's guaranteed that no student will live in more than one dormitories.

 

 

分析:

用总的情况减去 最多不用换宿舍的就是岁少换宿舍的

 

 

 

AC代码:

 /*************************************************
       Author       :	NIYOUDUOGAO
       Last modified:	2018-08-04 09:52
       Email        :	pjm000@163.com
       Filename     :	t.cpp
 *************************************************/
#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 305;
int n;
int mp1[305][5], mp2[305][5];
int mp[N][N];
int match[N];
int exright[N], exleft[N];
int visright[N], visleft[N];
int slack[N];
int dfs(int left) {
	visleft[left] = 1;
	for (int i = 0; i < n; i++) {
		if (visright[i]) continue;
		int tmp = exleft[left] + exright[i] - mp[left][i];
		if (tmp == 0) {
			visright[i] = 1;
			if (match[i] == -1 || dfs(match[i])) {
				match[i] = left;
				return 1;
			}
		} else {
			slack[i] = min(slack[i], tmp);
		}
	}
	return 0;
}
int km() {
	mset(match, -1); mset(exright, 0);
	for (int i = 0; i < n; i++) {
		exleft[i] = mp[i][0];
		for (int j = 1; j < n; j++) {
			exleft[i] = max(exleft[i], mp[i][j]);
		}
	}
	for (int i = 0; i < n; i++) {
		mset(slack, INF);
		while (1) {
			mset(visright, 0); mset(visleft, 0);
			if (dfs(i)) {
				break;
			}
			int d = INF;
			for (int j = 0; j < n; j++) {
				if (!visright[j]) {
					d = min(d, slack[j]);
				}
			}
			for (int j = 0; j < n; j++) {
				if (visleft[j]) {
					exleft[j] -= d;
				}
				if (visright[j]) {
					exright[j] += d;
				} else {
					slack[j] -= d;
				}
			}
		}
	}
	int res = 0;
	for (int i = 0; i < n; i++) {
		res += mp[match[i]][i];
	}
	return res;
}
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 4; j++) {
				cin >> mp1[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 4; j++) {
				cin >> mp2[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0 ; j < n; j++) {
				int cnt = 0;
				for (int k = 0; k < 4; k++) {
					for (int l = 0; l < 4; l++) {
						if (mp1[i][k] == mp2[j][l]) {
							cnt++;
							break;
						}
					}
				}
				mp[i][j] = cnt;
			}
		}
		cout << 4 * n - km() << endl;
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值