HDU3605:Escape(状态压缩+最大流)
Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 13271 Accepted Submission(s): 3351
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605
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
题意:
给出n个人,m个星球,每个人都有自己能去的星球,每个星球都有一定的容量,问是否所有人都能到这m个星球上面去。
题解:
注意这里人数有1e5,而m不超过10。如果我们直接建边跑最大流,边数可能为1e6,这样肯定会超时的。
我们注意到m很小,那么会想到对状态进行压缩(如果之前接触过这类题)。
虽然人数很多,但是最多有2^10种状态,所以我们可以将1e5个点压缩为1024个点,这样再来建图就能在时间、空间上有较大的优化。
压缩后的每个点表示对这m个星球意愿的状态,1代表能去,0代表不能。
细节见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#define INF 99999999
using namespace std;
typedef long long ll;
const int N = , M = ;
int n,m,cnt,tot,t;
int peo[N],head[N],d[N]; struct Edge{
int v,next,c;
}e[M];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].c=c;e[tot].next=head[u];head[u]=tot++;
e[tot].v=u;e[tot].c=;e[tot].next=head[v];head[v]=tot++;
}
int bfs(){
memset(d,,sizeof(d));d[]=;
queue <int > q;q.push();
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> && !d[v]){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[t]!=;
}
int dfs(int s,int a){
if(s==t || a==) return a;
int flow=,f;
for(int i=head[s];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[s]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f>){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[s]=-;
return flow;
}
int Dinic(){
int flow=;
while(bfs()) flow+=dfs(,INF);
return flow;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
cnt=tot=;memset(head,-,sizeof(head));
memset(peo,,sizeof(peo));
for(int i=;i<=n;i++){
int x = ;
for(int j=,op;j<=m;j++){
x<<=;
scanf("%d",&op);
x+=op;
}
if(!peo[x]) cnt++;
peo[x]++;
}
t=cnt+m+;
for(int i=;i<=m;i++){
int c;
scanf("%d",&c);
adde(cnt+i,t,c);
}
int p=;
for(int i=;i<(<<m);i++){
if(!peo[i]) continue ;
p++;
adde(,p,peo[i]);
for(int j=;j<m;j++)
if(i&(<<j)) adde(p,m-j+cnt,peo[i]);
}
int ans = Dinic();
if(ans==n) puts("YES");
else puts("NO");
}
return ;
}
HDU3605:Escape(状态压缩+最大流)的更多相关文章
- Escape(状态压缩+最大流,好题)
Escape http://acm.hdu.edu.cn/showproblem.php?pid=3605 Time Limit: 4000/2000 MS (Java/Others) Memo ...
- HDU3605(KB11-M 状态压缩+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- HDU 3605 Escape(状态压缩+最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意: 有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所 ...
- Codeforces 1383F - Special Edges(状态压缩+最大流)
Codeforces 题目传送门 & 洛谷题目传送门 首先暴力显然是不行的,如果你暴力最大流过了我请你吃糖 注意到本题的 \(k\) 很小,考虑以此为突破口解题.根据最大流等于最小割定理,点 ...
- hdu3605(最大流+状态压缩)
传送门:Escape 题意:给出每个人适合住的星球信息和该星球能住多少人 ,第一行给出n m 代表有 n 个人 m 个星球,然后接下来n行每行m个数字 1代表适合第 i 个星球 0 代表不适合第 i ...
- HDU 3605:Escape(最大流+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- HDU 3681 Prison Break(BFS+二分+状态压缩DP)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
随机推荐
- python函数的返回值
返回值:return1.没有返回值 #不写return #只写return:结束一个函数 #return None2.有一个返回值 #可以返回任何数据类型 #只要返回就可 ...
- 3.从print到I/O
为何对双引号念念不忘? >>> print("hello, world!") hello, world! 平x而论,既然在意双引号的去掉,为何不在意括号的去掉 ...
- FIFO队列(First In First Out)和优先队列
queue<类型名> q; q.size() - 返回队列中元素个数 q.empty() - 若队列为空,返回true ,否则返回false q.pop() - 删除队首元素,但不返回其值 ...
- Git使用之二:下载远程代码到本地指定文件夹
一.前期工作: 1.准备好本地的文件夹 2.如果后期需要继续以该文件夹进行同步的,则需要配置该文件夹,方法请参考之前的 Git使用之一:创建仓储和提交文件 二.用clone(克隆方式下载) 在本地下 ...
- CentOS 7 systemd添加自定义系统服务
systemd: CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,即:/usr/lib/systemd/syste ...
- Win10启动不了的问题处理记录
前几天电脑突然出现蓝屏的情况,而且使用Win10自带的修复功能根本没有卵用,修复不了,很郁闷,死活进不了系统了,说什么“INACCESSABE BOOT DEVICE”,好像是引导设备不可用. 到网上 ...
- 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)
基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...
- vue整合mui
步骤1:下载https://github.com/dcloudio/mui 步骤2:将下载来的包中的dist文件夹 放到vue项目的assets中 步骤3:修改webpack配置 找到build下 ...
- jsp 中获取自定义变量
首先确定El表达式的查找范围: 依次从Page.Request.Session.Application 中的 attribute属性中查找. <% String basePath = reque ...
- jmeter如何连接数据库
大家都知道jmeter是java编写的,java/jmeter如果想连接数据库就要通过java database connector(JDBC)去连接,首先需先下载一个驱动 (mysql-connec ...