POJ 2239-Selecting Courses

来源:互联网 发布:c语言温度转换 编辑:程序博客网 时间:2024/05/20 02:56

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.
For each test case, output one integer, which is the maximum number of courses Li Ming can select.

题目大意:

在大学里有许许多多的课程,现在小明需要去选择课程,他想尽可能多的选择课程。学校里有n(1 <= n <= 300)个课程,并且学校规定,每周里的每天有12节课,那么一周就有7*12节课。要求算出小明最多可以选择多少个课程。
输入有多组数据:
每组数据第一行为n,代表有n个课程。接下来n行,每行第一个数字x代表这个课程在这一周里面共有x次课。
然后跟着x对数字,第一个p(1 <= p <= 7) 代表这周的哪一天,第二个q(1 <= q <= 12)代表这天的哪一节课.。
如果几个课程都在那d天的那c节课上课,那么你需要选择其中的一个,而不能选择多个课程

Sample Input

51 1 12 1 1 2 21 2 22 3 2 3 31 3 3

Sample Output

4

题解

最大二分匹配。将n种课程和7*12节课匹配即可。

网络流做法:16ms

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<cmath>#include<algorithm>#define inf 1<<30#define T 400using namespace std;int n,zz,head[500];//7*12=84struct bian{int to,nx,v;} e[60000];int h[500],q[500],ans;void insert(int x,int y,int z){zz++; e[zz].to=y; e[zz].v=z; e[zz].nx=head[x]; head[x]=zz;zz++; e[zz].to=x; e[zz].v=0; e[zz].nx=head[y]; head[y]=zz;}void init(){int i,j,t,x,y,k;memset(head,0,sizeof(head));zz=1;for(i=1;i<=n;i++)   {insert(0,i,1);scanf("%d",&t);    for(j=1;j<=t;j++)       {scanf("%d%d",&x,&y);    insert(i,n+(x-1)*12+y,1);   }   }for(i=n+1;i<=n+84;i++) insert(i,T,1);}bool bfs(){memset(h,-1,sizeof(h));h[0]=0; q[0]=0;int i,t=0,w=1,p,x;while(t<w)   {x=q[t]; t++;for(i=head[x];i;i=e[i].nx)       {p=e[i].to;if(h[p]<0&&e[i].v)       {h[p]=h[x]+1;    q[w]=p; w++;   }   }   }if(h[T]==-1) return false;return true;}int dfs(int x,int f){if(x==T) return f;int usd=0,rest,i,p;for(i=head[x];i;i=e[i].nx)   {p=e[i].to;if(h[p]==h[x]+1&&e[i].v)   {rest=f-usd;    rest=dfs(p,min(e[i].v,rest));    e[i].v-=rest;    e[i^1].v+=rest;usd+=rest;if(usd==f) return f;   }   }if(usd==0) h[x]=-1;return usd;}void dinic(){ans=0;while(bfs()) ans+=dfs(0,inf);}int main(){while(scanf("%d",&n)!=EOF)   {init(); dinic();    printf("%d\n",ans);   }return 0;} 


匈牙利算法:16ms。

话说刚学这种算法没多久,所以理解地不是很透彻。解释详见http://blog.csdn.net/akof1314/article/details/4421262

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<cmath>#include<algorithm>#define inf 1<<30#define T 400using namespace std;int n,zz,head[500];//7*12=84struct bian{int to,nx,v;} e[60000];int vis[500],mark[500];void insert(int x,int y,int z){zz++; e[zz].to=y; e[zz].v=z; e[zz].nx=head[x]; head[x]=zz;}void init(){int i,j,t,x,y,k;memset(head,0,sizeof(head));zz=0;for(i=1;i<=n;i++)   {scanf("%d",&t);    for(j=1;j<=t;j++)       {scanf("%d%d",&x,&y);    insert(i,n+(x-1)*12+y,1);   }   }}bool dfs(int x){int i,p;for(i=head[x];i;i=e[i].nx)   {p=e[i].to;    if(!vis[p])       {vis[p]=1;    if(mark[p]==-1||dfs(mark[p]))       {mark[p]=x;    return true;   }   }   }return false;}void hungary(){int ct=0,i;memset(mark,-1,sizeof(mark));for(i=1;i<=n+84;i++)   {memset(vis,0,sizeof(vis));    if(dfs(i)) ct++;   }printf("%d\n",ct);}int main(){while(scanf("%d",&n)!=EOF)   {init(); hungary();}return 0;} 
0 0
原创粉丝点击