zoukankan      html  css  js  c++  java
  • UVA 1175 Ladies' Choice

    蓝书里的一道经典问题,稳定婚姻模型。

    具体内容参考蓝书,这里只提供代码。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<queue>
    #define ll long long
    #define maxn 1005
    using namespace std;
    queue<int> q;
    int man[maxn][maxn];
    int woman[maxn][maxn];
    int n,T,manpt[maxn],hus[maxn];
    
    inline void init(){
    	fill(manpt+1,manpt+n+1,1);
    	memset(hus,0,sizeof(hus));
    }
    
    inline bool marry(int x,int y){
    	if(!hus[y]){
    		hus[y]=x;
    		return 1;
    	}
    	else if(woman[y][x]<woman[y][hus[y]]){
    		q.push(hus[y]);
    		hus[y]=x;
    		return 1;
    	}
    	else return 0;
    }
    
    inline void solve(){
    	for(int i=1;i<=n;i++) q.push(i);
    	
    	int x,girl;
    	while(!q.empty()){
    		x=q.front(),q.pop();
    		girl=man[x][manpt[x]++];
    		if(!marry(x,girl)) q.push(x);
    	}
    }
    
    int main(){
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d",&n);
    		init();
    		for(int i=1;i<=n;i++)
    		    for(int j=1;j<=n;j++) scanf("%d",&man[i][j]);
    		int now;
    		for(int i=1;i<=n;i++)
    		    for(int j=1;j<=n;j++){
    		    	scanf("%d",&now);
    		    	woman[i][now]=j;
    			}
    		
    		solve();
    		
    		for(int i=1;i<=n;i++) printf("%d
    ",man[i][manpt[i]-1]);
    		if(T) puts("");
    	}
    	
    	return 0;
    }
    

      

  • 相关阅读:
    手机截屏雷阵雨
    2010年5月20日 用户的数据永远不会靠谱
    2010年5月17日 OCD
    2010年8月9日 流水账
    2010年5月19日 借鉴
    立表为据
    2010年6月2日 回来
    不重视小C打屁屁
    2010年8月16日 知行合一
    2010年5月18日 小细节大隐患
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8506298.html
Copyright © 2011-2022 走看看