hdu 4309 最大流 + DFS
题意:
给以三种有向边
(1) 隧道,可以过无数人,也可以藏c个人。
(2) 路,只能过人(流量INF)。
(3)古桥,如果不修理可以过1个人,修理可以过无数个人,但是要花费c那么多钱 同时还给了每个城市的人数,要求是城市的人去隧道里躲避,问你在躲避人数最多的前提下费用最小,费用就是修理古桥所用的钱。
思路:
先看古桥数量,最多12个,2^12 也就四千多,直接二分枚举哪些古桥是修理的,然后对于每一种状态,我们都重新建图,用最大流去找最多藏多少人,然后更新最优就行了。下面说下建图吧,这个题目的建图不难,直接建就行。
(0)建立超级远点和汇点,s,t。
(1)隧道,既然可以藏人,就可以虚拟出一个点来,比如给的隧道a(点),b(点),c(人数)
我们可以虚拟出来一个点d,add(a ,d ,INF),add(d ,b ,INF) ,add(d ,t ,c);
(2)路,这个没话说 add(a ,b ,INF);
(3)古桥,对于深搜的当前状态的当前这个古桥,如果是修理add(a ,b ,INF),如果不是修理add(a ,b ,1)这样一边重建,一边深搜更新最大值就行了。
#include<stdio.h>
#include<queue>
#include<string.h> #define N_node 500
#define N_edge 10000
#define INF 1000000000
using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; typedef struct
{
int a ,b ,c;
}NODE; typedef struct
{
int x ,t;
}DEP; STAR E[N_edge];
NODE L[1100] ,G[15] ,S[25];
int n1 ,n2 ,n3;
DEP xin ,tou;
int list[N_node] ,tot;
int list1[N_node];
int deep[N_node];
int mk[15];
int pp[1100]; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot; E[++tot].to = a;
E[tot].cost = 0;
E[tot].next = list[b];
list[b] = tot;
} int minn(int x ,int y)
{
return x < y ? x : y;
} bool BFS_Deep(int s ,int n ,int t)
{
memset(deep ,255 ,sizeof(deep));
deep[s] = 0;
xin.x = s ,xin.t = 0;
queue<DEP>q;
q.push(xin);
while(!q.empty())
{
tou = q.front();
q.pop();
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(deep[xin.x] != -1 || !E[k].cost)
continue;
deep[xin.x] = xin.t;
q.push(xin);
}
}
for(int i = 0 ;i <= n ;i ++)
list1[i] = list[i];
return deep[t] != -1;
} int DFS_flow(int s ,int t ,int flow)
{
if(s == t) return flow;
int nowflow = 0;
for(int k = list1[s] ;k ;k = E[k].next)
{
list1[s] = k;
int to = E[k].to;
int c = E[k].cost;
if(deep[to] != deep[s] + 1 || !c)
continue;
int tmp = DFS_flow(to ,t ,minn(c ,flow - nowflow));
nowflow += tmp;
E[k].cost -= tmp;
E[k^1].cost += tmp;
if(flow == nowflow) break;
}
if(!nowflow) deep[s] = 0;
return nowflow;
} int DINIC(int s ,int t ,int n)
{
int ans = 0;
while(BFS_Deep(s ,t ,n))
{
ans += DFS_flow(s ,t ,INF);
}
return ans;
} void Rebuid(int n)
{
memset(list ,0 ,sizeof(list)) ,tot = 1;
int T = n + n1 + 1;
for(int i = 1 ;i <= n1 ;i ++)
{
int a = S[i].a ,b = S[i].b ,c = S[i].c;
add(i + n ,T ,c);
add(a ,i + n ,INF) ,add(i + n ,b ,INF);
}
for(int i = 1 ;i <= n2 ;i ++)
add(L[i].a ,L[i].b ,INF);
for(int i = 1 ;i <= n3 ;i ++)
if(mk[i]) add(G[i].a ,G[i].b ,INF);
else add(G[i].a ,G[i].b ,1);
for(int i = 1 ;i <= n ;i ++)
add(0 ,i ,pp[i]);
} int Min_C ,Max_F;
void DFS(int s ,int t ,int ii ,int nowcost ,int n)
{
if(ii == n3 + 1)
{
Rebuid(n);
int now = DINIC(s ,t ,t);
if(Max_F < now || Max_F == now && Min_C > nowcost)
{
Max_F = now;
Min_C = nowcost;
}
return ;
}
mk[ii] = 1;
DFS(s ,t ,ii + 1 ,nowcost + G[ii].c ,n);
mk[ii] = 0;
DFS(s ,t ,ii + 1 ,nowcost ,n);
} int main ()
{
int n ,m ,i ,q;
while(~scanf("%d %d" ,&n ,&m))
{
for(i = 1 ;i <= n ;i ++)
scanf("%d" ,&pp[i]);
n1 = n2 = n3 = 0;
NODE A;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d %d" ,&A.a ,&A.b ,&A.c ,&q);
if(q < 0) S[++n1] = A;
if(!q) L[++n2] = A;
if(q > 0) G[++n3] = A;
}
Min_C = Max_F = 0;
DFS(0 ,n + n1 + 1 ,1 ,0 ,n);
if(!Max_F) puts("Poor Heaven Empire");
else printf("%d %d\n" ,Max_F ,Min_C);
}
return 0;
}
hdu 4309 最大流 + DFS的更多相关文章
- HDU 1532 最大流入门
1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...
- HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
随机推荐
- kubernetes cpu限制参数说明
docker CPU限制参数 Option Description --cpus=<value> Specify how much of the available CPU resourc ...
- MySql_176. 第二高的薪水 + limit + distinct + null
MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...
- AOP(面向切面编程)大概了解一下
前言 上一篇在聊MemoryCache的时候,用到了Autofac提供的拦截器进行面向切面编程,很明显能体会到其优势,既然涉及到了,那就趁热打铁,一起来探探面向切面编程. 正文 1. 概述 在软件业, ...
- LNMP配置——Nginx配置 —— Nginx的访问日志
一.配置 先来看看Nginx的日志格式 #grep -A2 log_format /usr/local/nginx/conf/nginx.conf log_format combined_realip ...
- Python基础(1)——变量和数据类型[xiaoshun]
目录 一.变量 1.概述 Variables are used to store information to be referenced(引用)and manipulated(操作) in a co ...
- Codeforces Round #558 B2. Cat Party (Hard Edition)
题面: 传送门 题目描述: 题意:确定最大的x,使去除掉前x天的其中一天后,所有不同数字的数量相等. 题目分析: 可能是我太久没打cf了,水题都做不出来. 这道题的关键在于:要记录相同数量,的不同 ...
- git分支管理--rebase&merge详解
目录 分支合并 git merge --squash [分支名] 注意点 git rebase [分支名] git rebase git rebase --abort git rebase -i gi ...
- JS 字符数组和数字数组转换
var newArr = ['1','2','3'].map(Number):// [1,2,3] var newArr =[1,2,3].map(String):// ['1','2','3']
- 攻防世界 reverse BabyXor
BabyXor 2019_UNCTF 查壳 脱壳 dump 脱壳后 IDA静态分析 int main_0() { void *v0; // eax int v1; // ST5C_4 char ...
- Baolu Aggregate TPS Report
1.说明 这是一个基于JMeter官方的Aggregate Report的监听器改进而来的监听器!!! 2.插件背景 早在很久之前,宝路就曾经改造过JMeter的Aggregate Report 的源 ...