首页 > 代码库 > poj 1698 Alice's Chance(网络流)
poj 1698 Alice's Chance(网络流)
Alice‘s Chance
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5280 | Accepted: 2171 |
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
2、每一天都当做一个点,然后如果这一天能演某个电影,则把该点和这个电影连一条边,边权为1;同时,把这一点和汇点连一条边,边权也为1.
3、求超级源点到汇点的最大流,若最大流等于总天数则输出Yes!!
#include"stdio.h" #include"string.h" #include"queue" using namespace std; #define N 505 const int inf=10000000; int g[N][N]; int pre[N],mark[N]; int min(int a,int b) { return a<b?a:b; } int ek(int n) { int i,u,ans=0; while(1) { queue<int>q; q.push(0); memset(mark,0,sizeof(mark)); memset(pre,-1,sizeof(pre)); mark[0]=1; while(!q.empty()) { u=q.front(); q.pop(); for(i=0;i<=n;i++) { if(!mark[i]&&g[u][i]) { mark[i]=1; pre[i]=u; q.push(i); } } } if(pre[n]==-1) break; int d=inf; for(i=n;i!=0;i=pre[i]) { d=min(d,g[pre[i]][i]); } for(i=n;i!=0;i=pre[i]) { g[pre[i]][i]-=d; g[i][pre[i]]+=d; } ans+=d; } //printf("%d\n",ans); return ans; } int main() { int i,j,k,n,t,u,sum,T; int a[22][8],d[22],w[22]; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(g,0,sizeof(g)); sum=t=0; //汇点 for(i=1;i<=n;i++) { for(j=0;j<7;j++) { scanf("%d",&a[i][j]); } scanf("%d%d",&d[i],&w[i]); if(w[i]>t) t=w[i]; } t=t*7+n+1; //汇点的下标 for(i=1;i<=n;i++) { g[0][i]=d[i]; //超级源点到一般源点(电影所需天数)的边权 for(k=0;k<w[i];k++) { for(j=0;j<7;j++) { if(a[i][j]) { u=k*7+j+n+1; g[i][u]=g[u][t]=1; } } } sum+=d[i]; } if(sum==ek(t)) printf("Yes\n"); else printf("No\n"); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。