【模板】【网络流】Dinic
/*
唐代杜荀鹤
《小松》
自小刺头深草里,而今渐觉出蓬蒿。
时人不识凌云木,直待凌云始道高。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <set>
#define LOCAL
const int INF = 0x7fffffff;
const int MAXN = + ;
const int maxnode = * + * ;
const int MAXM = + ;
const int MAX = ;
using namespace std;
struct Edge{
int u, v, c, f;
void init(int a, int b, int d, int e){
u = a;v = b;
c = d;f = e;
}
}edges[MAXM];
int next[MAXN], M, cur[MAXN], vis[MAXN];
int dist[MAXN], head[MAXN], n, m, sta, end; void addEdge(int u, int v, int c){
edges[M].init(u, v, c, );M++;
edges[M].init(v, u, , );//反向边
next[M - ] = head[u];
head[u] = M - ;
next[M] = head[v];
head[v] = M++;
return;
}
void init(){
M = ;//总边数
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++){
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
addEdge(u, v, c);
}
sta = ;
end = n;
}
bool bfs(){
memset(vis, , sizeof(vis));
queue<int>Q;
dist[sta] = ;
vis[sta] = ;
Q.push(sta);
while (!Q.empty()){
int u = Q.front();
Q.pop();
for (int i = head[u]; i != -; i = next[i]){
int e = i, v = edges[e].v;
if (vis[v]) continue;
if (edges[e].c > edges[e].f){
vis[v] = ;
dist[v] = dist[u] + ;
Q.push(v);
}
}
}
return vis[end];
}
int dfs(int x, int a){
if (x == end || a == ) return a;
int flow = , f;
if (cur[x] == -) cur[x] = head[x];
for (int &i = cur[x]; i != -; i = next[i]){
int e = i, v = edges[i].v;
if (dist[v] == dist[x] + && (f = dfs(v, min(edges[e].c - edges[e].f, a))) > ){
flow += f;
a -= f;
edges[e].f += f;
edges[e ^ ].f -= f;
if (a == ) break;
}
}
return flow;
}
int Dinic(){
int flow = ;
while (bfs()){
memset(cur, -, sizeof(cur));
flow += dfs(sta, INF);
}
return flow;
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
printf("%d", Dinic());
//printf("%d", (a == c));
return ;
}
【模板】【网络流】Dinic的更多相关文章
- 模板——网络流Dinic
感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...
- 模板-网络流-Dinic
//Dinic struct Edge{ int from,to,cap,flow; Edge(){ } Edge(int a,int b,int c,int d){ from=a; to=b; ca ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- 网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
随机推荐
- 【转】更新SDK后,打包APK时报 the zipalign tool was not found in the sdk
原文网址:http://wyong.blog.51cto.com/1115465/1546632 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...
- ipython notebook使用教程
在一次师兄(师兄博客地址)的例会汇报中,介绍了ipython notebook,当时觉得很酷炫,渐渐自己使用的时候才发现真的很强大.抽空整理下,找了些资料进行补充,并挨个进行了实现,留个笔记,也欢迎喜 ...
- AMBA总线介绍
The Advanced Microcontroller Bus Architecture (AMBA) specification defines an on- chip communication ...
- hdu 4472 Count(递推即dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4472 代码: #include <cstdio> #include <cstring ...
- C/C++基础(二)
(1)运算符优先级 #include <cstdio> using namespace std; int main() { unsigned char a = 0xA5; un ...
- 自定义使用S缓存方法
<?php $info=S("name","lizhaoyao"); $name=S("name"); var_dump($name) ...
- c#基础语言编程-序列化
引言 程序员在编写应用程序的时候往往要将程序的某些数据存储在内存中,然后将其写入某个文件或是将它传输到网络中的另一台计算机上以实现通讯.这个将程序数据转化成能被存储并传输的格式的过程被称为" ...
- c# 字符串转化成声音 分类: C# 2014-09-24 12:20 316人阅读 评论(0) 收藏
说明: (1)支持Window 7系统,但是xp系统智能朗读英文和数字: (2)添加引用 Interop.SpeechLib.dll; (3)使用时调用StringToVoice(str)即可. us ...
- JDK安装目录下的src
学Java这么久,JDK目录下的包有哪些都不知道,今天偶然解压到src明白了许多道理 1.进入JDK安装目录,我的安装路径为:E:\JDK\jdk1.8.0,可以看到这里有个src压缩文件 2.直接解 ...
- mysql使用硬链接配合truncate 删除2.2T的表 --杨奇龙
http://blog.csdn.net/wyzxg/article/details/8626814 http://blog.itpub.net/22664653/viewspace-750408/ ...