题意:输入 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 (状压 + 网络流)的更多相关文章

  1. HDU 4309 Seikimatsu Occult Tonneru

    Seikimatsu Occult Tonneru Time Limit: 6000ms Memory Limit: 32768KB This problem will be judged on HD ...

  2. HDU 4309 Seikimatsu Occult Tonneru 网络流量+像缩进

    主题链接:点击打开链接 意甲冠军: 题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图. 每一个点上都有一些人.每条边有4个属性(u,v,w,p). 这些边分为三种:( ...

  3. HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...

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

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

  5. hdu 5691 Sitting in Line 状压dp

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

  6. HDU 6149 Valley Numer II 状压DP

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

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

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

  8. HDU 1074 Doing Homework(状压DP)

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

  9. HDU 3605 Escape 最大流+状压

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

随机推荐

  1. Vue 折叠面板Collapse在标题上添加组件后,阻止面板冒泡的用法

    iView组件中,折叠面板Collapse点击面板标题部分,会出现面板收起或展开的效果.那么在面板标题后面再添加下拉框之类的组件时,会出现跟面板点击一样的效果,这时候就需要阻止冒泡的用法了.具体代码: ...

  2. 爬虫基础线程进程学习-Scrapy

    性能相关 学习参考:http://www.cnblogs.com/wupeiqi/articles/6229292.html 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时 ...

  3. nobup 与 后台运行命令

    1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_INTERRUPTIBLE),可 ...

  4. 1. jdk内存配置

    -Xms256m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=512m

  5. element-ui table 嵌套

    嵌套的时时候用template,数据 scope.row.xxx <template> <div> <el-table :data="urls" st ...

  6. JPA中建立数据库表和实体间映射小结

    在JPA中,映射数据库表和实体的时候,需要注意一些细节如下, 实体类要用@Entity的注解: 要用 @Id 来注解一个主键: 如果跟数据库相关联,要用@Table注解相关数据库表: 实体类中字段需要 ...

  7. Object.defineProperty(o,p,descriptor ) 理解应用

    1. Object.defineProperty  在一个对象上定义一个新属性,或修改一个已经存在的属性, 最终返回这个对象. var __define = this.__define || func ...

  8. BP神经网络与Python实现

    人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...

  9. board_led.h/board_led.c

    /******************************************************************************* Filename: board_led ...

  10. JMM中的重排序及内存屏障

    目录 1. 概述 2. 重排序 2-1. as-if-serial语义 2-2. 重排序的种类 2-3. 从Java源代码到最终实际执行的指令序列, 会分别经历下面3中重排序. 3. 内存屏障类型 3 ...