POJ - 2239 - Selecting Courses

先上题目

Selecting Courses
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7781 Accepted: 3444

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

  这一题的意思是给你n种不同的课,每种课有不同的上课时间,问你最多可以选多少节课。
  这是一条二分图最大匹配的基础题目。第一次做,在看完匈牙利算法以后用了邻接矩阵保存不同之间的关系,过了以后换成邻接表来过这题。感觉邻接矩阵写起来比较直观,但是邻接表的话处理速度更加快。
  用矩阵和邻接表的其中的一个区别就是,矩阵每一次都要判断课程和时间是否有联系(就是这节课在这个时间可不可以上),而用邻接表可以省掉这个判,因为出现在邻接表就代表它已经有联系存在。这里我用一个小的二维数组来保存每一天每一个时间段的信息。
上代码:
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define ms(x) memset(x,0,sizeof(x));
 4 using namespace std;
 5 
 6 int result[10][15],nx,dd,dt;
 7 bool data[400][10][15],state[10][15];
 8 
 9 bool Find(int a)
10 {
11     int i,j;
12     for(i=1;i<=dd;i++)
13     {
14         for(j=1;j<=dt;j++)
15         {
16             if(data[a][i][j] && !state[i][j])
17             {
18                 state[i][j]=1;
19                 if(result[i][j]==0 || Find(result[i][j]))
20                 {
21                     result[i][j]=a;
22                     return 1;
23                 }
24             }
25         }
26     }
27     return 0;
28 }
29 
30 int main()
31 {
32     int i,j,d,t,m,count;
33     //freopen("data.txt","r",stdin);
34     while(scanf("%d",&nx)!=EOF)
35     {
36         ms(data);
37         ms(result);
38         dd=-1;
39         dt=-1;
40         for(i=1;i<=nx;i++)
41         {
42             scanf("%d",&m);
43             for(j=0;j<m;j++)
44             {
45                 scanf("%d %d",&d,&t);
46                 data[i][d][t]=1;
47                 dd=d>dd ? d : dd;
48                 dt=t>dt ? t : dt;
49             }
50         }
51         count=0;
52         for(i=1;i<=nx;i++)
53         {
54             ms(state);
55             if(Find(i)) count++;
56         }
57         printf("%d\n",count);
58     }
59     return 0;
60 }
邻接矩阵

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAX 400
 4 #define ms(x) memset(x,0,sizeof(x))
 5 #define ms_(x) memset(x,-1,sizeof(x))
 6 using namespace std;
 7 
 8 int head[MAX],res[10][15],nx,tot;
 9 bool state[10][15];
10 
11 typedef struct
12 {
13     int d,t;
14     int next;
15 }node;
16 
17 node c[MAX*10*15];
18 
19 bool Find(int a)
20 {
21     int i;
22     for(i=head[a];i!=-1;i=c[i].next)
23     {
24         if(!state[c[i].d][c[i].t])
25         {
26             state[c[i].d][c[i].t]=1;
27             if(res[c[i].d][c[i].t]==0 || Find(res[c[i].d][c[i].t]))
28             {
29                 res[c[i].d][c[i].t]=a;
30                 return 1;
31             }
32         }
33     }
34     return 0;
35 }
36 
37 
38 void add(int a,int d,int t)
39 {
40     c[tot].d=d; c[tot].t=t; c[tot].next=head[a]; head[a]=tot++;
41 }
42 
43 int main()
44 {
45     int m,i,t,d,count;
46     //freopen("data.txt","r",stdin);
47     while(scanf("%d",&nx)!=EOF)
48     {
49         ms_(head);
50         ms(c);
51         for(i=1;i<=nx;i++)
52         {
53              scanf("%d",&m);
54              while(m--)
55              {
56                  scanf("%d %d",&d,&t);
57                 add(i,d,t);
58              }
59         }
60         count=0;
61         ms(res);
62         for(i=1;i<=nx;i++)
63         {
64             ms(state);
65             if(Find(i)) count++;
66         }
67         printf("%d\n",count);
68     }
69     return 0;
70 }
邻接表

 




转载于:https://www.cnblogs.com/sineatos/p/3233910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值