原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605

Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7145    Accepted Submission(s): 1553

Problem 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
 
Source
 

题意

地球人要移民,告诉你每个人适合居住的星球是哪些,每个星球有个容量,问你是否能安排所有人移民。

题解

这是道非常非常日狗的题。。。。。。。由于星球很少,人很多,所以很容易想到将人的居住情况状压,然后再建图。这样还是要T的,这是为什么呢?因为脸黑。。。在尝试了各种输入挂之后,终于998ms过了,老天有眼。

代码

#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#define MAX_S (1<<10)+10
#define MAX_V 1222
#define MAX_N MAX_V
#define INF 2500005
using namespace std; struct edge {
int to, cap, rev;
bool isRev; edge(int t, int c, int r, bool i)
: to(t), cap(c), rev(r), isRev(i) { } edge() { }
}; template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return ; //EOF
while(c!=' -' &&(c<'' ||c>'' )) c=getchar();
sgn=(c==' -' )?-:;
ret=(c==' -' )?:(c-'' );
while(c=getchar(),c>='' &&c<='' ) ret=ret*+(c-'' );
ret*=sgn;
return ;
} vector<edge> G[MAX_N];
int level[MAX_V];
int iter[MAX_V]; void init(int totNode) {
for (int i = ; i <= totNode; i++)
G[i].clear();
memset(level, , sizeof(level));
memset(iter, , sizeof(iter));
} void add_edge(int from,int to,int cap) {
G[from].push_back(edge (to, cap, G[to].size(),));
G[to].push_back(edge (from, , G[from].size() - ,));
} void bfs(int s) {
queue<int> que;
memset(level, -, sizeof(level));
level[s] = ;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = ; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[e.to] < ) {
level[e.to] = level[v] + ;
que.push(e.to);
}
}
}
} int dfs(int v,int t,int f) {
if (v == t)return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > ) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
} int max_flow(int s,int t) {
int flow = ;
for (; ;) {
bfs(s);
if (level[t] < )return flow;
memset(iter, , sizeof(iter));
int f;
while ((f = dfs(s, t, INF)) > ) {
flow += f;
}
}
} int n,m;
int S=;
int T=; int cnt[MAX_S]; int main() {
while (scanf("%d%d", &n, &m)!=EOF) {
init(T + );
memset(cnt, , sizeof(cnt));
for (int i = ; i < n; i++) {
int s = ;
for (int j = ; j < m; j++) {
int t;
scan_d(t);
if (t)s |= ( << j);
}
cnt[s]++;
}
for (int i = ; i < ( << m); i++) {
if (cnt[i]) {
add_edge(S, i, cnt[i]);
for (int j = ; j < m; j++)
if (( << j) & i)
add_edge(i, j + ( << m), cnt[i]);
}
}
for (int i = ; i < m; i++) {
int t;
scan_d(t);
add_edge(i + ( << m), T, t);
}
int f = max_flow(S, T);
if (f == n)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

HDU 3605 Escape 最大流+状压的更多相关文章

  1. Hdu 3605 Escape (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  2. HDU 3605 Escape 最大流

    题意: 如果这是2012年世界末日怎么办?我不知道该怎么做.但是现在科学家们已经发现,有些星球上的人可以生存,但有些人却不适合居住.现在科学家们需要你的帮助,就是确定所有人都能在这些行星上生活.输入多 ...

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

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  4. HDU 3605 Escape(状压+最大流)

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

  5. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  6. hdu 5691 Sitting in Line 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 题解: 和tsp用的状压差不多,就是固定了一些访问顺序. dp[i][j]表示前cnt个点中布 ...

  7. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  8. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  9. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. (转)webView清除缓存

    NSURLCache * cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; [cache setDiskCa ...

  2. MySQL数据库详解(二)执行SQL更新时,其底层经历了哪些操作?

    ​ 前面我们系统了解了一个查询语句的执行流程,并介绍了执行过程中涉及的处理模块.相信你还记得,一条查询语句的执行过程一般是经过连接器.分析器.优化器.执行器等功能模块,最后到达存储引擎. 那么,一条更 ...

  3. Appscan安全漏洞扫描使用(转)

    这里主要分享如何使用AppScan对一大项目的部分功能进行安全扫描. ----------------------------------------------------------------- ...

  4. 用virtualbox+模拟串口+CDT调试linux内核 TCP/IP协议栈-起步

    经常有人问一台机器如何将hello经网络发送给另一台机器,我确实是不知道,只能看代码了. 说明:本人对内核的研究学习也是刚刚起步,有很多不了解的,所以文中可能会有一些"一本正经的胡扯&quo ...

  5. Eclipse下创建Spring MVC web程序--非maven版

    首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...

  6. php 操作excel

    <?php $dir=dirname(__FILE__);//查找当前脚本所在路径 require $dir."/db.php";//引入mysql操作类文件 require ...

  7. python面向对象、模块讲解

    (1)模块的介绍: 1.什么是模块  模块是一系列功能的集合体  常见的模块形式(自定义模块.第三方模块.内置模块):   1.一个module.py文件就是一个模块,文件名是module.py,而模 ...

  8. [git 学习篇] git文件版本回退再学习

    需求;  准备把readme.txt回退到上一个版本,也就是“add distributed”的那个版本 首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3 ...

  9. [uiautomator篇][9]遇到问题

    1 (1) 修改apk的存储权限,不要创建文件会提示:文件找不到 (2) 退出应用 mDevice.executeShellCommand("am force-stop com.antutu ...

  10. 紫书第一章训练1 D -Message Decoding

    Some message encoding schemes require that an encoded message be sent in two parts. The first part, c ...