Selecting Courses
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 7144 | Accepted: 3141 |
题目链接:http://poj.org/problem?id=2239
Description
It is well knownthat it is not easy to select courses in the college, for there is usuallyconflict among the time of the courses. Li Ming is a student who loves studyevery much, and at the beginning of each term, he always wants to selectcourses as more as possible. Of course there should be no conflict among thecourses he selects.
There are 12 classes every day, and 7 days every week. There are hundreds ofcourses in the college, and teaching a course needs one class each week. Togive 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 betaught both at the 7-th class on Tuesday and 12-th class on Wednesday, youshould assume that there is no difference between the two classes, and thatstudents can select any class to go. At the different weeks, a student can evengo to different class as his wish. Because there are so many courses in thecollege, selecting courses is not an easy job for Li Ming. As his good friends,can you help him?
Input
The input containsseveral 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 linesrepresent 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 thecourse. 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 thep-th day of a week.
Output
For each testcase, output one integer, which is the maximum number of courses Li Ming canselect.
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
题意:
大概题意是讲选课,怎么选才能达到最多,第一个数是讲一共有几门课程,每一门课程在每个星期的第p天的第q堂课。问小明最多能选多少课程。
解题思路:
这个题目是属于二分匹配的题,该题一个比较难的就是建图的时候,怎么来表示?我是用1到84来表示的,具体的就是给一个星期的84堂课进行标号12*(第p天-1)+第q节课来表示。然后就用匈牙利算法来求出答案。
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 305
#define maxCourse 100
using namespace std;
int map[maxn][maxCourse];
int visited[maxn];
int mak[maxn];
int ts;
bool bfs(int x)
{
int i;
for(i=1;i<maxCourse;i++)
{
if(!visited[i] && map[x][i])
{
visited[i]=1;
if(!mak[i] || bfs(mak[i]))
{
mak[i]=x;
return true;
}
}
}
return false;
}
void hungary()
{
int i;
int count=0;
memset(mak,0,sizeof(mak));
for(i=1;i<=ts;i++)
{
memset(visited,0,sizeof(visited));
if(bfs(i))
{
count++;
}
}
printf("%d\n",count);
}
int main()
{
int i;
int p,q;
while(scanf("%d",&ts)!=EOF)
{
memset(map,0,sizeof(map));
for(i=1;i<=ts;i++)
{
int t;
scanf("%d",&t);
while(t--)
{
//第p天的第q堂课
scanf("%d%d",&p,&q);
//一星期有7*12节课,把它具体做标号
int temp=(p-1)*12+q;
map[i][temp]=1;
}
}
hungary();
}
return 0;
}