HDU 3605 Escape (网络流,最大流,位运算压缩)

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

Http

HDU:https://vjudge.net/problem/HDU-3605

Source

网络流,最大流,位运算压缩

题目大意

有n个人和m个星球,每一个人可以适应若干个星球的环境,而每一个星球都有人数上限。现在求能否让所有人都分配到一个其可以适应环境的星球上去。

解决思路

首先如果不管数据范围,这道题的网络流建模还是比较好想的。从源点出发连上所有的人,容量为1,从每一个人连边到其所有能适应的星球,容量为1,再从星球连边到汇点,容量为星球能容纳的人数上限。跑最大流后看看最大流是否与人数相等,如果相等则存在可行解,否则无解。

但要注意到本题的数据范围。人数有100000,这样直接跑最大流是会出问题的。再看一看m的数据范围,诶……只有10。于是我们可以想到一定有非常多的人,他们对所有星球的适应情况是一样的。所以我们可以用位运算的方式,把每一个人对星球的适应情况压缩在一个整数里面,然后统计这个数相同的人的人数,相当于把这些人都看作一个人。然后连边的时候就不是连所有的人,而是连不同的类型,而容量就是这种类型的人的个数,其他建图不变。

另:这里使用Dinic实现最大流,可以参考这篇文章

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std; const int maxN=100101;
const int maxM=maxN*70;
const int inf=2147483647; class Edge
{
public:
int u,v,flow;
}; int n,m;
int cnt;
int tot;
int Head[maxN];
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxN];
map<int,int> Cnt; void Add_Edge(int u,int v,int flow);
bool bfs();
int dfs(int u,int flow); int main()
{
while (cin>>n>>m)
{
cnt=-1;
tot=0;
memset(Head,-1,sizeof(Head));
Cnt.clear();
for (int i=1;i<=n;i++)
{
int ret=0;
for (int j=1;j<=m;j++)
{
int is;
scanf("%d",&is);
ret=ret+(is<<j);//位运算统计
}
Cnt[ret]++;//Cnt就是统计某一种类型的人有多少个
}
map<int,int>::iterator loop;
for (loop=Cnt.begin();loop!=Cnt.end();loop++)//遍历所有的类型
{
tot++;
for (int i=1;i<=m;i++)//连人与星球
if ((loop->first)&(1<<i))//若这类人都能适应第j个星球,则连边,注意容量为这类人的个数
Add_Edge(tot,Cnt.size()+i,loop->second);
Add_Edge(0,tot,loop->second);//连源点与这类人
}
for (int i=1;i<=m;i++)
{
int flow;
scanf("%d",&flow);//读入每一个星球的人数上限,并连汇点
Add_Edge(tot+i,tot+m+1,flow);
}
int Ans=0;//求解最大流
while (bfs())
{
for (int i=0;i<=tot+m+1;i++)
cur[i]=Head[i];
while (int di=dfs(0,inf))
Ans+=di;
}
if (Ans==n)//若满流,则有解,否则无解
printf("YES\n");
else
printf("NO\n");
}
return 0;
} void Add_Edge(int u,int v,int flow)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow; cnt++;
Next[cnt]=Head[v];
Head[v]=cnt;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
} bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
Q[1]=0;
depth[0]=1;
do
{
t++;
int u=Q[t];
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (h!=t);
if (depth[tot+m+1]==-1)
return 0;
return 1;
} int dfs(int u,int flow)
{
if (u==tot+m+1)
return flow;
for (int &i=cur[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}

最新文章

  1. BZOJ3631: [JLOI2014]松鼠的新家
  2. JavaScript基础整理(1)
  3. 全自动编译FFmpeg(含x264,fdk aac,libmp3lame,libvpx等第3方库)
  4. javascript 拷贝文本
  5. HTML xmlns
  6. [cocos2dx]怎样将Android手机游戏移植到电视?
  7. HDU 4462 Scaring the Birds (暴力求解,二进制法)
  8. 在PHP中PDO解决中文乱码问题
  9. MySql连接问题
  10. 201521123049 《JAVA程序设计》 第13周学习总结
  11. Android : Camera2/HAL3 框架分析
  12. JAVA实训第二次作业
  13. BZOJ2946 [Poi2000]公共串(后缀自动机)
  14. golang sort包使用
  15. tomcat cluster session同步时保存map数据遇到的问题
  16. img-html-2
  17. 网络编程 生产者消费者模型 GiL
  18. js判断元素是否是disable状态
  19. 【转】WPF自定义控件与样式(2)-自定义按钮FButton
  20. 增加定时检测linux占用内存,及时清理功能

热门文章

  1. Linux-C-Program:makefile
  2. easyui datagrid remoteSort的实现 Controllers编写动态的Lambda表达式 IQueryable OrderBy扩展
  3. bootstrap是什么
  4. v-for v-if || v-else
  5. 矩形A + B HDU2524
  6. 安装Visual Studio 2013以及简单使用
  7. 嵌入式linux教程
  8. Leetcode——171.宝石与石头
  9. redis的优缺点
  10. Expanded encryption and decryption signature algorithm SM2 &amp; SM3