首页 > 其他 > 详细

飞行员配对方案问题

时间:2020-02-23 14:39:38      阅读:63      评论:0      收藏:0      [点我收藏+]

裸的二分图,匈牙利算法可以水过。

#include<bits/stdc++.h>
using namespace std;
const int maxn=500;
int n,m,p[maxn],match[maxn];
int a[maxn][maxn],to[maxn];
int dfs(int x){
    //puts("dfs");
    for(int i=m+1;i<=n+m;i++)
        if(!p[i]&&a[x][i]){
            //printf("%d\n",i);
            p[i]=1;
            if(!match[i]||dfs(match[i])){
                match[i]=x;
                to[x]=i;
                return 1;
            }
        }
    return 0;
}
int main(){
    cin>>m>>n;
    int x,y;
    while(1){
        cin>>x>>y;
        if(x==-1)break;
        a[x][y]=a[y][x]=1;
    }
    //for(int i=1;i<=n+m;i++){
    //    for(int j=1;j<=n+m;j++)
    //        printf("%d ",a[i][j]);
    //    puts("");    
    //}
    int ans=0;
    for(int i=1;i<=m;i++){
        memset(p,0,sizeof(p));
        ans+=dfs(i);
    }
    printf("%d\n",ans);
    for(int i=1;i<=m;i++)
        if(to[i])printf("%d %d\n",i,to[i]);
    return 0;
}

深深地感到自己的弱小。

飞行员配对方案问题

原文:https://www.cnblogs.com/syzf2222/p/12349249.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!