链接:

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

题意:

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.

思路:

1e5个人,每个人都连边会T, 考虑每个人的状态,选10个星球,最多1024中选择,吧人转换程选择,压缩成1024中情况.

连到星球,在跑最大流即可.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; const int MAXN = 1e4+10;
const int INF = 1e9; struct Edge
{
int from, to, cap;
};
vector<Edge> edges;
vector<int> G[MAXN];
int Dis[MAXN], Vis[MAXN];
int Peo[MAXN];
int n, m, s, t, d; void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge{from, to, cap});
edges.push_back(Edge{to, from, 0});
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
} bool Bfs()
{
//Bfs构造分层网络
memset(Dis, -1, sizeof(Dis));
queue<int> que;
que.push(s);
Dis[s] = 0;
while (!que.empty())
{
int u = que.front();
que.pop();
// cout << u << endl;
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
que.push(e.to);
Dis[e.to] = Dis[u]+1;
}
}
}
return (Dis[t] != -1);
} int Dfs(int u, int flow)
{
//flow 表示当前流量上限
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
{
int tmp = Dfs(e.to, min(flow, e.cap)); // 递归计算顶点 v
flow -= tmp;
e.cap -= tmp;
res += tmp;
edges[G[u][i]^1].cap += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
} int MaxFlow()
{
int res = 0;
while (Bfs())
{
res += Dfs(s, INF);
}
return res;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n >> m)
{
memset(Peo, 0, sizeof(Peo));
s = 1023+m+1, t = 1023+m+2;
for (int i = 0;i <= t;i++)
G[i].clear();
edges.clear();
int w;
for (int i = 1;i <= n;i++)
{
int res = 0;
for (int j = 0;j < m;j++)
{
cin >> w;
res += (1<<j)*w;
}
// cout << res << endl;
Peo[res]++;
}
for (int i = 1;i <= m;i++)
{
cin >> w;
AddEdge(1023+i, t, w);
}
for (int i = 0;i <= 1023;i++)
{
if (Peo[i] == 0)
continue;
AddEdge(s, i, Peo[i]);
int tmp = i;
int cnt = 1;
while (tmp)
{
if (tmp&1)
{
AddEdge(i, 1023+cnt, Peo[i]);
}
tmp >>= 1;
cnt++;
}
}
int res = MaxFlow();
if (res == n)
cout << "YES" << endl;
else
cout << "NO" << endl;
} return 0;
}
/*
2 2
1 1
0 1
2 2
*/

HDU-3605-Escape(最大流, 状态压缩)的更多相关文章

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

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

  2. HDU 3605 Escape 最大流+状压

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others)    ...

  3. HDU 3605 Escape 最大流

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

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

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

  5. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

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

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

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

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

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

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

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

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

  10. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

随机推荐

  1. 【神经网络与深度学习】如何将别人训练好的model用到自己的数据上

    caffe团队用imagenet图片进行训练,迭代30多万次,训练出来一个model.这个model将图片分为1000类,应该是目前为止最好的图片分类model了. 假设我现在有一些自己的图片想进行分 ...

  2. python 迭代器(第二次总结)

    迭代器 1.先明白迭代器是什么意思 迭代:不断的取值的(器)工具 迭代器:就是一个重复的过程,每一次重复都是基于上一次的结果而来的. (单纯的重复不是迭代) 2.为什么要有迭代器 不依赖索引取值的方法 ...

  3. 2019CSP-S游记(真)

    本来是考完了的,但是由于江西省的负责人员的不小心(?),江西oier的大部分代码都被删掉了, 所以我们需要重考,想看我之前CSP的游记可以看这个点我.下面是我江西重考的游记: Day0 又集训了一个星 ...

  4. 项目中使用vue的API。 和项目的结构

    <template> <!--组件的 结构--> <div id="app"> <h3>{{ msg }}</h3> & ...

  5. [LeetCode] 矩形面积

    题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...

  6. Ubuntu下更新Pycharm时权限不够(PyCharm does not have write access to...)

    问题描述 更新Pycharm时,出现如下问题 PyCharm does not have write access to /usr/local/software/pycharm-2019.1.3. P ...

  7. visual studio git 忽略文件配置模板

    ## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Stud ...

  8. 外壳程序(shell):命令解释器commond

    在Linux系统中有好几种shell,常见的有:ash.bash.zsh.ksh.tcsh等,计算机一般默认的是bash,使用命令echo $SHELL可以看出我们使用的哪种shell

  9. python初始装饰器

    python装饰器: 一,函数名的运用. 函数名是一个变量,但他是一个特殊的变量与括号配合可以执⾏行行函数的变量 1.函数名的内存地址 def func(): print("呵呵" ...

  10. 企业面试题|最常问的MySQL面试题集合(三)

    分区表的原理 分库分表的原理 延伸: MySQL的复制原理及负载均衡 分区表的工作原理 对用户而言,分区表是一个独立的逻辑表,但是底层MySQL将其分成了多个物理子表,这对用户来说是透明的,每一个分区 ...