poj1325二分图

题意:给出机器1 的n种工作模式,给出机器2的m种工作模式。 在给出k 个东西可以在 机器一的 ai 种模式或者在 机器二的 bi种模式工作。 切换机器的工作模式需要重启,问最少需要重启几次?

二分图:  最小点覆盖=最大匹配

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include<math.h>
using namespace std;
int n,m;
const int maxn=105;
int g[maxn][maxn];
int link[maxn*maxn];
int used[maxn];
int dfs(int x)
{

    for(int i=1;i<=m;i++){
        if(!used[i]&&g[x][i]){
            used[i]=1;
            if(link[i]==-1||dfs(link[i])){
                link[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

void solve()
{
    int ans=0;
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++){
        memset(used,0,sizeof(used));
        if(dfs(i)) ans++;
    }
    printf("%d\n",ans);
}

int main()
{
    int k;
    while(cin>>n,n){
        cin>>m>>k;
        memset(g,0,sizeof(g));
        for(int i=0;i<k;i++){
            int a;int b;int c;
            cin>>a>>b>>c;
            g[b][c]=1;
        }
        solve();
    }
    return 0;
}

 

posted on 2014-08-04 11:00  一个西瓜  阅读(149)  评论(0)    收藏  举报

导航