题目链接:http://poj.org/problem?id=2239
题目大意:学校开设了n门课程,李明现在想选择尽可能多的课程,当然这些课程时间上不能冲突。其中每门课程每周都有一次或多次上课时间,李明只要选择其中任意一个时间去上课就行了。现在给出每门课程开课的时间,计算出不冲突的最大课程数目。其中一星期上7天课,每天12节课。
题解:典型的最大二分图匹配题目。其中每门课程作为点集L,所有课程的上课时间为R,求最大匹配数目。
算法:匈牙利算法
关于匈牙利算法,我发现了一个讲解的比较通俗易懂的博客:
http://blog.youkuaiyun.com/dark_scope/article/details/8880547
相信你看了之后一定可以理解的。
参考代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=300+7;
const int m=7*12;
bool g[maxn][m+1];
int belong[maxn]; //belong数组记录为每门课程分配的上课时间,
bool vis[m+1]; //0代表尚未分配上课时间
int n;
//匈牙利算法
bool find(int x){
for(int j=1;j<=m;j++){
if(g[x][j]&&!vis[j]){
vis[j]=true;
if(belong[j]==0||find(belong[j])){
belong[j]=x;
return true;
}
}
}
return false;
}
int calc(){
int ans=0;
for(int i=1;i<=n;i++){
memset(vis,false,sizeof(vis));
if(find(i))
ans++;
}
return ans;
}
int main(int argc, const char * argv[]) {
int t,p,q;
while(~scanf("%d",&n)){
memset(g,false,sizeof(g));
memset(belong,0,sizeof(belong));
for(int i=1;i<=n;i++){
scanf("%d",&t);
while(t--){
scanf("%d%d",&p,&q);
g[i][(p-1)*12+q]=true;
}
}
printf("%d\n",calc());
}
}