poj 1698(最大流)

来源:互联网 发布:苹果手机修改游戏数据 编辑:程序博客网 时间:2024/06/05 04:58
Alice's Chance
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2493 Accepted: 1098

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

220 1 0 1 0 1 0 9 30 1 1 1 0 0 0 6 420 1 0 1 0 1 0 9 40 1 1 1 0 0 0 6 2

Sample Output

YesNo

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

Source

POJ Monthly--2004.07.18
题目:http://poj.org/problem?id=1698
分析:这题直接把每天看成一个点,每部电影也看成一个点,然后源点与电影连接,容量为要求天数,电影与能工作的那些天连容量为1的边,每天再连一条容量为1 的边到汇,最大流为所有要求天数之和就是Yes
#include<cstdio>using namespace std;const int mm=22222;const int mn=555;const int oo=1000000000;int node,src,dest,edge;int ver[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn],d[mn],w[mn];bool p[mn][7];inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1;    edge=0;}inline void addedge(int u,int v,int c){    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=next[i])            if(flow[i]&&dis[v=ver[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=next[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0;i<node;++i)work[i]=head[i];        while(delta=Dinic_dfs(src,oo))ret+=delta;    }    return ret;}int main(){    int i,j,k,mw,sum,n,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=1,mw=sum=0;i<=n;++i)        {            for(j=0;j<7;++j)scanf("%d",&k),p[i][j]=k;            scanf("%d%d",&d[i],&w[i]);            sum+=d[i];            w[i]*=7;            if(w[i]>mw)mw=w[i];        }        prepare(n+mw+2,0,n+mw+1);        for(i=1;i<=n;++i)        {            addedge(src,i,d[i]);            for(j=0;j<7;++j)                if(p[i][j])                {                    k=j+1;                    while(k<=w[i])addedge(i,n+k,1),k+=7;                }        }        for(i=1;i<=mw;++i)addedge(i+n,dest,1);        if(Dinic_flow()==sum)printf("Yes\n");        else printf("No\n");    }    return 0;}


代码: