poj 1698 Alice's Chance(网络流)

Alice‘s Chance

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(网络流)

时间: 2024-12-28 09:52:29

poj 1698 Alice's Chance(网络流)的相关文章

POJ 1698 Alice&#39;s Chance 网络流(水

题目链接:点击打开链接 题目大意:   有个人想拍n部电影,每部电影限定每周哪几天可以拍 并且必须在第ki周之前把这部电影拍完,问能否拍完n部电影 解题思路:  把每部电影当作一个顶点,源点指向这些顶点,容量为该电影需要拍多少天 然后把每一天都当作顶点,某个工作可以在这天完成就连容量为1大边 每天的顶点指向汇点,容量也为1 最后求出最大流,满流则说明可以完成这些工作 啦啦啦 #include <cstdio> #include <cstring> #include <algo

POJ 1698 Alice&#39;s Chance(网络流之最大流)

题目地址:POJ 1698 水题..将每部电影与它可以演的那一天连边就行了.建二分图.用二分最大匹配也完全可以做. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queu

POJ 1698 Alice&#39;s Chance(网络流+构图)

题目链接:http://poj.org/problem?id=1698 题目: 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. Unfortuna

poj 1698 Alice&#39;s Chance 拆点最大流

将星期拆点,符合条件的连边,最后统计汇点流量是否满就行了,注意结点编号. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #include<s

POJ 1698 Alice&#39;s Chance(最大流+拆点)

POJ 1698 Alice's Chance 题目链接 题意:拍n部电影,每部电影要在前w星期完成,并且一周只有一些天是可以拍的,每部电影有个需要的总时间,问是否能拍完电影 思路:源点向每部电影连边,容量为d,然后每部电影对应能拍的那天连边,由于每天容量限制是1,所以进行拆点,然后连向汇点即可 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> usi

poj 1698 Alice&#39;s Chance SAP 最大流

[题意]:Alice有n部电影要拍,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影 [建图]:源点与每部电影连边,容量为天数,每部电影与可以拍该电影的那些天数连边,容量为1,再所有的天数与汇点连边容量为1. 要注意天数和汇点连边的时候不要重复了,我这里用的数组不会出现这种情况. 1 #include<iostream> 2 #include<vector> 3 #include<cstring&g

poj 1698 Alice&#39;s Chance 二分图多重匹配

题意: 一个演员要拍n部电影,每部电影只能在一周的特定几天拍(如周2,周4,周5),第i部电影要拍di天,必须要在wi周拍完,问演员是否可以完成任务. 分析: 建二分图,转化为二分图的多重匹配. 代码: //poj 1698 //sep9 #include <iostream> using namespace std; const int maxX=64*7; const int maxY=64; int g[maxX][maxY],match[maxY][maxX]; int vis[max

POJ 1698 Alice&amp;#39;s Chance(最大流+拆点)

POJ 1698 Alice's Chance 题目链接 题意:拍n部电影.每部电影要在前w星期完毕,而且一周仅仅有一些天是能够拍的,每部电影有个须要的总时间,问能否拍完电影 思路:源点向每部电影连边,容量为d,然后每部电影相应能拍的那天连边,因为每天容量限制是1.所以进行拆点,然后连向汇点就可以 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> us

[poj1698]Alice&#39;s Chance[网络流]

[转]原文:http://blog.csdn.net/wangjian8006/article/details/7926040 题目大意:爱丽丝要拍电影,有n部电影,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影第一行代表有多少组数据对于每组数据第一行代表有n部电影接下来2到n+1行,每行代表一个电影,每行9个数,前面7个数,1代表拍,0代表不拍,第8个数代表要拍几天,第9个数代表有几个礼拜时间拍 解题思路:这题可以