hdu 3605(二分图多重匹配)

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8747    Accepted Submission(s): 2026

Problem Description
2012
If this is the end of the world how to do? I do not know how. But now
scientists have found that some stars, who can live, but some people do
not fit to live some of the planet. Now scientists want your help, is to
determine what all of people can live in these planets.
 
Input
More
set of test data, the beginning of each data is n (1 <= n <=
100000), m (1 <= m <= 10) n indicate there n people on the earth, m
representatives m planet, planet and people labels are from 0. Here are
n lines, each line represents a suitable living conditions of people,
each row has m digits, the ith digits is 1, said that a person is fit to
live in the ith-planet, or is 0 for this person is not suitable for
living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
 
Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
 
Sample Input
1 1
1
1

2 2
1 0
1 0
1 1

 
Sample Output
YES
NO
 
Source
 
题意:有 n 个人,m颗行星, 每个人可以适应其中的某一些星球,每个星球有一个容量上限,问着n个人能否全部安排到这m个行星?
题解:n 个人和行星是一对多的关系,所以我们对每个人和其移居行星建边,然后进行二分图多重匹配即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
const int M = ;
int graph[N][M];
int cap[M];
int n,m;
int linker[M][N],link[N];
bool vis[M];
bool dfs(int u){
for(int v=;v<=m;v++){
if(!vis[v]&&graph[u][v]){
vis[v] = true;
if(link[v]<cap[v]){
linker[v][link[v]++] = u;
return true;
}
for(int i=;i<link[v];i++){
if(dfs(linker[v][i])){
linker[v][i] = u;
return true;
}
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
int x;
memset(graph,,sizeof(graph));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&x);
if(x) graph[i][j] = ;
}
}
for(int i=;i<=m;i++) scanf("%d",&cap[i]);
memset(link,,sizeof(link));
bool flag = true;
for(int i=;i<=n;i++){
memset(vis,false,sizeof(vis));
if(!dfs(i)){ ///第i个人不行就直接结束
flag = false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
上一篇:1734: [Usaco2005 feb]Aggressive cows 愤怒的牛


下一篇:HDU 1669 二分图多重匹配+二分