【模板】【网络流】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. ...
随机推荐
- (转载)mysql_query( )返回值
(转载)http://hi.baidu.com/tfbzccqceabfhyd/item/bd01db9f8995204af04215e4 调用mysql_query( ),当查询操作是update. ...
- iOS屏蔽高频点击技巧
例如高频率点击一个按钮或者TableViewCell,会造成功能多次重复执行,在异步网络请求时候或者多线程时候,造成的问题尤其明显. 解决方法: 声明一个属性self.actionWorking ,标 ...
- JavaScript高级程序设计2.pdf
第三章 基本概念 区分大小写 ECMAScript中的一切(变量.函数名和操作符)都区分大小写 标识符 指变量.函数.属性的名字或者函数的参数 第一个字符必须是一个字母.下划线或美元符号,其它字符可以 ...
- margin设置为负数
1.为负margin“平反” 我们在CSS中都会使用margin,但将margin设置成负数,那可能就不大好处理了.在网页设计中,人们对负margin用法的态度大相径庭,有的人非常喜欢,而有的人则认为 ...
- 3D视频的质量评价报告 (MSU出品)
俄罗斯的MSU Graphics & Media Lab (Video Group)出品的3D视频的质量评价报告.测试了一些3D视频的质量,其测试方法值得我们参考.在此翻译一下部分文字. 注: ...
- Java开发中文件读取方式总结
JAVA开发中,免不了要读文件操作,读取文件,首先就需要获取文件的路径. 路径分为绝对路径和相对路径. 在文件系统中,绝对路径都是以盘符开始的,例如C:\abc\1.txt. 什么是相对路径呢?相对路 ...
- AFNetworking 使用总结
NSString *URLTmp = @""; NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncod ...
- Microsoft office word关闭英文输入首字母大写设置
1.概述: 在使用office word的时,经常出现输入一段不需首字母大写的英文时,通常敲击完回车时word会自动将首字母大写,需要重新将首字母修改成小写,这样操作很不方便.于是需要对这个功能进行一 ...
- 1/8=1/a+1/b,a,b为自然数
#include "stdio.h" int main(){ int a; int b; for(a=1;a<1000;a++) { for(b=1;b<1000; ...
- js添加遮罩层
直接用代码来说明 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MaskT ...