[POJ 1325] Machine Schedule

[题目链接]

          http://poj.org/problem?id=1325

[算法]

        二分图最小覆盖

[代码]

          

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
#define MAXN 110
#define MAXK 1010

struct edge
{
        int to,nxt;
} e[MAXK];

int i,n,m,k,ans,tot,t,x,y;
int match[MAXN << 1],head[MAXN << 1];
bool visited[MAXN << 1];

inline void addedge(int u,int v)
{
        tot++;
        e[tot] = (edge){v,head[u]};
        head[u] = tot; 
}
inline bool hungary(int u)
{
        int i,v;
        visited[u] = true;
        for (i = head[u]; i; i = e[i].nxt)
        {
                v = e[i].to;
                if (!visited[v])
                {
                        visited[v] = true;
                        if (!match[v] || hungary(match[v]))
                        {
                                match[v] = u;
                                return true;
                        }
                }
        }
        return false;
}

int main() 
{
        
        while (scanf("%d",&n) && n)
        {
                tot = 0;
                memset(head,0,sizeof(head));
                memset(match,0,sizeof(match));
                scanf("%d%d",&m,&k);
                for (i = 1; i <= k; i++)
                {
                        scanf("%d%d%d",&t,&x,&y);
                        if (x * y) addedge(x,y + n);        
                }
                ans = 0;
                for (i = 1; i <= n; i++)
                {
                        memset(visited,false,sizeof(visited));
                        if (hungary(i)) ans++;
                }
                printf("%d\n",ans);
        }
        
        return 0;
    
}

 

posted @ 2018-08-03 16:48  evenbao  阅读(132)  评论(0编辑  收藏  举报