网络流之最大流Dinic --- poj 1459
Description

Input
Output
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15
6
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int N = ;
const int MAXN = 1e9 + ; struct Edge {
int to;
int value;
int next;
}e[*N*N];
int head[N], cnt;
int deep[N];
int n, np, nc, m; void insert(int u, int v, int value) {
e[++cnt].to = v;
e[cnt].value = value;
e[cnt].next = head[u];
head[u] = cnt;
} void init() {
memset(head, -, sizeof(head));
cnt = -;
} bool BFS() {
memset(deep,-,sizeof(deep));
queue<int> Q;
deep[] = ;
Q.push();
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int edge = head[u]; edge != -; edge = e[edge].next) {
int v = e[edge].to;
if (deep[v] == - && e[edge].value > ) {
deep[v] = deep[u] + ;
Q.push(v);
}
}
}
if (deep[n + ] == -) return false;
return true;
} int DFS(int u,int flow_pre) {
if (u == n + ) return flow_pre;
int flow = ;
for (int edge = head[u]; edge != -; edge = e[edge].next) {
int v = e[edge].to;
if (deep[v] != deep[u]+ || e[edge].value==) continue;
int _flow= DFS(v, min(flow_pre, e[edge].value));
flow_pre -= _flow;
flow += _flow;
e[edge].value -= _flow;
e[edge ^ ].value += _flow;
if (flow_pre == ) break;
}
if (flow == ) deep[u] = -;
return flow;
}
int GetMaxFlow() {
int ans = ;
while (BFS()) {
ans += DFS(,MAXN);
}
return ans;
}
int main()
{
while (scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF) {
init();
int u, v, z;
for (int i = ; i < m; i++) {
scanf(" (%d,%d)%d", &u, &v, &z);
insert(u+, v+, z);
insert(v+, u+, );
}
for (int i = ; i < np; i++) {
scanf(" (%d)%d", &u, &z);
insert(, u+, z);
insert(u+, , );
}
for (int i = ; i < nc; i++) {
scanf(" (%d)%d", &u, &z);
insert(u + , n + , z);
insert(n + , u + , );
}
printf("%d\n",GetMaxFlow());
}
}
网络流之最大流Dinic --- poj 1459的更多相关文章
- 网络流之最大流EK --- poj 1459
题目链接 本篇博客延续上篇博客(最大流Dinic算法)的内容,此次使用EK算法解决最大流问题. EK算法思想:在图中搜索一条从源点到汇点的扩展路,需要记录这条路径,将这条路径的最大可行流量 liu 增 ...
- 网络流之最大流Dinic算法模版
/* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...
- 我爱网络流之最大流Dinic
直接上大佬博客: Dinic算法详解及实现来自小菲进修中 Dinic算法(研究总结,网络流)来自SYCstudio 模板步骤: 第一步,先bfs把图划分成分成分层图网络 第二步,dfs多次找增广路 当 ...
- 网络流(最大流-Dinic算法)
摘自https://www.cnblogs.com/SYCstudio/p/7260613.html 网络流定义 在图论中,网络流(Network flow)是指在一个每条边都有容量(Capacity ...
- [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]
题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- POJ 3436 ACM Computer Factory (网络流,最大流)
POJ 3436 ACM Computer Factory (网络流,最大流) Description As you know, all the computers used for ACM cont ...
- poj 1459 多源多汇点最大流
Sample Input 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 ...
随机推荐
- 7.Java基础_Java数据输入
import java.util.Scanner; public class Output { public static void main(String[] args){ Scanner sc=n ...
- 201871010111-刘佳华《面向对象程序设计(java)》第七周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第七周学习总结 实验时间 2019-10-11 1.实验目的与要求 1) 掌握四种访问权限修饰符的使用特点: (1)进一步理 ...
- LG2996 「USACO10NOV」Visiting Cows
问题描述 LG2996 题解 和没有上司的舞会双倍经验? \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; te ...
- VMWare虚拟机提示:另一个程序已锁定文件的一部分,打不开磁盘...模块"Disk"启动失败的解决办法
重启了电脑之后,打开VMware就发现出现了“锁定文件失败,打不开磁盘......模块"Disk"启动失败.”这些文字 为什么会出现这种问题: 这是因为虚拟机在运行的时候,会锁定文 ...
- array 数组
- 记 2019蓝桥杯校内预选赛(JAVA组) 赛后总结
引言 好像博客好久没更新了 哈哈哈哈哈 趁现在有空更新一波 不知道还有没有人看 确实该记录一下每天做了什么了 不然感觉有些浑浑噩噩了 比赛介绍 全称: 蓝桥杯全国软件和信息技术专业人才大赛 蓝桥杯 实 ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [NewLife.XCode]百亿级性能
NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...
- 从游击队到正规军:马蜂窝旅游网的IM系统架构演进之路
本文引用自马蜂窝公众号,由马蜂窝技术团队原创分享. 一.引言 今天,越来越多的用户被马蜂窝持续积累的笔记.攻略.嗡嗡等优质的分享内容所吸引,在这里激发了去旅行的热情,同时也拉动了马蜂窝交易的增长.在帮 ...
- 安装pip-9.0.1-py2.py3-none-any.whl
pip的安装 1.从https://pypi.python.org/pypi/pip#downloads下载所需的.whl文件 2.将下载的文件放入Python的根目录 我的根目录是F:\Python ...