POJ 2239 Selecting Courses EK!匈牙利!SAP?


Selecting Courses
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8098 Accepted: 3586

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

Source

POJ Monthly,Li Haoyuan

大学里面有很多门课程,一周七天,一天12节课,但是有的课程之间会有冲突。Li Ming很爱学习,所以想要学习尽可能多的课程,求它最多能选择多少门课程。

用二分匹配来做的话,比较简单,效率也高,将课程作为一个集合,将所有的课程时间为一个集合(12*7=84节课)。然后将课程和时间连接,求最大匹配数。

//4360K	47MS
#include<stdio.h>
#include<string.h>
#define M 1007
int link[M],g[M][M];
bool vis[M];
bool find(int i)
{
    for(int j=1;j<=84;j++)
        if(g[i][j]&&!vis[j])
        {
            vis[j]=true;
            if(!link[j]||find(link[j]))
            {
                link[j]=i;
                return true;
            }
        }
    return false;
}
int main()
{
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        memset(link,0,sizeof(link));
        memset(g,0,sizeof(g));
        int count=0,knob,a,b;
        for(int i=1;i<=num;i++)
        {
            scanf("%d",&knob);
            while(knob--)
            {
                scanf("%d%d",&a,&b);
                g[i][(a-1)*12+b]=1;
            }
        }
        for(int i=1;i<=num;i++)
        {
            memset(vis,false,sizeof(vis));
            if(find(i))count++;
        }
        printf("%d\n",count);
    }
    return 0;
}

当然此题还可以用最大流的方法来解决,建图如下:

1:将源点和每门课程连接,容量为1.

2:将课程与相应的时间连接(时间为12*7=84),容量为1

3:将有课程的时间与汇点相连,容量为1

最大流就是所求。

//1848K	375MS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 9999999
int map[400][400],pre[400],flow[400][400],p[400],a[400];
int EK(int s,int t)
{
    int sum=0;
    queue<int>q;
    memset(flow,0,sizeof(flow));
    for(;;)
    {
        memset(a,0,sizeof(a));//记录残量
        a[s]=INF;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=0; i<=399; i++)
                if(!a[i]&&map[u][i]>flow[u][i])
                {
                    p[i]=u;//记录i的父亲节的是u
                    q.push(i);
                    a[i]=a[u]<map[u][i]-flow[u][i]?a[u]:map[u][i]-flow[u][i];
                }
        }
        if(!a[t])break;//如果残量是0的话,就找到最大流
        for(int i=t; i!=s; i=p[i])//每条路加上最小残量
        {
            flow[p[i]][i]+=a[t];
            flow[i][p[i]]-=a[t];
        }
        sum+=a[t];//记录流量
    }
    return sum;
}
int main()
{
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        int s=0,knob,aa,b;
        int t=399;
        memset(map,0,sizeof(map));
        memset(p,0,sizeof(p));
        memset(a,0,sizeof(a));
        for(int i=1; i<=num; i++)
        {
            scanf("%d",&knob);
            map[0][i]=1;
            while(knob--)
            {
                scanf("%d%d",&aa,&b);
                int c=num+(aa-1)*12+b;
                map[i][c]=1;
                map[c][t]=1;
            }
        }
        printf("%d\n",EK(0,t));
    }
    return 0;
}

一开始此题用的是SAP算法,可是不知哪出错,总是不对,无奈才改成EK算法。

小弟不才,愿路过的众神帮小弟看看,这哪里错了。。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 9999999
#define M 3007
#define MIN(a,b) a>b?b:a;
using namespace std;
struct E
{
    int v,w,next;
} edg[500000];
int dis[M],gap[M],head[M],nodes;
int sourse,sink,nn;
void addedge(int u,int v,int w)
{
    edg[nodes].v=v;
    edg[nodes].w=w;
    edg[nodes].next=head[u];
    head[u]=nodes++;
    edg[nodes].v=u;
    edg[nodes].w=0;
    edg[nodes].next=head[v];
    head[v]=nodes++;
}
int dfs(int src,int aug)
{
    if(src==sink)return aug;
    int left=aug,mindis=nn;
    for(int j=head[src]; j!=-1; j=edg[j].next)
    {
        int v=edg[j].v;
        if(edg[j].w)
        {
            if(dis[v]+1==dis[src])
            {
                int minn=MIN(left,edg[j].w);
                minn=dfs(v,minn);
                edg[j].w-=minn;
                edg[j^1].w+=minn;
                left-=minn;
                if(dis[sourse]>=nn)return aug-left;
                if(left==0)break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }

    if(left==aug)
    {
        if(!(--gap[dis[src]]))dis[sourse]=nn;
        dis[src]=mindis+1;
        gap[dis[src]]++;
    }
    return aug-left;
}
int sap(int s,int e)
{
    int ans=0;
    nn=e+1;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    sourse=s;
    sink=e;
    while(dis[sourse]<nn)
        ans+=dfs(sourse,inf);
    return ans;
}
int main()
{
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        int s=0,knob,a,b;
        nodes=0;
        memset(head,-1,sizeof(head));
        int t=num+85;
        for(int i=1; i<=num; i++)
        {
            scanf("%d",&knob);
            addedge(0,i,1);
            while(knob--)
            {
                scanf("%d%d",&a,&b);
                int c=num+(a-1)*12+b;
                addedge(i,c,1);
                addedge(c,t,1);
            }
        }
        int anss=sap(s,t);
        printf("%d\n",anss);
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值