POJ 2239题

//类型:二分图的最大匹配:使用匈牙利算法实现
#include <stdio.h>
#include <string.h>
//#include <conio.h>
#define arraysize1 301       //课程数
#define arraysize2 85        //时间(将时间统一化成节数)
int map[arraysize1][arraysize2];
int match[arraysize1];
bool final[arraysize1];
int n;
bool DFS(int p)
{
     int i,j;
     int temp;
     for(i=1;i<85;++i)
     {
         if(map[p][i] && !final[i])
         {
              final[i] = true;
              temp = match[i];
              match[i] = p;
              if(temp==0 || DFS(temp)) return true;
              match[i] = temp;       
         }
     }
     return false;
}
int mat()
{
    int i,j;
    int maxmatch = 0;
    for(i=1;i<n+1;++i)
    {
        memset(final,0,sizeof(final));
        if(DFS(i)) maxmatch++;
    }
    return maxmatch;
}
int main()
{
    //freopen("1.txt","r",stdin);
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
         int t;
         memset(map,0,sizeof(map));
         memset(match,0,sizeof(match));
         for(j=1;j<n+1;++j)
         {
            int p,q;
            int time;
            scanf("%d",&t);
            for(i=1;i<t+1;++i)
            {
                scanf("%d%d",&p,&q);
                time = (p-1)*12+q;  //将时间化成节数
                map[j][time] = 1;
            }           
         }
         printf("%d\n",mat());
    }
    //getch();
    return 0;
}

posted @ 2010-05-06 22:46  北海小龙  阅读(180)  评论(0编辑  收藏  举报