POJ 1698 Alice's Chance 网络流/匹配


这道题目即可以用网络流解决,也可以用匹配解决,但因为匹配要把代表一部电影的点按照天数拆成多个点,这里选用了网络流。

总的点数 20+7*50+2=372

构图:

st=0,ed=371;

1: 1-350     代表天数  与汇点相连

连弧<j,ed>弧的容量为1,因为一天只能做一部电影

2: 350-370 代表电影 与源点相连

连弧<st,350+i> 弧的容量为电影完成的总天数

3: 然后如果第i天能拍第j部电影则

连弧<i,350+j>  弧的容量为1

最后求一次最大流如果maxflow等于完成所有电影所需的总天数则Yes否则就是No。

 

 

 

Alice's Chance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3893 Accepted: 1644

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

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

#define MAXN 400
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
};

edge e[999999]; 
int que[MAXN*100];
int dis[MAXN];
int pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed;
int maxflow;
int en;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
  for (int i=pre[ed];i!=-1;i=pre[i])
	 if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
  for (int i=pre[ed];i!=-1;i=pre[i]) 
	 e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
  maxflow+=flow; 
  return p;
}  

void dfs()  
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
  for(int i=st,j;i!=-1;)  
  {  
    int flag=false;  
    for(int k=head[i];k!=-1;k=e[k].next)    
    if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
    {  
      pre[j]=i; 
      head2[i]=k;  
			i=j; 
			flag=true;  
      if(i==ed) i=update();  
      if(flag) break;  
    }  
    if (!flag) dis[i]=-1,i=pre[i];   
  }  
} 

int dinic()
{
	maxflow=0;
	while(bfs())
		dfs();
	return maxflow;
}

void solve()
{
  int tn,sum=0;
  scanf("%d",&tn);
  memset(head,-1,sizeof(head));
  en=0;
  st=0,ed=350+20+1;
  for(int i=1;i<=350;i++)
    add(i,ed,1);
  for(int j=0;j<tn;j++)
  {
    int days,weeks,p[8];
    for(int i=1;i<=7;i++)
      scanf("%d",&p[i]);
    scanf("%d%d",&days,&weeks);
    add(st,351+j,days);
    for(int i=1;i<=7;i++)
    {
      if(!p[i]) continue;
      for(int k=0;k<weeks;k++)
        add(351+j,k*7+i,1);
    }
    sum+=days;
  }
  if(sum==dinic())
    printf("Yes\n");
  else
    printf("No\n");
}
 
 
int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
    solve();
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值