• 二分图匹配之稳定婚姻问题(题源,hdu1522)


    老师布置了作业,让写下稳定婚姻问题的解答,GS算法的实现,正好hdu上有类似的题目,顺便拿来切了……

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1522 

    很简单的方式,C++,用队列存储尚在单身的男士,退出条件为队列为空。 由于答案的输出一定要把男士放前面,所以又定义了个bres[]用来存储男士的选择,其实没有必要。

    #include <cstring>
    #include <stdio.h>
    #include <string>
    #include <cmath>  
    #include <queue>   
    #include <map>  
    #include <memory>
    #include <iostream>
    using namespace std;
    map<stringint> bmap, gmap;
    string bname[501], gname[501];
    int bpre[501[501];    //used to store boys' prefrence, bpre[i][j] = k means to boy i, he ranks the girl k in jth place, the girl k is his jth choice.
    int gpre[501][501];    //used to store girls' prefrence, gre[i][j] = k means to girl i, she ranks the boy j in the kth place
                        
    //(different from the storing way of bpre[][], because to boys, they traverse girls from highest to lowest; 
                        
    //but to girls, they need to quickly get to know one boy's rank)
    int gres[501];    //used to store girls' result, gres[i]=j means girl i is with boy j
    int bres[501];
    char input[1500];
    int n;
    queue<int> bqueue;

    int main(){
        while(~scanf("%d",&n)){
            memset(gres,-1,sizeof gres);
            memset(bres,-1,sizeof bres);
            getchar();
            bmap.clear();
            gmap.clear();
            int gcount = 0, i = 0, j = 0;
            int index = 0;
            for(i = 1;i <= n;i++){
                scanf("%s", input);
                bname[i] = input;
                bmap[input] = i;
                bqueue.push(i);
                bpre[i][0] = 1;    //b[i][0] store thes ranking number of the girl whom this boy is asking, every boy starts from the 1st girl.
                for(j = 1;j <= n;j++){
                    scanf("%s",input);
                    if(gmap[input] == 0){
                        index++;
                        gmap[input] = index;
                        gname[index] = input;
                    }
                    bpre[i][j] = gmap[input];
                }
            }
            for(i = 1;i <= n;i++){
                scanf("%s", input);
                int gindex = gmap[input];
                for(j = 1;j <= n;j++){
                    scanf("%s",input);
                    gpre[gindex][bmap[input]] = j; //store the rank, the smaller, the higher the propriety will be.
                }
            }
            int bindex = 0, gaskindex = 0;
            while(!bqueue.empty()){
                bindex = bqueue.front();
                //cout << "current boy's name: "<< bname[bindex] <<endl;
                bqueue.pop();
                gaskindex = bpre[bindex][bpre[bindex][0]];
                if(gres[gaskindex] < 0){
                    gres[gaskindex] = bindex;
                    bres[bindex] = gaskindex;
                }else if(gpre[gaskindex][bindex] < gpre[gaskindex][gres[gaskindex]]){
                    bqueue.push(gres[gaskindex]);
                    bpre[gres[gaskindex]][0] = (bpre[gres[gaskindex]][0]+1) % (n+1);
                    gres[gaskindex] = bindex;
                    bres[bindex] = gaskindex;
                }else{
                    bqueue.push(bindex);
                    bpre[bindex][0] = (bpre[bindex][0]+1) % (n+1);
                }
            }
            for(i = 1;i <=n ;i++){
                cout<<bname[i]<<' '<<gname[bres[i]]<<endl;
            }
        }
        return 0;
    }

    去了注释代码还有1757B,罪过……

  • 相关阅读:
    vue子父组件传值
    springboot后端controller参数接收
    mybatis-plus 相关
    整理 node-sass 安装失败的原因及解决办法
    vue组件name的作用小结
    关于npm audit fix
    Vue项目
    你们都在用IntelliJ IDEA吗?或许你们需要看一下这篇博文
    Eslint配置
    spring boot 资料整合
  • 原文地址:https://www.cnblogs.com/felixfang/p/2463811.html
Copyright © 2020-2023  润新知