您的位置:首页 > 其它

POJ1698 Alice's Chance 最大流Dinic

2017-01-20 01:24 232 查看
Alice's Chance
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 7179     Accepted: 2934

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,

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
POJ Monthly--2004.07.18


源点S连接每一天d 容量为1

如果某天di能拍电影Fj di到Fj连一条边 容量为1

每部电影Fi到汇点T连一条边 容量为该电影需要拍的天数

跑一遍最大流 判断总流量是否和需要拍电影的总天数相同即可

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std
156a4
;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 50*7+50;
const int M = N*20*2;

int level
;//标号 level[i]:s到i的最短距离

struct Edge{
int to,c,next;
}edge[M];
int head
;

inline void add_edge(int k,int u,int v,int c){
edge[k].to = v;
edge[k].c = c;
edge[k].next = head[u];
head[u] = k;
}

bool bfs(int s,int t,int n){//标号 计算level
deque<int>que;
fill(level,level+n+1,-1);
que.push_back(s);
level[s] = 0;
while(!que.empty()){
int u = que.front();
if(u == t){
return true;
}
que.pop_front();
for(int i = head[u];i!=-1;i = edge[i].next){
if(edge[i].c > 0 && level[edge[i].to] == -1){
level[edge[i].to] = level[u] + 1;
que.push_back(edge[i].to);
}
}
}
return false;
}

int dfs(int u,int t,int maxf){//u:所在的点 t:汇点 maxf:能流到u的流量
if(u == t){
return maxf;
}
int sum = 0;
for(int i = head[u];i!=-1;i = edge[i].next){
Edge&e = edge[i];
if(e.c>0 && level[e.to]>level[u]){
int f = dfs(e.to,t,min(maxf - sum,e.c));
sum += f;
edge[i].c -= f;
edge[i^1].c += f;
if(sum == maxf){//流量用完了
break;
}
}
}
level[u]=-1;
return sum;
}

int dinic(int s,int t,int n){//s:源点 t:汇点 n:点数
int ans = 0;
while(bfs(s,t,n)){
ans += dfs(s,t,2*INF);
}
return ans;
}

int F[20][9];
int s,t;

inline int day_hash(int i,int j){
return 7*i+j+1;
}

inline int film_hash(int i,int weeks){
return 7*weeks+i+1;
}

bool slove(int n){//返回期望的流量(总拍电影天数)
int weeks=0,daysum=0;
for(int i=0;i<n;++i){
daysum+=F[i][7];
weeks=max(weeks,F[i][8]);
}
fill(head,head+7*weeks+n+2+1,-1);//7天*weeks n个电影 s t
s=7*weeks+n+1,t=s+1;
int tmp=0;
for(int i=1;i<=7*weeks;++i){//s到每一天
add_edge(tmp++,s,i,1);
add_edge(tmp++,i,s,0);
}
for(int i=0;i<n;++i){//每天到每个电影
for(int j=0;j<7;++j){
if(F[i][j]==1){
for(int k=0;k<F[i][8];++k){
add_edge(tmp++,day_hash(k,j),film_hash(i,weeks),1);
add_edge(tmp++,film_hash(i,weeks),day_hash(k,j),0);
}
}
}
}
for(int i=0;i<n;++i){//每个电影到t
add_edge(tmp++,film_hash(i,weeks),t,F[i][7]);
add_edge(tmp++,t,film_hash(i,weeks),0);
}

int ans=dinic(s,t,7*weeks+n+2);
/*   cout<<ans<<' '<<daysum<<endl;*/
return ans==daysum;
}

int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;++i){
for(int j=0;j<9;++j){
scanf("%d",&F[i][j]);
}
}
puts(slove(n)?"Yes":"No");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ 图论 最大流 Dinic