dinic网络流模板
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网络流模板的更多相关文章
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- DINIC网络流+当前弧优化
DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...
- Power Network POJ - 1459 [网络流模板]
http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
- HDU1532最大流 Edmonds-Karp,Dinic算法 模板
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 2435 dinic算法模板+最小割性质
#include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- 网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...
随机推荐
- 活动倒计时代码(精确到毫秒)jquery插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Symfony2 是什么(转)
本文转自:http://www.cnblogs.com/Seekr/archive/2012/06/15/2550894.html Symfoy2 是什么? PHP世界里又一广受关注的web MVC框 ...
- Jasper_table_Cloud not resolve style(s)
resolve method : delete style="".
- Facebook和Google如何激发工程师的创造力
http://taiwen.lofter.com/post/664ff_ad8a15 今天终于“朝圣”了两个伟大的公司——Facebook和Google,对创造力和驱动力的来源有了更多的理解,尤其是对 ...
- C++虚函数表解析(转)
C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数.这种技术可以让父类的指针有“多种形态”,这是一种泛型技术 ...
- Oracle_OCP课程实验学习
Linux启动oracl.查看lsnrctl状态,然后启动监听start.sqlplus / as sysdba 启动数据库.conn sys/jxsrpv as sysdba .startup Ad ...
- RAILS ON
我是按照下面这个URL来轻快安装的. http://lxiaodao.iteye.com/blog/1579992 (1)RVM官方网站应该是改版过一次, 使用 curl -L https://get ...
- Cracking the coding interview--Q1.6
原文: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...
- MyBatis查询两个字段,返回Map,一个字段作为key,一个字段作为value的实现
1. 问题描述 在使用MyBatis,我们经常会遇到这种情况:SELECT两个字段,需要返回一个Map,其中第一个字段作为key,第二个字段作为value.MyBatis的MapKey虽然很实用,但并 ...
- mysql之事务
事务处理 begin 开始一个事物 commit 事务确认 rollback 事务回滚 end 事务结束 innodb下可以实现事务 开始执行事务时如果 ...