zoukankan      html  css  js  c++  java
  • 关于二分图的完美匹配问题

    前言

    占坑

    代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using std::min; using std::max;
    using std::swap; using std::sort;
    using std::__gcd;
    typedef long long ll;
    
    template<typename T>
    void read(T &x) {
        int flag = 1; x = 0; char ch = getchar();
        while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
        while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
    }
    
    const int N = 3e2 + 10, Inf = 1e9 + 7;
    int n, w[N][N], match[N], ret;
    int lx[N], ly[N];//定点标号
    bool visx[N], visy[N];
    void upt0(int &x, int y) { if(x < y) x = y; }
    void upt1(int &x, int y) { if(x > y) x = y; }
    
    bool Hungary(int x) {
    	visx[x] = 1;
    	for(int y = 1; y <= n; ++y) {
    		if(!visy[y] && lx[x] + ly[y] == w[x][y]) {
    			visy[y] = true;
    			if(!match[y] || Hungary(match[y])) { match[y] = x; return 1; }
    		} 
    	} return 0;
    }
    
    int KM() {
    	for(int i = 1; i <= n; ++i) {
    		lx[i] = -Inf, ly[i] = 0;
    		for(int j = 1; j <= n; ++j) upt0(lx[i], w[i][j]);
    	}
    	memset(match, 0, sizeof match);
    	for(int x = 1; x <= n; ++x)
    		while(1) {
    			memset(visx, 0, sizeof visx), memset(visy, 0, sizeof visy);
    			if(Hungary(x)) break;
    			int inc = Inf;//增量标记
    			for(int i = 1; i <= n; ++i)
    				if(visx[i])
    					for(int j = 1; j <= n; ++j)
    						if(!visy[j]) upt1(inc, lx[i] + ly[j] - w[i][j]);
    			for(int i = 1; i <= n; ++i) {
    				if(visx[i]) lx[i] -= inc;
    				if(visy[i]) ly[i] += inc;
    			}
    		}
    	for(int i = 1; i <= n; ++i)
    		if(match[i]) ret += w[match[i]][i];
    	return ret;
    }
    
    int main () {
    	while(scanf("%d", &n) != EOF) {
    		for(int i = 1; i <= n; ++i)
    			for(int j = 1; j <= n; ++j)
    				read(w[i][j]);
    		printf("%d
    ", KM()), ret = 0;
    	}
    	return 0;
    } 
    
  • 相关阅读:
    Scala中隐式转换(implicit conversion)的优先顺序
    protege4.0使用中的理论
    国外程序员整理的 C++ 资源大全
    什么是本体论?
    深入分析C++引用
    在基于对话框的MFC程序中,使程序在任务栏中不显示图标
    PhoneGap搭建运行环境(3.2版本)
    [JS代码]如何判断ipad或者iphone是否为横屏或者竖屏
    windwos iis 7.5 使用html 报405错误
    NodeJs 开源
  • 原文地址:https://www.cnblogs.com/water-mi/p/10253521.html
Copyright © 2011-2022 走看看