/*
唐代杜荀鹤
《小松》
自小刺头深草里,而今渐觉出蓬蒿。
时人不识凌云木,直待凌云始道高。
*/
#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的更多相关文章

  1. 模板——网络流Dinic

    感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...

  2. 模板-网络流-Dinic

    //Dinic struct Edge{ int from,to,cap,flow; Edge(){ } Edge(int a,int b,int c,int d){ from=a; to=b; ca ...

  3. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  4. ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)

    //有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...

  5. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  6. POJ 3281 [网络流dinic算法模板]

    题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...

  7. 网络流dinic ek模板 poj1273

    这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...

  8. Power Network POJ - 1459 网络流 DInic 模板

    #include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...

  9. 网络流Dinic模板

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  10. 网络流dinic模板,邻接矩阵+链式前向星

    //这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...

随机推荐

  1. Linux学习笔记2——Linux中常用文件目录操作命令

    ls 显示文件和目录列表 -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 mkdir 创建目录 -p 父目录不存在情况下先生成父目录 cd 切换目录 touch 生成一个空文件 e ...

  2. UVa816 Abbott's Revenge

    Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge  Abbott’s Revenge The 1999 World FinalsC ...

  3. 《University Calculus》-chape10-向量与空间几何学-向量夹角

    点积.向量夹角: 无论对于空间向量还是平面向量,我们所熟知的是:给出任意两个向量,我们都能够根据公式计算它们的夹角,但是这个夹角必须是将两个向量的起点重合后所夹成的小于等于π的角,可是,这是为什么呢? ...

  4. Validate XML using a XSD (XML Schema)

    Consider this XML file howto.xml : <?xml version="1.0" encoding="ISO-8859-1"? ...

  5. 2014年河南省第七届ACM大赛总结

    虽然大赛已经结束了两天,不过比赛的场景还是不断地在眼前回放,一遍遍,这次的比赛给了我很深刻的感悟还有教训. 刚开始比赛选择了贩卖武器那道题,也是全场到最后唯一没有被人做出来的一道题,策略的严重错误,大 ...

  6. poj 3159 Candies

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 25776   Accepted: 7033 Descrip ...

  7. AE 线编辑

    转自原文 AE 线编辑 1.高亮显示节点 //高亮显示节点和端点 public void HighLightNode() { //清空 _mapCtrl.Map.ClearSelection(); _ ...

  8. 深入理解C#:编程技巧总结(一)

    原创文章,转载请注明出处! 以下总结参阅了:MSDN文档.<C#高级编程>.<C#本质论>.前辈们的博客等资料,如有不正确的地方,请帮忙及时指出!以免误导! 1.实现多态性的两 ...

  9. jQuery 实现上下,左右滑动

    前几天的任务:http://t.sina.com.cn/  的下滑效果. 渐变移动出足够的空白 -> 淡出最后一个 ->渐变移动出足够的空白 我们要做的是向左移动效果.这个效果用时需添加一 ...

  10. my.cnf已经存在,影响安装--mysql

    Found existing config file ./my.cnf on the system. Because this file might be in use, it was not rep ...