hdu 2063 过火车(二分图匹配--hungarian)

Problem Description
RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?
 

Input
输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
 

Output
对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。
 

Sample Input
  
  
6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
 

Sample Output
  
  
3
二分图匹配,裸的匈牙利算法。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

struct Edge
{
    int from,to;
    Edge(int a,int b):from(a),to(b){}
};

vector<int> G[1100];
vector<Edge> edge;
int k,m,n;
int check[1100];
int match[1100];

bool dfs(int u)
{
    for(vector<int>::iterator i=G[u].begin();i!=G[u].end();i++)
    {
        int v=edge[*i].to;
    if(!check[v])
    {
        check[v]=1;
        if(match[v]==-1)
        {
            match[u]=v;
            match[v]=u;
            return true;
        }
        else if(dfs(match[v]))
        {
            match[u]=v;
            match[v]=u;
            return true;
        }
    }


    }
    return false;
}

int hungrian()
{
    memset(match,-1,sizeof(match));
    int ans=0;
    for(int i=0;i<m;i++)
    {
        if(match[i]==-1)//没被匹配过
        {
            memset(check,0,sizeof(check));
            if(dfs(i))//寻找左边第i个点的增广路
            ans++;
            //for(int i=0;i<m;i++)
               // cout<<match[i]<<endl;
        }
    }
    return ans;
}

int main()
{

    while(scanf("%d",&k)!=EOF&&k)
    {
        scanf("%d %d",&m,&n);

        memset(match,0,sizeof(match));
        edge.clear();
        for(int i=0;i<m+n;i++)
            G[i].clear();
        for(int i=0;i<k;i++)
        {
            int from,to;
            scanf("%d %d",&from,&to);
            edge.push_back(Edge(from-1,to+m-1));
            G[from-1].push_back(i);
            G[to+m-1].push_back(i);
        }
        cout<<hungrian()<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值