您的位置:首页 > 其它

poj1698二分图多重匹配

2016-04-30 16:56 302 查看
Alice's Chance

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6728 Accepted: 2751
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


Source

POJ Monthly--2004.07.18

题意:有M部电影要拍,规定每一部电影只能在每周的某几天拍,给定每一部电影的总拍摄日期以及截至周数,问能否拍完所有的电影,(每天只能拍一部电影)

x集是日期,y集是电影,将日期分配给有关联的电影;

最后判断是否能拍完只需看一下每一部电影是否被分配了足够的日期

将电影作为Y方点,每一天作为X方点(最多50周,每周7天,所以共设350个X方点),若第i个电影可以在第j天拍就连边(i, j)。最后判断匹配数是否等于所有电影要拍的天数和即可

代码有详细解释:

///多重匹配
/**
二分图多重匹配x集合是单流量
y集合可以多重匹配;
*/
#include<iostream>
#include<algorithm>
#include<string>
#include <string.h>
#include <stdio.h>
#include<vector>
using namespace std;
const int M = 20+10;
const int N = 350+10;
int link[M]
,vlink[M],cap[M];
bool vis[M];
vector<int> g
;
void init()
{
for(int i=0;i<N;i++)
g[i].clear();
}
int path(int s)
{
for(int it=0;it<g[s].size();it++)
{
int v=g[s][it];
if(!vis[v])
{
vis[v]=true;
if(vlink[v]<cap[v])///cap[v],表示y集合v点最多可以连几条边,
///y集合每个点固定流量时,只需要该这个点就行
{
link[v][vlink[v]++]=s;
return 1;
}
for(int j=0;j<vlink[v];j++)
{
if(path(link[v][j]))
{
link[v][j]=s;
return 1;
}
}
}
}
return 0;
}
bool max_match(int n,int m)
{
int ans=0;
memset(vlink,0,sizeof(vlink));
for(int i=0;i<n;i++)///x集合,每个点只能用一次
{
if(g[i].size()==0)///x集合不连续,并不是n天每天都有电影可以拍摄
continue;
memset(vis,false,sizeof(vis));
if(path(i))///i点可以匹配出去
ans++;
}
///ans值表示最大的匹配数
int num=0;
for(int i=0;i<m;i++)
{
///if(vlink[i]!=cap[i]) vlink 表示多重集合流量!!!这样也是可以的
num+=cap[i];
///return false;
}
///return true ;
return ans==num;
}
int main()
{
int T;
int n,m,a[10],w,maxw;
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
maxw=0;
init();
for(int i=0;i<m;i++)
{
for(int j=0;j<7;j++)
scanf("%d",&a[j]);
scanf("%d %d",&cap[i],&w);
maxw=max(maxw,w);
for(int j=0;j<7;j++)
{
if(a[j])
{
for(int k=0;k<w;k++)
g[k*7+j].push_back(i);
}
}
}
n=maxw*7;
if(max_match(n,m))
puts("Yes");
else puts("No");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: