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. Linux系统从零到高手的进阶心得

    初次了解到Linux系统还是在我初中的时候,那时候正是在一个中二年龄,喜欢看小说,对于小说中出现的明显的非现实场景感到十分钦佩.羡慕,并常常幻想自己也有小说主人公那样的本领.那正是在这样一个充满幻想的 ...

  2. Linux防火墙iptables的策略

    iptables策略 iptables -L #查看现有防火墙所有策略 iptables -F #清除现有防火墙策略 只允许特定流量通过,禁用其他流量 1.允许SSH流量(重要) iptables - ...

  3. NIO完成网络通信(一)

    NIO:即非阻塞式IO 视频教程:  https://chuanke.baidu.com/v1982732-211322-1316084.html 使用步骤: 1.创建 ServerSocketCha ...

  4. Java反射(一眼就看会)

    参考:(1)http://blog.csdn.net/liujiahan629629/article/details/18013523(2)https://www.zhihu.com/question ...

  5. Valgrind,内存调试工具

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具 官网:http://valgrind.org/ 用户开发手册地址:http://valgrind.org/docs/manu ...

  6. css文件 如何使背景图片大小适应div的大小

    对背景图片设置属性:background-size:cover;可以实现背景图片适应div的大小. background-size有3个属性: auto:当使用该属性的时候,背景图片将保持100% 的 ...

  7. SQL-29 使用join查询方式找出没有分类的电影id以及名称

    题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...

  8. android 获取Asset中Properties文件配置的键值对

    1 获取 AssetManager AssetManager assetManager = context.getApplicationContext().getAssets(); 2 获取流 Str ...

  9. Java语法基础学习DayTwo

    一.数据类型补充问题 数据类型的自动转换等级: byte,short,char -- int -- long -- float -- double long是8个字节,float是4个字节,为什么是这 ...

  10. 1--Selenium环境准备--Eclipse 添加Testng插件

    Eclipse安装TestNG TestNG官网地址:http://testng.org/ 1.离线安装TestNG插件: 受网络等因素影响,在线安装方式速度比较慢,可以通过如下方式离线安装TestN ...