首页 > 其他 > 详细

poj 2239 Selecting Courses 二分图最大匹配

时间:2014-04-01 08:43:29      阅读:519      评论:0      收藏:0      [点我收藏+]

链接:http://poj.org/problem?id=2239

题意:又是选课!N种课程,每种课程在不同时间可以选择,总共有7天*12节的时间可以选择课程,问最多可以选多少种课。

思路:建图。X区为12*7个结点,Y区为N种课,直接求二分最大匹配即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define maxn 500
#define maxm 60005
using namespace std;
int head[maxn];
int t_c,t_s;
struct Edge
{
    int v,w;
    int next;
} edge[maxm];
int top=0;
int add_edge(int u,int v)
{
    edge[top].v=v;
    edge[top].next=head[u];
    head[u]=top++;
    return 0;
}
int from[maxn],tt;
bool use[maxn];
bool match(int x)
{
    for(int i=head[x]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!use[v])
        {
            use[v]=1;
            if(from[v]==-1||match(from[v]))
            {
                from[v]=x;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    tt=0;
    mem(from,-1);
    for(int i=1; i<=t_c; i++)
    {
        mem(use,0);
        if(match(i))
            tt++;
    }
    return tt;
}
int init()
{
    mem(head,-1);
    top=0;
    return 0;
}
int main()
{
    int ttt;
    while(scanf("%d",&t_c)!=EOF)
    {
        init();
        for(int i=1; i<=t_c; i++)
        {
            scanf("%d",&ttt);
            for(int j=1; j<=ttt; j++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                add_edge(i,t_c+(a-1)*12+b);
            }
        }
        int res=hungary();
        printf("%d\n",res);
    }
    return 0;
}


poj 2239 Selecting Courses 二分图最大匹配,布布扣,bubuko.com

poj 2239 Selecting Courses 二分图最大匹配

原文:http://blog.csdn.net/ooooooooe/article/details/22709693

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