Stable Matching Problem HDU1522

A simple problem about the match problem.

We know that the KM algorithm solved the match problem with weight,but the stable matching is easier.

first: the man ask the best on his mind and never bend him girl.

second: if the girl is free,she will say yes,and match them.

               else the girl will choose the better one from the new man and her husband

                        if the girl choose the new one,her ex become free,and match the new one.

              continue until the queue is empty();

the code.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
class man
{
public:
    man() {};
    queue<int> w_q;
    string name;
    int wife_id;
};
class woman
{
public:
    woman()
    {

    };
    string name;
    int hus_id;
    vector<int> love_man;
};
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    int n,i,j,tmp_id,cur;
    string m_tmp,w_tmp;
    vector<man> m_v;
    vector<woman> w_v;
    map<string,int> w_mp;
    map<string,int> m_mp;
    queue<int> free_q;
    while(cin>>n)
    {

        m_v.clear();
        w_v.clear();
        w_v.resize(n+1);
        m_v.resize(n+1);
        w_mp.clear();
        m_mp.clear();
        cin>>m_tmp;
        m_mp.insert(map<string,int>::value_type(m_tmp,1));   //map the first man
        m_v[1].name=m_tmp;
        for(j=1;j<=n;j++){
         cin>>w_tmp;
         w_mp.insert(map<string,int>::value_type(w_tmp,j));  //map the n girls 
         m_v[1].w_q.push(j);

        }

        for(i=2; i<=n; i++)
        {
            cin>>m_tmp;
            m_mp.insert(map<string,int>::value_type(m_tmp,i));  //map 2->n men
            m_v[i].name=m_tmp;
            for(j=1;j<=n;j++){
              cin>>w_tmp;
              m_v[i].w_q.push(w_mp[w_tmp]);   //insert the girl number by sort

            }

        }
        for(i=1; i<=n; i++)
        {
            cin>>w_tmp;
            tmp_id=w_mp[w_tmp] ;
            w_v[tmp_id ].name=w_tmp;
            w_v[tmp_id ].hus_id=0;      //init hus_id
            w_v[tmp_id ].love_man.resize(n+1);
            for(j=1;j<=n;j++){
                cin>>m_tmp;
                w_v[tmp_id].love_man[m_mp[m_tmp]]=j;  //set the love_man

            }
        }
        for(i=1; i<=n; i++)
        {
            free_q.push(i); //set for the freeman
        }
        int wid,hid;   //tmp
        while(!free_q.empty())
        {
            cur=free_q.front();
            wid=m_v[cur].w_q.front();  //the best woman id
            m_v[cur].w_q.pop();        //give up the woman,no matter choose or not.
            if(w_v[wid].hus_id==0)       //free
            {
                w_v[wid].hus_id=cur;     //set husband id
                m_v[cur].wife_id=wid;    //cur set wife id
                free_q.pop();            //pop the man from free

            }
            else
            {
                hid=w_v[wid].hus_id;     //get the wid-th  woman's husband
                if(w_v[wid].love_man[cur]<w_v[wid].love_man[hid])  //if  cur is better
                {
                    free_q.pop();        //pop the man from free
                    free_q.push(hid);    //push the wid-th ex in free
                    w_v[wid].hus_id=cur; //set the wid-th woman husband id
                    m_v[cur].wife_id=wid;//set wife id
                }
                  //if failed,go on;
            }
        }

        for(i=1; i<=n; i++)
        {
            cout<<m_v[i].name<<" "<<w_v[m_v[i].wife_id].name<<endl;
        };
    }
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    #endif
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值