原题链接: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. OpenCV中图像的读取,显示与保存

      图像的读取,显示与保存 相关函数:cv2.imread().cv2.imshow().cv2.imwrite() 1.读入图像: 用cv2.imread()函数来读取图像,cv2.imread(路 ...

  2. C#语言入门

    1.基础知识 2.数据类型 3.控制语句 4.

  3. TTL与COMS的区别

    1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是  2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...

  4. Android开发——减小APK大小

    0. 前言 APK的大小对APP的加载速度,使用内存大小和消耗功率多少有一定影响.如何减小APK的大小对于Android开发者是一个永恒的话题. 查阅了很多相关资料,并将其做了删减以及总结.本文原创, ...

  5. dubbo doc入门文档

    dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

  6. iOS关于Xcode上的Other linker flags

    Targets选项下有Other linker flags的设置,用来填写XCode的链接器参数,如:-ObjC -all_load -force_load等.还记得我们在学习C程序的时候,从C代码到 ...

  7. python - unittest - 单元测试

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_unittest.py@ide: PyCharm Communi ...

  8. javascript是脚本语言?javascript万物皆对象?

    呵呵哒!带你见识下js面对对象的魅力 是的是的,退后,朕要开始装逼了- 这是什么鸟东西?是的是的,装逼开始,2016年度最佳JS编译器,ES6标准出来后,小伙伴们对新特性摩拳擦掌,奈何浏览器支持把我们 ...

  9. PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述

    本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章 execute_ex 我们来看看执行一个简单的脚本 test.p ...

  10. Jeddict研究过程中的总结

    一.与作者交流的总结 说来也是惭愧,没有太多的经验,先给大家贴两张图,看看大家能不能发现问题: 在最开始的时候,都处于Gaurav Gupta让我给材料的过程,因为我不是缺这个就是缺那个,根本说不清楚 ...