src:源点

sink:汇点

#include<queue>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std; const int inf = ;
const int maxn = , maxm = ;
struct Edge{
int v, f, nxt;
};
int src, sink;
int g[maxn + ];
int nume;
Edge e[maxm*+];
void addedge(int u, int v, int c){
e[++nume].v = v;
e[nume].f = c;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].f = ;
e[nume].nxt = g[v];
g[v] = nume;
}
void init(){
memset(g, , sizeof(g));
nume = ;
}
queue<int> que;
bool vis[maxn +];
int dist[maxn + ];
void bfs(){
memset(dist, , sizeof(dist));
while(!que.empty()) que.pop();
vis[src] = ;
que.push(src);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = g[u]; i; i = e[i].nxt)
if(e[i].f && !vis[e[i].v]){
que.push(e[i].v);
dist[e[i].v] = dist[u] + ;
vis[e[i].v] = true;
}
}
}
int dfs(int u, int delta){
if(u == sink){
return delta;
}else{
int ret = ;
for(int i = g[u]; delta && i; i = e[i].nxt){
if(e[i].f && dist[e[i].v] == dist[u] +){
int dd = dfs(e[i].v, min(e[i].f, delta));
e[i].f -= dd;
e[i^].f += dd;
delta -= dd;
ret += dd;
}
}
return ret;
}
}
int maxflow(){
int ret = ;
while(true){
memset(vis, , sizeof(vis));
bfs();
if(!vis[sink])return ret;
ret += dfs(src, inf);
}
return ret;
}
int main(){
int n, m;
while(scanf("%d%d", &n, &m)!=EOF){
init();
src = ;
sink = m;
for(int i = ; i < n; i++){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
}
printf("%d\n", maxflow());
}
}

dinic网络流模板的更多相关文章

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

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

  2. DINIC网络流+当前弧优化

    DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...

  3. Power Network POJ - 1459 [网络流模板]

    http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...

  4. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

  5. HDU1532最大流 Edmonds-Karp,Dinic算法 模板

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  6. hdu 2435 dinic算法模板+最小割性质

    #include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...

  7. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

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

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

  9. 网络流dinic ek模板 poj1273

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

随机推荐

  1. How To Learn English Very Fast

    How do you learn English very fast? Every week, I get emails about this topic.   Typically, someone ...

  2. 清除IE下链接虚线框

    方法一:利用javascript的onfocus事件,实现如下: Html代码 <a href="http://www.www.baidu.com"  onfocus=&qu ...

  3. 转:fopen与open可以转换吗

    绝对不可以.fopen是C运行库级别的函数,而open是system call的wrapper routine.fopen返回FILE *的指针,这个结构本身维护着一些关于这个文件的信息,而open返 ...

  4. 高性能web

    http://developer.51cto.com/art/201104/255619.htm http://developer.51cto.com/art/201104/254031.htm ht ...

  5. 自定义事件实现不同窗体间的通讯Delphi篇

    要实现子窗体与父窗体之间的通讯,有多种方法(比如:重载子窗体的构造函数,将父窗体的引用作为参数传递给子窗体).下面我要介绍的是利用自定义事件的方法,它能够最大程度的避免模块之间的耦合,充分体现面向对象 ...

  6. curl_easy_setopt-curl库的关键函数之一

    函数原型:#include <curl/curl.h>CURLcodecurl_easy_setopt(CURL *handle, CURLoption option, parameter ...

  7. HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

    Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn ...

  8. cf581C Developing Skills

    Petya loves computer games. Finally a game that he's been waiting for so long came out! The main cha ...

  9. spring aop实现原理

    通过两种代理方式,一是JDK本身的代理方式,二是CGLIB提供的代理方式,在代理类的前面加事务begin,在后面加事务commit,需要的数据库连接从ThreadLocal中取

  10. linux下常用FTP命令

    linux下常用FTP命令 1. 连接ftp服务器 1. 连接ftp服务器格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1b)服 ...