POJ 2239 Selecting Courses EK!匈牙利!SAP?
来源:互联网 发布:在mac上安装jmeter 编辑:程序博客网 时间:2024/05/22 06:12
点击打开链接
Selecting Courses
Time Limit: 1000MS Memory Limit: 65536KTotal 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?
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
51 1 12 1 1 2 21 2 22 3 2 3 31 3 3
Sample Output
4
Source
POJ Monthly,Li Haoyuan
大学里面有很多门课程,一周七天,一天12节课,但是有的课程之间会有冲突。Li Ming很爱学习,所以想要学习尽可能多的课程,求它最多能选择多少门课程。
用二分匹配来做的话,比较简单,效率也高,将课程作为一个集合,将所有的课程时间为一个集合(12*7=84节课)。然后将课程和时间连接,求最大匹配数。
//4360K47MS#include<stdio.h>#include<string.h>#define M 1007int 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
最大流就是所求。
//1848K375MS#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define INF 9999999int 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;}
0 0
- POJ 2239 Selecting Courses EK!匈牙利!SAP?
- poj 匈牙利二分匹配算法2239 Selecting Courses
- POJ 2239 Selecting Courses(二分匹配匈牙利啊)
- POJ 2239 Selecting Courses
- poj 2239 Selecting Courses
- POJ-2239 Selecting Courses
- poj 2239 Selecting Courses
- POJ 2239 Selecting Courses
- POJ-2239-Selecting Courses
- poj 2239 Selecting Courses
- POJ 2239-Selecting Courses
- poj 2239 Selecting Courses
- poj 2239 Selecting Courses
- poj 2239 Selecting Courses
- poj 2239 Selecting Courses
- poj 2239 Selecting Courses
- poj 2239 Selecting Courses 匹配
- POJ 2239:Selecting Courses 选课
- 转android gralloc流程分析for msm8960
- POJ 2976 Dropping tests 01分数规划
- glibc改进
- 多功能数字收音机
- 写给静不下心来的朋友们
- POJ 2239 Selecting Courses EK!匈牙利!SAP?
- HBase写数据过程
- CString 如何转化为 char* ?
- 暂停更新博客,请谅解!
- linux0.11调试
- effective c++ 之代替#define
- 使用过滤器来检测session失效
- Android平台,C/C++代码内存对齐问题(signal SIGBUS Error)
- JAVA小练习-排序算法