您的位置:首页 > 其它

POJ 1698 Alice's Chance(最大流)

2016-02-08 00:18 369 查看
题意:爱丽丝要拍电影,有n部电影,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影?
思路:直接说建图吧。0为源点,1-350为每一天,因为最多50周,源点和每一天连一条容量为1的边,350+n+1为汇点,每一部电影和汇点连一条容量为拍摄这部电影的时间的边,如果某天可以拍某部电影,就连一条容量为1的边,最后跑一遍最大流看看是否等于需要的时间即可。

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 550
#define INF 1<<29
#define LL long long
int cas=1,T;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n,m;
struct Dinic
{
//	int n,m;
int s,t;
vector<Edge>edges;        //边数的两倍
vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn];           //BFS使用
int d[maxn];              //从起点到i的距离
int cur[maxn];            //当前弧下标
void init()
{
for (int i=0;i<=500;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));        //反向弧
int mm=edges.size();
G[from].push_back(mm-2);
G[to].push_back(mm-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=0;
vis[s]=1;
while (!q.empty())
{
int x = q.front();q.pop();
for (int i = 0;i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to]=1;
d[e.to] = d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}

int DFS(int x,int a)
{
if (x==t || a==0)
return a;
int flow = 0,f;
for(int &i=cur[x];i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if (a==0)
break;
}
}
return flow;
}

int Maxflow(int s,int t)
{
this->s=s;
this->t=t;
int flow = 0;
while (BFS())
{
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}dc;
int week[maxn];
int day[30][10];    //第i部电影在第几天可以拍
int need[maxn];

int main()
{
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
//	memset(need,0,sizeof(need));
//	memset(week,0,sizeof(week));
//	memset(day,0,sizeof(day));
int sum = 0;           //总需要天数
scanf("%d",&n);
for (int i = 1;i<=n;i++)
{
for (int j = 1;j<=7;j++)
scanf("%d",&day[i][j]);
scanf("%d",&need[i]);
scanf("%d",&week[i]);
sum+=need[i];
}
dc.init();
for (int i = 1;i<=350;i++)
dc.AddEdge(0,i,1);              //0源点到每一天有一条容量为1的边
for (int i = 1;i<=n;i++)
dc.AddEdge(350+i,350+n+1,need[i]);    //每一部电影到汇点有容量为need[i]的边
for (int i = 1;i<=n;i++)
{
for (int j = 1;j<=350;j++)
{
if (day[i][(j%7)+1]==1 && (j-1)/7<week[i])
dc.AddEdge(j,i+350,1);
}
}
printf("%s\n",dc.Maxflow(0,350+n+1)==sum?"Yes":"No");
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: