首页 > 代码库 > POJ1698_Alice's Chance(二分图多重最大匹配/最大流)
POJ1698_Alice's Chance(二分图多重最大匹配/最大流)
解题报告
http://blog.csdn.net/juncoder/article/details/38237641
题目传送门
题意:
N个电影,每个电影在每一周有固定拍映时间,电影必须在W周前演完。有一个演员,他每天只能演一场电影,对于每部电影必须演完D天才算完。
思路:
二分图多重最大匹配问题,对于每个电影,源点与每个电影连上一条边容量为D,电影与每周7天对应拍映连线,容量为1,每周每天与汇点连线容量为1
在二分图最大匹配中,每个点(不管是X方点还是Y方点)最多只能和一条匹配边相关联,然而,我们经常遇到这种问题,即二分图匹配中一个点可以和多条匹配边相关联,但有上限,或者说,Li表示点i最多可以和多少条匹配边相关联。
在原图上建立源点S和汇点T,S向每个X方点连一条容量为该X方点L值的边,每个Y方点向T连一条容量为该Y方点L值的边,原来二分图中各边在新的网络中仍存在,容量为1(若该边可以使用多次则容量大于1),求该网络的最大流,就是该二分图多重最大匹配的值。
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #define inf 99999999 using namespace std; int mmap[400][400],n,l[400],m; int bfs() { memset(l,-1,sizeof(l)); queue<int>Q; Q.push(0); l[0]=0; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=0; i<=m; i++) { if(l[i]==-1&&mmap[u][i]) { l[i]=l[u]+1; Q.push(i); } } } if(l[m]>1) return 1; return 0; } int dfs(int x,int f) { int a; if(x==m)return f; for(int i=0; i<=m; i++) { if(l[i]==l[x]+1&&mmap[x][i]&&(a=dfs(i,min(f,mmap[x][i])))) { mmap[x][i]-=a; mmap[i][x]+=a; return a; } } l[x]=-1; return 0; } int main() { int t,_h[10],d,w; cin>>t; while(t--) { memset(mmap,0,sizeof(mmap)); memset(_h,0,sizeof(_h)); cin>>n; int mon=0; int sum=0; for(int i=1; i<=n; i++) { scanf("%d%d%d%d%d%d%d%d%d",&_h[1],&_h[2],&_h[3],&_h[4],&_h[5],&_h[6],&_h[7],&d,&w); mmap[0][i]=d; sum+=d; if(mon<w) mon=w; for(int j=0; j<w; j++) { for(int k=1; k<=7; k++) { if(_h[k]) mmap[i][n+j*7+k]=1; } } } m=n+mon*7+1; for(int i=n+1; i<=n+mon*7; i++) mmap[i][m]=1; int ans=0,a; while(bfs()) while(a=dfs(0,inf)) ans+=a; if(sum==ans) printf("Yes\n"); else printf("No\n"); } return 0; }
Alice‘s Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5277 | Accepted: 2168 |
Description
Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. Unfortunately, all these companies will start making the films at the same time, and the greedy Alice doesn‘t want to miss any of them!! You are asked to tell her whether she can act in all the films.
As for a film,
For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice should work for the film at least for 4 days; and it must be finished within 3 weeks. In this case she can work for the film on Monday of the first week, on Monday and Saturday of the second week, and on Monday of the third week.
Notice that on a single day Alice can work on at most ONE film.
As for a film,
- it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days;
- Alice should work for it at least for specified number of days;
- the film MUST be finished before a prearranged deadline.
For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice should work for the film at least for 4 days; and it must be finished within 3 weeks. In this case she can work for the film on Monday of the first week, on Monday and Saturday of the second week, and on Monday of the third week.
Notice that on a single day Alice can work on at most ONE film.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a single line containing an integer N (1 <= N <= 20), the number of films. Each of the following n lines is in the form of "F1 F2 F3 F4 F5 F6 F7 D W". Fi (1 <= i <= 7) is 1 or 0, representing whether the film can be made on the i-th day in a week (a week starts on Sunday): 1 means that the film can be made on this day, while 0 means the opposite. Both D (1 <= D <= 50) and W (1 <= W <= 50) are integers, and Alice should go to the film for D days and the film must be finished in W weeks.
Output
For each test case print a single line, ‘Yes‘ if Alice can attend all the films, otherwise ‘No‘.
Sample Input
2 2 0 1 0 1 0 1 0 9 3 0 1 1 1 0 0 0 6 4 2 0 1 0 1 0 1 0 9 4 0 1 1 1 0 0 0 6 2
Sample Output
Yes No
Hint
A proper schedule for the first test case: date Sun Mon Tue Wed Thu Fri Sat week1 film1 film2 film1 film1 week2 film1 film2 film1 film1 week3 film1 film2 film1 film1 week4 film2 film2 film2
Source
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。