POJ 1698 Alice's Chance(网络流+构图)

题目链接:http://poj.org/problem?id=1698

题目:

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,

  1. it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days;
  2. Alice should work for it at least for specified number of days;
  3. 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题意:Alice去演n部电影,每部电影只能在每个星期的某几天演,需要演d天,截止星期为w。问Alice能否把这n部电影都演完。题解:挺简单的一道题。创建一个超级源点和汇点,源点和每部电影连接,容量为电影需要演的天数;每部电影和可以演的日期连接,容量为1;350天全部和超级汇点连接,容量为1。记得日期由星期都转换成天(最多50个星期,所以最多50*7=350天),注意答案的大小写...
 1 //POJ 1698
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8
 9 const int N=444;
10 const int M=2*N*N;
11 const int INF=0x3f3f3f3f;
12 int s,t,cnt;
13 int Head[N],Depth[N],cur[N];
14 int Next[M],V[M],W[M];
15
16 void init(){
17     cnt=-1;
18     memset(Head,-1,sizeof(Head));
19     memset(Next,-1,sizeof(Next));
20 }
21
22 void add_edge(int u,int v,int w){
23     cnt++;Next[cnt]=Head[u];V[cnt]=v;W[cnt]=w;Head[u]=cnt;
24     cnt++;Next[cnt]=Head[v];V[cnt]=u;W[cnt]=0;Head[v]=cnt;
25 }
26
27 bool bfs(){
28     queue <int> Q;
29     while(!Q.empty()) Q.pop();
30     memset(Depth,0,sizeof(Depth));
31     Depth[s]=1;
32     Q.push(s);
33     while(!Q.empty()){
34         int u=Q.front();Q.pop();
35         for(int i=Head[u];i!=-1;i=Next[i]){
36             if((W[i]>0)&&(Depth[V[i]]==0)){
37                 Depth[V[i]]=Depth[u]+1;
38                 Q.push(V[i]);
39             }
40         }
41     }
42     if(Depth[t]==0) return 0;
43     return 1;
44 }
45
46 int dfs(int u,int dist){
47     if(u==t) return dist;
48     for(int& i=cur[u];i!=-1;i=Next[i]){
49         if((Depth[V[i]]==Depth[u]+1)&&W[i]!=0){
50             int di=dfs(V[i],min(dist,W[i]));
51             if(di>0){
52                 W[i]-=di;
53                 W[i^1]+=di;
54                 return di;
55             }
56         }
57     }
58     return 0;
59 }
60
61 int Dinic(){
62     int ans=0;
63     while(bfs()){
64         for(int i=0;i<=371;i++) cur[i]=Head[i];
65         while(int d=dfs(s,INF)) ans+=d;
66     }
67     return ans;
68 }
69
70 int main(){
71     int T,n,D,W;
72     scanf("%d",&T);
73     while(T--){
74         int day[8],sum=0;
75         scanf("%d",&n);
76         init();
77         s=0;t=371;
78         for(int i=1;i<=n;i++){
79             for(int j=1;j<=7;j++) scanf("%d",&day[j]);
80             scanf("%d %d",&D,&W);
81             sum+=D;
82             add_edge(s,350+i,D);
83             for(int j=1;j<=W;j++){
84                 for(int k=1;k<=7;k++) {if(day[k]>0) add_edge(350+i,(j-1)*7+k,1);}
85             }
86         }
87         for(int i=1;i<=350;i++) add_edge(i,t,1);
88         if(sum==Dinic()) printf("Yes\n");
89         else printf("No\n");
90     }
91
92     return 0;
93 }
 

POJ 1698 Alice's Chance(网络流+构图)

时间: 2024-08-25 15:53:37

POJ 1698 Alice's Chance(网络流+构图)的相关文章

poj 1698 Alice&#39;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 invit

POJ 1698 Alice&#39;s Chance 网络流(水

题目链接:点击打开链接 题目大意:   有个人想拍n部电影,每部电影限定每周哪几天可以拍 并且必须在第ki周之前把这部电影拍完,问能否拍完n部电影 解题思路:  把每部电影当作一个顶点,源点指向这些顶点,容量为该电影需要拍多少天 然后把每一天都当作顶点,某个工作可以在这天完成就连容量为1大边 每天的顶点指向汇点,容量也为1 最后求出最大流,满流则说明可以完成这些工作 啦啦啦 #include <cstdio> #include <cstring> #include <algo

POJ 1698 Alice&#39;s Chance(网络流之最大流)

题目地址:POJ 1698 水题..将每部电影与它可以演的那一天连边就行了.建二分图.用二分最大匹配也完全可以做. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queu

poj 1698 Alice&#39;s Chance 拆点最大流

将星期拆点,符合条件的连边,最后统计汇点流量是否满就行了,注意结点编号. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #include<s

POJ 1698 Alice&#39;s Chance(最大流+拆点)

POJ 1698 Alice's Chance 题目链接 题意:拍n部电影,每部电影要在前w星期完成,并且一周只有一些天是可以拍的,每部电影有个需要的总时间,问是否能拍完电影 思路:源点向每部电影连边,容量为d,然后每部电影对应能拍的那天连边,由于每天容量限制是1,所以进行拆点,然后连向汇点即可 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> usi

poj 1698 Alice&#39;s Chance SAP 最大流

[题意]:Alice有n部电影要拍,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影 [建图]:源点与每部电影连边,容量为天数,每部电影与可以拍该电影的那些天数连边,容量为1,再所有的天数与汇点连边容量为1. 要注意天数和汇点连边的时候不要重复了,我这里用的数组不会出现这种情况. 1 #include<iostream> 2 #include<vector> 3 #include<cstring&g

poj 1698 Alice&#39;s Chance 二分图多重匹配

题意: 一个演员要拍n部电影,每部电影只能在一周的特定几天拍(如周2,周4,周5),第i部电影要拍di天,必须要在wi周拍完,问演员是否可以完成任务. 分析: 建二分图,转化为二分图的多重匹配. 代码: //poj 1698 //sep9 #include <iostream> using namespace std; const int maxX=64*7; const int maxY=64; int g[maxX][maxY],match[maxY][maxX]; int vis[max

POJ 1698 Alice&amp;#39;s Chance(最大流+拆点)

POJ 1698 Alice's Chance 题目链接 题意:拍n部电影.每部电影要在前w星期完毕,而且一周仅仅有一些天是能够拍的,每部电影有个须要的总时间,问能否拍完电影 思路:源点向每部电影连边,容量为d,然后每部电影相应能拍的那天连边,因为每天容量限制是1.所以进行拆点,然后连向汇点就可以 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> us

[poj1698]Alice&#39;s Chance[网络流]

[转]原文:http://blog.csdn.net/wangjian8006/article/details/7926040 题目大意:爱丽丝要拍电影,有n部电影,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影第一行代表有多少组数据对于每组数据第一行代表有n部电影接下来2到n+1行,每行代表一个电影,每行9个数,前面7个数,1代表拍,0代表不拍,第8个数代表要拍几天,第9个数代表有几个礼拜时间拍 解题思路:这题可以