【POJ - 1698】Alice's Chance(网络流最大流,建图)
題干:
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.?
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 2Sample Output
Yes NoHint
A proper schedule for the first test case:date Sun Mon Tue Wed Thu Fri Satweek1 film1 film2 film1 film1week2 film1 film2 film1 film1week3 film1 film2 film1 film1week4 film2 film2 film2題目大意:
alice接了N部電影的拍攝工作,所有電影在同一天開(kāi)工。對(duì)于每個(gè)電影來(lái)說(shuō)(第i個(gè)),每周只有固定幾天拍,且必須在周內(nèi)拍完,alice至少需要去拍天。問(wèn)alice能否完成所有電影拍攝。alice每天只能去拍一部電影。
解題報(bào)告:
將電影也看做節(jié)點(diǎn),將每一天看做一個(gè)節(jié)點(diǎn),因?yàn)樽疃?0周,所以最多350個(gè)節(jié)點(diǎn)。從源點(diǎn)出發(fā)流向每部電影,流量就是需要拍攝的天數(shù),從每部電影到拍攝周內(nèi)拍攝的那些天,流量是1,因?yàn)槊刻熘荒芘囊徊?#xff0c;同理這些天都連接到匯點(diǎn),流量也是1,然后看源點(diǎn)能否滿(mǎn)流就可以了。
AC代碼:
#include<cstring> #include<cstdio> #include<algorithm> #include<iostream> using namespace std; int tot; struct Edge {int to,ne,w; } e[100005 * 2]; int head[10005]; int st,ed; int dis[10050],q[10005];//一共多少個(gè)點(diǎn)跑bfs,dis數(shù)組和q數(shù)組就開(kāi)多大。 void add(int u,int v,int w) {e[++tot].to=v; e[tot].w=w; e[tot].ne=head[u]; head[u]=tot;e[++tot].to=u; e[tot].w=0; e[tot].ne=head[v]; head[v]=tot; } bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1; } int dfs(int cur,int limit) {//limit為源點(diǎn)到這個(gè)點(diǎn)的路徑上的最小邊權(quán) if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) { if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow; } int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans; } int a[100005]; int main() {int t,n;cin>>t;while(t--) {tot=1;memset(head,-1,sizeof head);scanf("%d",&n);st=2000,ed=2001;//自己定就行了 for(int i = 101; i<=500; i++) /*add(i,i+1000,1),*/add(i,ed,1);//讓每一天的拆點(diǎn)連起來(lái),并且把后置點(diǎn)和ed連起來(lái) //其實(shí)上一步是不需要拆點(diǎn)的,因?yàn)槊總€(gè)點(diǎn)和終點(diǎn)只有一個(gè)連邊,所以不需要控制這個(gè)點(diǎn)只能被選擇一次。 但是拆不拆點(diǎn)都可以過(guò)。int ans = 0;for(int i = 1; i<=n; i++) {for(int j = 1; j<=9; j++) scanf("%d",a+j);add(st,i,a[8]);//我要給這個(gè)電影提供a[8]這么多天的流量。 ans += a[8];for(int j = 1; j<=7; j++) {if(a[j] == 0) continue;for(int k = 0; k<a[9]; k++) {//k=1,k<a[9]也可以? add(i,100+7*k+j,1);}}}int out = dinic();if(ans == out) printf("Yes\n");else printf("No\n"); }return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【POJ - 1698】Alice's Chance(网络流最大流,建图)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 信用卡申请冻结 冻结不等于挂失别对它置之
- 下一篇: soundtrax.exe - soun