poj 2239 Selecting Courses(二分图最大匹配or最大流)

46 篇文章 0 订阅
20 篇文章 0 订阅
Selecting Courses
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7633 Accepted: 3365

Description

It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning of each term, he always wants to select courses as more as possible. Of course there should be no conflict among the courses he selects.

There are 12 classes every day, and 7 days every week. There are hundreds of courses in the college, and teaching a course needs one class each week. To give students more convenience, though teaching a course needs only one class, a course will be taught several times in a week. For example, a course may be taught both at the 7-th class on Tuesday and 12-th class on Wednesday, you should assume that there is no difference between the two classes, and that students can select any class to go. At the different weeks, a student can even go to different class as his wish. Because there are so many courses in the college, selecting courses is not an easy job for Li Ming. As his good friends, can you help him?

Input

The input contains several cases. For each case, the first line contains an integer n (1 <= n <= 300), the number of courses in Li Ming's college. The following n lines represent n different courses. In each line, the first number is an integer t (1 <= t <= 7*12), the different time when students can go to study the course. Then come t pairs of integers p (1 <= p <= 7) and q (1 <= q <= 12), which mean that the course will be taught at the q-th class on the p-th day of a week.

Output

For each test case, output one integer, which is the maximum number of courses Li Ming can select.

Sample Input

5
1 1 1
2 1 1 2 2
1 2 2
2 3 2 3 3
1 3 3

Sample Output

4
 
题目大意:学校开设多门课,每门课会在第 p 天(1<=i<=7)的第 q (1<=j<=12)节课等多个时间内上,他们都没区别。求一个选课方案使得能选的课最多。

思路: 建图方式:N={课程},M={课程举行的时间} 。转化成二分图的最大匹配问题
求二分图的最大匹配,用匈牙利算法:
 
 
•算法轮廓:
•(1)置M为空
•(2)找出一条增广路径P,通过取反操作获得更大的匹配M’代替M
•(3)重复(2)操作直到找不出增广路径为止
AC代码:
#include <iostream>
#include <cstring>

using namespace std;
const int maxn=305;
bool map[maxn][maxn],used[maxn];  //map记录相连的边
int match[maxn];
int m,n;
bool find(int x)         //从x开始找增广路
{
    for(int i=1; i<=m; i++)
        if(!used[i]&&map[x][i])           //如果节点i与x相连并且未被查找过
        {
            used[i]=true;                  //标记i为已查找过
            if(match[i]==-1||find(match[i]))    //如果i未在前一个匹配M中或i在匹配M中,但是从与i相连的节点出发可以有增广路
            {
                match[i]=x;               //记录查找成功记录
                return true;             //返回查找成功
            }
        }
    return false;
}
int MMG()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1; i<=n; i++)
    {
        memset(used,false,sizeof(used));      //清空上次搜索
        if(find(i)) ans++;              //从节点i尝试扩展
    }
    return ans;
}
int main()
{
    int t,p,q;
    while(cin>>n)
    {
        memset(map,false,sizeof(map));
        m=0;
        for(int i=1; i<=n; i++)
        {
            cin>>t;
            while(t--)
            {
                cin>>p>>q;
                map[i][(p-1)*12+q]=true;
                m=max(m,(p-1)*12+q);
            }
        }
        cout<<MMG()<<endl;
    }
    return 0;
}


最大流:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;

const int INF=1000000000;
const int maxn=500;
struct Edge{
    int u,v,cap,flow,next;
}et[maxn*maxn];
int cnt[maxn],low[maxn],dis[maxn],cur[maxn],pre[maxn],eh[maxn];
int s,t,n,num;
void init(){
    memset(eh,-1,sizeof(eh));
    s=num=0;
    t=n+84+1;
}
void add(int u,int v,int cap,int flow){
    Edge e={u,v,cap,flow,eh[u]};
    et[num]=e;
    eh[u]=num++;
}
void addedge(int u,int v,int cap){
    add(u,v,cap,0);
    add(v,u,0,0);
}
int isap(int s,int t,int n){
    int u,v,now,flow=0;
    memset(cnt,0,sizeof(cnt));
    memset(low,0,sizeof(low));
    memset(dis,0,sizeof(dis));
    for(u=0;u<=n;u++) cur[u]=eh[u];
    low[s]=INF,cnt[0]=n,u=s;
    while(dis[s]<n)
    {
        for(now=cur[u];now!=-1;now=et[now].next)
        if(et[now].cap-et[now].flow&&dis[u]==dis[v=et[now].v]+1) break;
        if(now!=-1)
        {
            cur[u]=pre[v]=now;
            low[v]=min(et[now].cap-et[now].flow,low[u]);
            u=v;
            if(u==t)
            {
                for(;u!=s;u=et[pre[u]].u)
                {
                    et[pre[u]].flow+=low[t];
                    et[pre[u]^1].flow-=low[t];
                }
                flow+=low[t];
                low[s]=INF;
            }
        }
        else
        {
            if(--cnt[dis[u]]==0) break;
            dis[u]=n,cur[u]=eh[u];
            for(now=eh[u];now!=-1;now=et[now].next)
            if(et[now].cap-et[now].flow&&dis[u]>dis[et[now].v]+1)
            dis[u]=dis[et[now].v]+1;
            cnt[dis[u]]++;
            if(u!=s) u=et[pre[u]].u;
        }
    }
    return flow;
}
int main()
{
    int k,p,q;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            addedge(s,i,1);
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d%d",&p,&q);
                addedge(i,(p-1)*12+q+n,1);
            }
        }
        for(int i=n+1;i<=n+84;i++) addedge(i,t,1);
        printf("%d\n",isap(s,t,t+1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值