zoukankan      html  css  js  c++  java
  • 匈牙利算法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总结之json解析(FastJson Gson 对比)
    Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
    IOS遍历网页获取网页中<img>标签中的图片url
    IOS各种集合遍历效率对比
    cx_oracle访问处理oracle中文乱码问题
    使用tar解压文件提示gzip: stdin: not in gzip format错误
    Mac安装crfpp
    oracle 常用操作
    docker启动centos7后sudo不能使用
    常见Python爬虫工具总结
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13412566.html
Copyright © 2011-2022 走看看