题干:
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,
- 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 Satweek1 film1 film2 film1 film1week2 film1 film2 film1 film1week3 film1 film2 film1 film1week4 film2 film2 film2
题目大意:
alice接了N部电影的拍摄工作,所有电影在同一天开工。对于每个电影来说(第i个),每周只有固定几天拍,且必须在周内拍完,alice至少需要去拍天。问alice能否完成所有电影拍摄。alice每天只能去拍一部电影。
解题报告:
将电影也看做节点,将每一天看做一个节点,因为最多50周,所以最多350个节点。从源点出发流向每部电影,流量就是需要拍摄的天数,从每部电影到拍摄周内拍摄的那些天,流量是1,因为每天只能拍一部,同理这些天都连接到汇点,流量也是1,然后看源点能否满流就可以了。
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];//一共多少个点跑bfs,dis数组和q数组就开多大。
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为源点到这个点的路径上的最小边权 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);//让每一天的拆点连起来,并且把后置点和ed连起来 //其实上一步是不需要拆点的,因为每个点和终点只有一个连边,所以不需要控制这个点只能被选择一次。 但是拆不拆点都可以过。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]);//我要给这个电影提供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 ;
}