HDU 4309 Seikimatsu Occult Tonneru (状压 + 网络流)
题意:输入 n 个城市 m 条边,但是边有三种有向边 a b c d,第一种是 d 是 0,那么就是一条普通的路,可以通过无穷多人,如果 d < 0,那么就是隧道,这个隧道是可以藏 c 个人,当然也是通过无穷多人的,如果 d > 0,那么这是一座桥,第一次可以通过一个人,如果修复的话,就可以通过无穷多人,问你最多藏的人数,还有最少花费。
析:只是这样是不能做的,但是题目说了桥不超过 12 个,说实话这个条件太隐蔽了,就是不想让人发现,可惜的是队友没读出来,我也实在是没想出来怎么做,后来一查题解,知道有这个条件,那么很简单了,枚举桥的每一个状态,是修还是不修,每次跑一次最大流,进去判断,下面说一下怎么建图。
建立一个超级源点 s 和超级汇点 t,对于每个城市,从 s 向每个城市连一条边,容量就是城市人数,然后对于普通的路,那么就直接连接容量无穷大,对于隧道也是直接连接容量无穷大,然后把隧道向汇点 t 连接,容量是可以藏人的数,注意连的隧道的左端点,最后是桥,每次枚举桥的状态,如果是修复,那么就连一条容量无穷大的,如果不修复,那么就连一条容量为 1 的边,然后就OK了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 20;
const int maxm = 1e6 + 10;
const LL mod = 1000000000000000LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } struct Edge{
int from, to, cap, flow;
}; struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init(int n){
FOR(i, n, 0) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap){
edges.pb((Edge){from, to, cap,0});
edges.pb((Edge){to, from, 0, 0});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
} bool bfs(){
ms(vis, 0); vis[s] = 1; d[s] = 0;
queue<int> q; q.push(s); while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(!vis[e.to] && e.cap > e.flow){
d[e.to] = d[u] + 1;
vis[e.to] = 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int u, int a){
if(u == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[u]; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
e.flow += f;
edges[G[u][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
} int maxFlow(int s, int t){
this->s = s; this->t = t;
int flow = 0;
while(bfs()){ ms(cur, 0); flow += dfs(s, INF); }
return flow;
}
}; Dinic dinic; struct Node{
int u, v, c;
};
vector<Node> bridge; int main(){
while(scanf("%d %d", &n, &m) == 2){
int s = 0, t = n + 1;
dinic.init(t + 5);
bridge.cl;
for(int i = 1; i <= n; ++i) dinic.addEdge(s, i, readInt());
bool ok = false;
for(int i = 1; i <= m; ++i){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if(d == 0) dinic.addEdge(a, b, INF);
else if(d < 0){
dinic.addEdge(a, b, INF);
dinic.addEdge(a, t, c);
ok = true;
}
else bridge.pb((Node){a, b, c});
}
if(!ok){ puts("Poor Heaven Empire"); continue; }
int ans1 = 0, ans2 = 0;
int all = 1<<bridge.sz;
for(int i = 0; i < all; ++i){
int tmp = 0;
for(int j = 0; j < bridge.sz; ++j)
if(i&1<<j){
tmp += bridge[j].c;
dinic.addEdge(bridge[j].u, bridge[j].v, INF);
}
else dinic.addEdge(bridge[j].u, bridge[j].v, 1);
int res = dinic.maxFlow(s, t);
if(res > ans1){
ans1 = res;
ans2 = tmp;
}
else if(res == ans1 && ans2 > tmp) ans2 = tmp;
for(int j = 0; j < bridge.sz; ++j){
dinic.edges.pop_back();
dinic.G[bridge[j].u].pop_back();
dinic.G[bridge[j].v].pop_back();
}
for(int i = 0; i < dinic.edges.sz; ++i)
dinic.edges[i].flow = 0;
}
if(ans1 == 0) puts("Poor Heaven Empire");
else printf("%d %d\n", ans1, ans2);
}
return 0;
}
HDU 4309 Seikimatsu Occult Tonneru (状压 + 网络流)的更多相关文章
- HDU 4309 Seikimatsu Occult Tonneru
Seikimatsu Occult Tonneru Time Limit: 6000ms Memory Limit: 32768KB This problem will be judged on HD ...
- HDU 4309 Seikimatsu Occult Tonneru 网络流量+像缩进
主题链接:点击打开链接 意甲冠军: 题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图. 每一个点上都有一些人.每条边有4个属性(u,v,w,p). 这些边分为三种:( ...
- HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- hdu 5691 Sitting in Line 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 题解: 和tsp用的状压差不多,就是固定了一些访问顺序. dp[i][j]表示前cnt个点中布 ...
- HDU 6149 Valley Numer II 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 3605 Escape 最大流+状压
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- 插件: Hammer.js
官网: http://hammerjs.github.io/ hammer.js 官网 http://hammerjs.github.io/api/ 官网API(官网写的实在太简了!不好用.注意里面 ...
- LNMP 支持 ThinkPHP 的 pathinfo 模式
注意使用LNMP 1.4版 1.修改php.ini 启用pathinfo /usr/local/php/etc/php.ini cgi.fix_pathinfo = 0 值改为1 2.修改/usr/l ...
- mysql连接测试java脚本
JDBC.java import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.uti ...
- 使用wireshark以及filddler配合抓去手机端的TCP以及HTTP请求
在测试手机客户端时,有时候需要查看网络请求状况.使用在IDE中查看log的方式,能够解决问题,但是会比较复杂.wireshark不能够做代理,而fiddler主要是抓HTTP请求,没有wireshar ...
- 最新ceph集群常用命令梳理
结合网络.官网.手动查询等多方渠道,整理ceph维护管理常用命令,并且梳理常规命令在使用过程中的逻辑顺序.另外整理期间发现ceph 集群的命令体系有点乱,详细情况各自体验. 一:ceph集群启动.重启 ...
- 配置远程主机http服务器 打包资源
<1> 搭建nginx 验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...
- Linux下,如何查看磁盘是否包含数据
可以使用lquerypv -h来查看磁盘是否包含数据,或磁盘头是否被dd过.这在安装RAC的过程中,是非常实用的一个命令.如果不包括数据的话,那么如下所示: [ZFFR4CB2101:root]/]& ...
- 微信小程序和微信公众号的id是一个吗
首先,简单说下我遇到的问题是我们的程序调用微信小程序得到openid,然后通过openID得到用户的唯一标识,用户得以登录,然而,当我们调用微信公众号也同样的到openid,同一以用户两个不同的ope ...
- 小强学渲染之OpenGL状态机理解
状态机是理论上的一种机器,呃这个说法非常非常的抽象.通俗一点理解,状态机描述了一个对象在其生命周期内所经历的各种状态,状态间的转变,发生转变的动因,条件及转变中所执行的活动.或者说,状态机是一种行为, ...
- python 迭代器生成
博客:http://www.cnblogs.com/alex3714/articles/5765046.html 列表生成式 [i*2 for i in range(10)] #创建时就生成,不调用也 ...