[最小割最大流 || 最短路] roadblock Dinic SPFA + SLE
大家都很强, 可与之共勉。
震惊!cky 竟率兵攻打jyb!
jyb 面对强敌,振作精神,想要抗击侵略。jyb 的国家一共有n 个城市,m 条距离为1 的双向道路。现在jyb的所在地是1 号城市,cky 已经占领了n 号城市。jyb 得到线报,cky 想要走最近的路线抓到自己,而jyb 还在调集部队,所以他希望拖延一点时间。他派出了一些工程人员去破坏道路,破坏每条道路都要消耗一定的资源,jyb 希望消耗最小的资源并且保证能拖延到cky。
Input
第1 行,1 个整数T, 表示数据组数,对于每组数据:
第1 行,2 个整数n;m,表示城市数和道路数
接下来m 行,每行3 个整数u; v;w, 表示城市u 到城市v 有一条路,破坏消耗的资源为w。
Output
对于每组数据,输出最小消耗的资源。
Sample
roadblock.in
1
4 4
1 2 1
2 4 2
3 1 3
4 3 4
roadblock.out
4
数据范围:
1 T 5, 1 n 1000,1 m 10000,1 w 300000。
题意其实挺简单的,先BFS找出最短路,因为边权为1,最短路显然有很多,然后我们建一个只有最短路上边的新图,然后跑出最小割就好。需要加当前弧优化才能过所有点。
调了我好久啊, 天哪!
#include "queue"
#include "cctype"
#include "cstdio"
#include "cstring"#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define rep(i, m, n) for(register int i = (m); i <= (n); ++i)
#define edges(u) for(register int i = head[u]; i; i = g[i].pre)template <class T>
inline bool readIn(T &x) {T opt = 1;static char ch;while(!isdigit(ch = (char) getchar()) && (ch ^ -1) && (ch ^ 45));if(ch == -1) return false;if(ch == 45) opt = -1, ungetc(ch, stdin);for(x = -48 + ch; isdigit(ch = (char) getchar()); x = (x << 1) + (x << 3) + ch - 48);x *= opt;return true;
}template <class T>
inline bool writeIn(T x) {T st[25], top = 0;if(x < 0) putchar('-'), x = -x;if(x == 0) { putchar(48); return true; }while(x) st[++top] = x % 10, x /= 10;while(top) putchar(st[top--] + 48);return true;
}const int oo = 0x3f3f3f3f;struct edge {int to, pre, cost;edge(int to = 0, int pre = 0, int cost = 0) : to(to), pre(pre), cost(cost) { }
} g[20005];struct Edge {int to, cap, flow, pre;Edge(int to = 0, int flow = 0, int cap = 0, int pre = 0) : to(to), flow(flow), cap(cap), pre(pre) { }
} e[20005];typedef long long LL;int T, n, m, tot, ne, head[10005], dis1[10005], dis2[10005], u[10005], v[10005], w[10005];
LL ans;void SPFA(int s, int t) {static std::deque<int> q;static bool vis[10005];memset(dis1, 0x3f, sizeof (int) * (n + 1));memset(dis2, 0x3f, sizeof (int) * (n + 1));memset(vis, false, sizeof (int) * (n + 1));q.push_front(s); vis[s] = true; dis1[s] = 0;while( !q.empty() ) {int u = q.front(); q.pop_front();vis[u] = false;edges(u) {int v = g[i].to;if( dis1[v] > dis1[u] + 1 ) {dis1[v] = dis1[u] + 1;if( !vis[v] ) {if( q.empty() || dis1[v] < dis1[q.front()] )q.push_front(v);elseq.push_back(v);vis[v] = true;}}}}memset(vis, false, sizeof (int) * (n + 1));q.push_front(t); vis[t] = true; dis2[t] = 0;while( !q.empty() ) {int u = q.front(); q.pop_front();vis[u] = false;edges(u) {int v = g[i].to;if( dis2[v] > dis2[u] + 1 ) {dis2[v] = dis2[u] + 1;if( !vis[v] ) {if( q.empty() || dis2[v] < dis2[q.front()] )q.push_front(v);elseq.push_back(v);vis[v] = true;}}}}
}class Dinic {
public:int d[10005], cur[10005], s, t;std::queue<int> Q;bool vis[10005];bool bfs(int s, int t) { memset(vis, false, sizeof (int) * (n + 1));d[s] = 0, vis[s] = true;Q.push(s);while(!Q.empty()) {int u = Q.front(); Q.pop();for( int i = head[u]; i; i = e[i].pre ) {int v = e[i].to;if( !vis[v] && e[i].cap > e[i].flow ) {vis[v] = true;d[v] = d[u] + 1;Q.push(v);}}}return vis[t];}int dfs( int u, int a ) {if( u == t || !a ) return a;int flow = 0, f;for(register int &i = cur[u]; i; i = e[i].pre ) {int v = e[i].to;if( d[u] + 1 == d[v] && (f = dfs(v, min(e[i].cap - e[i].flow, a))) > 0) {e[i].flow += f;e[i^1].flow -= f;flow += f;a -= f;if(!a) return flow;}}return flow;}int Maxflow( int s, int t ) {this->s = s, this->t = t;int flow = 0;while( bfs( s, t ) ) {rep(i, 1, n) cur[i] = head[i];flow += dfs( s, oo );}return flow;}
} M;#define adde(u, v, cost) { g[++ne] = edge(v, head[u], cost); head[u] = ne; }inline void initialize() {readIn(n);readIn(m); ne = 1, tot = 0;memset(head, 0, sizeof (int) * (n + 1));int x, y, o;while( m-- ) {readIn(x);readIn(y);readIn(o);adde(x, y, o);adde(y, x, o);}SPFA(1, n);rep(x, 1, n) edges(x) if(dis1[x] + dis2[g[i].to] + 1 == dis1[n]) { u[++tot] = x, v[tot] = g[i].to, w[tot] = g[i].cost; }ne = 1;memset(head, 0, sizeof (int) * (n + 1));rep(i, 1, tot) {e[++ne] = Edge(v[i], 0, w[i], head[u[i]]); head[u[i]] = ne; e[++ne] = Edge(u[i], 0, 0, head[v[i]]); head[v[i]] = ne;}
}int main() {freopen("roadblock.in", "r", stdin);freopen("roadblock.out", "w", stdout);for(readIn(T); T; --T) {initialize();writeIn(M.Maxflow(1, n));putchar(10);}
}
[最小割最大流 || 最短路] roadblock Dinic SPFA + SLE相关推荐
- 网络流 最大流 最小割 费用流
[腾讯文档]网络流初步 网络流初步 文章目录 网络流初步 一.网络流简介 1. 网络 2. 流 3. 再次理解网络流 二.常见题型(三种) 三.相关问题对应算法介绍 1.最大流 (1) FF算法 - ...
- hdu 4289(最小割最大流定理)
题意:有N个城市,现在城市S出现了一伙歹徒,他们想运送一些炸弹到D城市,不过警方已经得到了线报知道他们的事情,不过警察不知道他们所在的具体位置,所以只能采取封锁城市的办法来阻断暴徒,不过封锁城市是需要 ...
- HDU-1569 方格取数(2) 最小割最大流
题义很简单,还记得方格取数(1)的时候,使用状态压缩写的,这里由于行列数太大,因此无法进行压缩.所以要运用的最小割最大流的思想来解这道题. 大概是这样分析的,题义是要我们求在一个方格内取出N个点,使得 ...
- Destroying The Graph 最小点权集--最小割--最大流
Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...
- 图像分割经典算法--《最小割最大流》(Minimum Cut——Max Flow)
1.算法介绍 最小割算法(Minimum Cut)是图像分割的经典算法之一,同时也在"Graph Cut"."Grab Cut"等算法中都有被使用过.最小割最大 ...
- HDU - 5889 Barricade(最短路+最小割-最大流)
题目链接:点击查看 题目大意:给出一张无向图,每条边的长度为1,第i条边建立障碍的花费为wi,题目要求在保证从1到n号点的所有的最短路径上,都有障碍的情况下的最小费用 题目分析:要求最短路上的最小割, ...
- hdu3035 最小割转换成最短路
题意: 给你一个平面图,要求从求出从左上角到右下角的最小割. 思路: 如果大意的可能直接上来一遍最大流,然后就会各种悲剧的MLE,TLE,其实这个题目可以用到有个论文里面的那个 ...
- UVA - 10480 Sabotage(最小割-最大流+输出割边)
题目链接:点击查看 题目大意:给出一张无向图,1为起点,2为终点,删除每条边都需要权值wi,题目需要求将起点与终点分割为两个部分的最小花费,并且输出方案 题目分析:如果不让输出方案的话就是一个裸的最大 ...
- HDU - 4289 Control(最小割-最大流)
题目链接:点击查看 题目大意:给出一张n个点m条边的无向图,一些恐怖分子要从点st到点ed去安装炸弹,为了阻止他们这样做,必须在某些点布置警察,只要恐怖分子路过警察所在的点就会被逮捕,在某个点布置警察 ...
最新文章
- m叉树的叶子节点数量
- python打字案例_Python 小案例 打字练习
- L2-001. 紧急救援-PAT团体程序设计天梯赛GPLT(Dijkstra算法)
- [转]Java程序员们最常犯的10个错误
- Atitit 数据处理查询 中的异常标准化草案 jpa jdbc hb oql规范attilax总结
- spring事务源码执行过程分析
- unity--newtonsoft.json解析
- 2021最新微信漫画小程序全开源商业版:带漫画资源,带搭建教程,流量变现利器。附安装说明和源码。
- Linux入学—共享文件夹(保姆教程)
- C语言rs485编程,- 第六讲 单片机之c语言RS485通信
- 《股票大作手操盘术--杰西.利弗莫尔》
- c# 阿拉伯数字转成中文
- Rescue-Prime hash STARK 代码解析
- Flashback 技术总结(转贴整理)
- Unity3D UGUI 渐变效果
- 嘿嘿各位兄弟姐妹们,赚钱是一个男人最大的底气和尊严!
- cas latex模板参考文献APA等引用格式(Elsevier期刊)
- 2021年美容师(初级)最新解析及美容师(初级)考试资料
- Apache 配置虚拟主机三种方式
- 创业者需要考虑的外部环境
热门文章
- 在K8s上轻松部署Tungsten Fabric的两种方式
- Microsoft Toolkit(win8.1激活工具)v2.5.2 绿色版
- DM8 数据库连接Alibaba druid 提示:dbType not support
- C/C++计算类/结构体和联合体(union)所占内存大小(内存对齐问题)
- 计算机 语言学 交叉,计算机和语言学
- 胖哈勃杯第十三届CUIT校赛web300短域名工具wp及出题心得
- 《数据库》_考研复试_面试篇
- bboss redis组件使用实例
- 有符号整数除以无符号整数会发生什么??!!!
- 关于C语言二级指针正确使用总结