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(状态压缩+最大流)的更多相关文章

  1. Escape(状态压缩+最大流,好题)

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

  2. HDU3605(KB11-M 状态压缩+最大流)

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

  3. HDU 3605 Escape(状态压缩+最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意: 有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所 ...

  4. Codeforces 1383F - Special Edges(状态压缩+最大流)

    Codeforces 题目传送门 & 洛谷题目传送门 首先暴力显然是不行的,如果你暴力最大流过了我请你吃糖 注意到本题的 \(k\) 很小,考虑以此为突破口解题.根据最大流等于最小割定理,点 ...

  5. hdu3605(最大流+状态压缩)

    传送门:Escape 题意:给出每个人适合住的星球信息和该星球能住多少人 ,第一行给出n m 代表有 n 个人 m 个星球,然后接下来n行每行m个数字 1代表适合第 i 个星球 0 代表不适合第 i ...

  6. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  7. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  8. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  9. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

随机推荐

  1. pyc是个什么鬼?

    1.Python是一门解释型语音? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释型语音,我就这样一直相信下去,知道发现了*.pyc文件的存在.如果是解释型语音,那 ...

  2. RubyMine常用快捷键

    一级必会 Shift+F10:运行running Ctrl+Alt+R:弹出RakeCtrl+Alt+G:弹出GenerateCtrl+Alt+L:格式化代码Alt+F1:切换视图(Project, ...

  3. java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict

    在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...

  4. 创建react

    cnpm install -g create-react-app 安装项目create-recat-app myapp

  5. 添加用户-查看用户列表-禁止默认root登陆

    程序小屌丝狒狒: (Q971751392) linux添加用户 adduser feifei passwd [用户名] 设置密码 可以查看所有用户的列表 cat /etc/passwd  w 可以查看 ...

  6. Android TextView 单行文本的坑

    这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...

  7. Android AppWidget偶尔无响应原因及解决办法

    Android AppWidget偶尔会出现无响应问题,如按钮点击失效,数据不更新等等. 测试后发现,一般出现在手机用清理工具(或系统自己)清理后发生,或手机重启后发生. 目前经过测试,找到的办法是把 ...

  8. 【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}

    这条题目当时卡了我们半天,于是成功打铁--今天回来一看,mmp,贪心思想怎么这么弱智.....(怪不得场上那么多人A了 题意分析 这里是原题: Tree Time Limit: 2000/1000 M ...

  9. hdu1505City Game(动态规划)

    City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  10. Ubuntu 安装Google浏览器

    Ubuntu自带的浏览器是火狐浏览器,使用的时候多多少少有些不方便,这里安装Googel浏览器. 下载 可以到 Ubuntu chrome去下载安装包. 安装 首先到下载的根目录 cd ~/Downl ...