HDU-3605-Escape(最大流, 状态压缩)
链接:
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(最大流, 状态压缩)的更多相关文章
- Hdu 3605 Escape (最大流 + 缩点)
题目链接: Hdu 3605 Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...
- HDU 3605 Escape 最大流+状压
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3605 Escape 最大流
题意: 如果这是2012年世界末日怎么办?我不知道该怎么做.但是现在科学家们已经发现,有些星球上的人可以生存,但有些人却不适合居住.现在科学家们需要你的帮助,就是确定所有人都能在这些行星上生活.输入多 ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3605:Escape(最大流+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...
- HDU 3605 Escape(状压+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- hdu3605(最大流+状态压缩)
传送门:Escape 题意:给出每个人适合住的星球信息和该星球能住多少人 ,第一行给出n m 代表有 n 个人 m 个星球,然后接下来n行每行m个数字 1代表适合第 i 个星球 0 代表不适合第 i ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
随机推荐
- Elasticsearch 6.2.3版本 Windows环境 简单操作
背景描述 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...
- 【SSM】---增删改查
20:43:06 package com.chinasofti.dao; import java.util.List; import com.chinasofti.entity.User; publi ...
- Sublime Text 3 相关
Sublime Text 3 相关 Sublime Text 3是款非常实用代码编辑神器,但是想要用任何一款软件,掌握一些快捷键还是很有必要的.. 将Sublime Text 3 添加到右键选项中 打 ...
- NFA转换为等价的DFA
在编译系统中,词法分析阶段是整个编译系统的基础.对于单词的识别,有限自动机FA是一种十分有效的工具.有限自动机由其映射f是否为单值而分为确定的有限自动机DFA和非确定的有限自动机NFA.在非确定的有限 ...
- 【AI-人工智能-mmdetection】ModuleNotFoundError: No module named 'mmdet.version'
在集成 mmdetection 框架时遇到这样的问题. ModuleNotFoundError: No module named 'mmdet.version' mmdetection 框架搭建过程很 ...
- 【神经网络与深度学习】【Python开发】Caffe配置 windows下怎么安装protobuf for python
首先从google上下载protobuf-2.5.0.zip和protoc-2.5.0-win32.zip,然后把protoc-2.5.0-win32.zip里的protoc.exe放到protobu ...
- Akka系列(六):Actor解决了什么问题?
前言..... 文档来源于 : What problems does the actor model solve? Actor解决了什么问题? Akka使用Actor模型来克服传统面向对象编程模型的 ...
- Spring(二)--Spring入门案例
Spring入门案例 1.需要的实体类 2.需要的接口和实现类 3.需要的service和实现类 /** * service层的作用 * 在不改变dao层代码的前提下,增加业务逻辑操作 */ publ ...
- MySQL 用 limit 为什么会影响性能?
一,前言 首先说明一下MySQL的版本: mysql> select version();+-----------+| version() |+-----------+| 5.7.17 |+-- ...
- [题解][洛谷]_U75702/P5462_X龙珠_论何为字典序
赛时嫌麻烦,没写 赛后自闭了,写了一下午 题目描述 “X龙珠”是一款益智小游戏.游戏中有 n(2|n)n(2∣n) 个编号互不相同龙珠按照给定的顺序排成一个队列,每个龙珠上面都有一个编号.每次操作时, ...