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.

题意:有N个人,每个人能适应若干个星球(共10个星球),每个星球能够容纳若干人,问是否能让所有人都有星球住。

其实应该是一个二分图多重匹配的题目。

不过一开始当成最大流的题目了,做的时候TLE了,然后将对10个星球的适应性的情况用状压变成了2^10种情况,即2^10种人,并且加上读入优化在vj上也卡过去了,不过在杭电上始终还是TLE了。

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; const int maxn=;
const int maxm=;
int match[maxn][maxm],g[maxn][maxm],cnt[maxm],lim[maxn],n,m;
bool vis[maxm]; bool dfs(int s){
for(int i=;i<m;++i){
if(g[s][i]==||vis[i]==)continue;
vis[i]=;
if(cnt[i]<lim[i]){
match[i][cnt[i]++]=s;
return ;
}
else{
for(int j=;j<lim[i];++j){
if(dfs(match[i][j])==){
match[i][j]=s;
return ;
}
}
}
}
return ;
}
bool hungary(int n){
for(int i=;i<n;++i){
memset(vis,,sizeof(vis));
if(dfs(i)==)return ;
}
return ;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(cnt,,sizeof(cnt));
for(int i=;i<n;++i)
for(int j=;j<m;++j)scanf("%d",&g[i][j]);
for(int i=;i<m;++i)scanf("%d",&lim[i]);
if(hungary(n))printf("YES\n");
else printf("NO\n");
}
return ;
}

多重匹配

 #pragma comment(linker,"/STACK:16777216")
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxm=;
const int INF=0x3f3f3f3f; struct edge{
int from,to,c,f;
edge(int a,int b,int m,int n):from(a),to(b),c(m),f(n){}
}; struct dinic{
int s,t,m;
vector<edge>e;
vector<int>g[maxm];
bool vis[maxm];
int cur[maxm],d[maxm]; void init(int n){
for(int i=;i<=n;i++)g[i].clear();
e.clear();
} void add(int a,int b,int c){
e.push_back(edge(a,b,c,));
e.push_back(edge(b,a,,));
m=e.size();
g[a].push_back(m-);
g[b].push_back(m-);
} bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=;
d[s]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++){
edge tmp=e[g[u][i]];
if(!vis[tmp.to]&&tmp.c>tmp.f){
d[tmp.to]=d[u]+;
vis[tmp.to]=;
q.push(tmp.to);
}
}
}
return vis[t];
} int dfs(int x,int a){
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<g[x].size();i++){
edge& tmp=e[g[x][i]];
if(d[tmp.to]==d[x]+&&(f=dfs(tmp.to,min(a,tmp.c-tmp.f)))>){
tmp.f+=f;
e[g[x][i]^].f-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
if(!flow)d[x]=-;
return flow;
} int mf(int s,int t){
this->s=s;
this->t=t;
int flow=;
while(bfs()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}; int num[]; int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
memset(num,,sizeof(num));
dinic d;
d.init(m+(<<m)+);
int i,j;
for(i=;i<=n;i++){
int tmp=;
for(j=;j<=m;j++){
int a=;
char c=getchar();
while(c>''||c<'')c=getchar();
a=c-'';
tmp=tmp*+a;
}
num[tmp]++;
}
for(i=;i<=(<<m)-;i++){
if(num[i]){
d.add(,i,num[i]);
int tmp=i;
for(j=m;j>=;j--){
if(tmp&){
d.add(i,(<<m)+j,INF);
}
tmp>>=;
}
}
}
for(j=;j<=m;j++){
int a=;
char c=getchar();
while(c>''||c<'')c=getchar();
while(c>=''&&c<=''){
a=a*+c-'';
c=getchar();
}
d.add((<<m)+j,m+(<<m)+,a);
}
if(n==d.mf(,m+(<<m)+))printf("YES\n");
else printf("NO\n");
}
return ;
}

最大流

hdu3605 Escape 二分图多重匹配/最大流的更多相关文章

  1. HDU3605 Escape —— 二分图多重匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  2. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  3. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  4. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  5. HDU - 3605 Escape (缩点+最大流/二分图多重匹配)

    题意:有N(1<=N<=1e5)个人要移民到M(1<=M<=10)个星球上,每个人有自己想去的星球,每个星球有最大承载人数.问这N个人能否移民成功. 分析:可以用最大流的思路求 ...

  6. 【网络流24题】No.7 试题库问题 (最大流,二分图多重匹配)

    [题意] 假设一个试题库中有 n 道试题. 每道试题都标明了所属类别. 同一道题可能有多个类别属性.现要从题库中抽取 m 道题组成试卷.并要求试卷包含指定类型的试题. 试设计一个满足要求的组卷算法. ...

  7. 网络流24题 第五题 - PowerOJ1740 CodeVS1905 圆桌问题 二分图多重匹配 网络最大流

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - PowerOJ1740 - 有SPJ - 推荐 题目传送门 - CodeVS1905 - 无SPJ - 0% ...

  8. HDU 3605 Escape(二分图多重匹配问题)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

随机推荐

  1. 尚学堂java答案解析 第一章

    本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改 一.选择题: 1.C 解析:java为了安全,中并没有引入C语言的指针概念. 2.AD 解析:B:Java先通过ja ...

  2. sys 模块

    import sys #环境变量 print(sys.path) #查看已经加载的模块 print(sys.modules) #获取终端调用时的参数 print(sys.argv) #获取解释器的版本 ...

  3. bzoj2440

    题解: 莫比乌斯反演 ans=sigma(x/(i*i)*miu[i]) 代码: #include<bits/stdc++.h> using namespace std; ; int T, ...

  4. dgango中admin下添加搜索功能

    admin下添加搜索功能: 在表单中加入search_fields = ['ip','hostname']   可模糊匹配 当有人在管理搜索框中进行搜索时,Django将搜索查询分解成单词,并返回包含 ...

  5. 使用laravel搭建CURD后台页面

    配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...

  6. 类型重命名 typedef

    所谓数据重命名就是给数据类型起一个新的名字,比如int 这个数据类型,可以给他起一个新的名字叫 my int.他俩的用法.特点.属性等是一模一样,仅仅名字不同而已. 作用:1,增加代码的可读性.2,让 ...

  7. [leetcode121]股票买卖 Best Time to Buy and Sell Kadane算法

    [题目] Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  8. Linux U盘安装

    Ubuntu 15 U盘安装: 用UltraISO把iso文件写入到U盘中,选择hdd+模式. u盘启动后提示not a com32r image,先按tab键,然后输入live进入试用模式,然后再点 ...

  9. aspnet core 2.0 发布之后没有 views文件夹

    在项目文件里面 增加这个节点: MvcRazorCompileOnPublish 设置为false 是会发布views <PropertyGroup> <PackageTargetF ...

  10. python笔记6-while、for循环

    1.while--while循环之前,先判断一次,如果满足条件的话,再循环 #while循环 count=(input("请输入循环次数"))#计数器 while count< ...