• 匈牙利算法C++实现


    简介

    一般场景是男生和女生配对的问题,现有男生要去配对如果想去配对的女生已经有喜欢的男生了,那就让想去配对的女生已经喜欢的男生挪挪位置,看看想去配对的女生已经喜欢的男生能不能喜欢其他人,给现有配对的男生让一个位置。
    有点绕。但是就是这样。

    参考链接

    https://www.bilibili.com/video/BV1Wx411L7Di?from=search&seid=16983503622667189725
    http://acm.hdu.edu.cn/showproblem.php?pid=2063

    code 对应 hdoj 2063

    /*
    7
    1 1
    1 2
    2 2
    2 3
    3 1
    3 2
    4 3
    */
    
    #include <map>
    #include <stack>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 510;
    
    
    /**************************************************
    @brief   : used 表示男生的某一次访问过程中女生有没有被匹配到
    		   nxt 表示女生喜欢的男生
    @author  : none
    @input   :none
    @output  :none
    @time    : none
    **************************************************/
    int line[maxn][maxn], used[maxn], nxt[maxn];
    int t, n, m, u, v;
    
    /**************************************************
    @brief   : 男生配对
    @author  : none
    @input   :none
    @output  :none
    @time    : none
    **************************************************/
    bool Find(int x) {
    	for (int i = 1; i <= m; i++) { // m 个女生
    		if (line[x][i] && !used[i]) { // x 和 i 是互相喜欢的,并且这个妹子名花无主
    			used[i] = 1;// 表示这个妹子配对上
    			if (nxt[i] == 0 || Find(nxt[i])) {
    				// 如果这个妹子没有匹配上人 或者 这个男生可以喜欢别人
    				nxt[i] = x;// i 个女生就和 x 配对上
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    /**************************************************
    @brief   : 匹配算法
    @author  : none
    @input   :none
    @output  :none
    @time    : none
    **************************************************/
    int match() {
    	int sum = 0;
    	for (int i = 1; i <= n; i++) {// n 男生的个数
    		memset(used, 0, sizeof(used));
    		if (Find(i)) sum++; // 寻找匹配的妹子
    	}
    	return sum;
    }
    
    int main() {
    	ios::sync_with_stdio(false);
    	while (cin >> t && t) {
    		cin >> n >> m;
    		memset(nxt, 0, sizeof(nxt));
    		memset(line, 0, sizeof(line));
    		while (t--) {
    			cin >> u >> v;
    			line[u][v] = 1;
    		}
    		cout << match() << endl;
    	}
    	// system("pause");
    }
    

    深入思考

    能不能用这个C++算法解决下面的问题,下面数据说明 越接近0 表示小姐姐越想去上班,1表示小姐姐那天有事儿不能去上班。
    https://www.cnblogs.com/eat-too-much/p/13409628.html

    TIPS

    上面的是多解法,有多个答案,让总体的小姐姐上班开心数值最大也就是总和数值(上班心情值)最小。

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    android学习---ListView
    android学习---ViewStub惰性装载控件
    android学习---TabHost
    微信小程序二维码带参数
    as
    niginx高性能原因
    Nginx动态反向代理1
    Niginx反向代理负载均衡
    jmeter测试,定制化内嵌tomcat调优
    Git通过密钥对远程仓库上传和更新详细操作
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13412566.html
Copyright © 2020-2023  润新知