码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1698 Alice's Chance(网络流)

时间:2014-07-29 21:53:53      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   strong   io   for   

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 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

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
思路:1、新建一个超级源点,该点到每个电影(把电影也当做一个点)的流量为所需的天数;

2、每一天都当做一个点,然后如果这一天能演某个电影,则把该点和这个电影连一条边,边权为1;同时,把这一点和汇点连一条边,边权也为1.

3、求超级源点到汇点的最大流,若最大流等于总天数则输出Yes!!


#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 505
const int inf=10000000;
int g[N][N];
int pre[N],mark[N];
int min(int a,int b)
{
	return a<b?a:b;
}
int ek(int n)
{
    int i,u,ans=0;
    while(1)
    {
        queue<int>q;
        q.push(0);
        memset(mark,0,sizeof(mark));
        memset(pre,-1,sizeof(pre));
        mark[0]=1;
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            for(i=0;i<=n;i++)
            {
                if(!mark[i]&&g[u][i])
                {
                    mark[i]=1;
                    pre[i]=u;
                    q.push(i);
                }
            }
        }
        if(pre[n]==-1)
            break;
        int d=inf;
        for(i=n;i!=0;i=pre[i])
        {
            d=min(d,g[pre[i]][i]);
        }
        for(i=n;i!=0;i=pre[i])
        {
            g[pre[i]][i]-=d;
            g[i][pre[i]]+=d;
        }
        ans+=d;
    }
    //printf("%d\n",ans);
    return ans;
}
int main()
{
    int i,j,k,n,t,u,sum,T;
    int a[22][8],d[22],w[22];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(g,0,sizeof(g));
        sum=t=0;           //汇点
        for(i=1;i<=n;i++)
        {
            for(j=0;j<7;j++)
            {
                scanf("%d",&a[i][j]);
            }
            scanf("%d%d",&d[i],&w[i]);
            if(w[i]>t)
                t=w[i];
        }
        t=t*7+n+1;          //汇点的下标
        for(i=1;i<=n;i++)
        {
            g[0][i]=d[i];     //超级源点到一般源点(电影所需天数)的边权
            for(k=0;k<w[i];k++)
			{
				for(j=0;j<7;j++)
				{
					if(a[i][j])
					{
					    u=k*7+j+n+1;
						g[i][u]=g[u][t]=1;
					}
				}
			}
            sum+=d[i];
        }
        if(sum==ek(t))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


poj 1698 Alice's Chance(网络流),布布扣,bubuko.com

poj 1698 Alice's Chance(网络流)

标签:des   style   http   color   os   strong   io   for   

原文地址:http://blog.csdn.net/u011721440/article/details/38277689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!