网络流模板

#include<bits/stdc++.h>
using namespace std;
#define M 10001
#define inf 100000
int ip=0,head[M],lv[M];
struct E{
    int to,next,cap;
}e[M];
void add(int x,int y,int z){
    e[ip].to=y;e[ip].next=head[x];e[ip].cap=z;head[x]=ip++;
    e[ip].to=x;e[ip].next=head[y];e[ip].cap=0;head[y]=ip++;
}
int s,t;
bool bfs(){
    memset(lv,-1,sizeof(lv));
    queue<int> q;
    q.push(s);
    lv[s]=0;
    while(!q.empty()){
        int t=q.front();
        q.pop();
        for(int i=head[t];~i;i=e[i].next){
            if(~lv[e[i].to]&&e[i].cap){
                lv[e[i].to]=lv[t]+1;
                q.push(e[i].to);
            }
        }
    }
    return ~lv[t];
}

int dfs(int u,int f){
    if(u==t||f==0)return f;
    int ret=0;
    for(int i=head[u];~i;i=e[i].next){
        if(lv[e[i].to]==lv[u]+1&&e[i].cap){
            int w=dfs(e[i].to,min(f,e[i].cap));
            e[i].cap-=w;
            e[i^1].cap+=w;
            f-=w;
            ret+=w;
        }
    }
    if(!ret)lv[u]=-1;
    return ret;
}
int dinic(){
    int ans=0;
    while(bfs()){
        ans+=dfs(s,inf);
    }
    return ans;
}

int main(){

    return 0;
}
时间: 2024-07-29 13:19:04

网络流模板的相关文章

Kuangbin网络流模板

Kuangbin网络流模板. const int maxn=20010; const int maxm=200010; const double INF=1e20; const double eps=1e-8; struct Edge{ int to,next; double cap,flow; }edge[maxm],tmpG[maxm]; int n,tot,toth,totv; int head[maxn],tmph[maxn]; int gap[maxn],dep[maxn],cur[m

HDU 4280 Island Transport(网络流模板)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 Problem Description In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies

网络流模板 NetworkFlow

身边的小伙伴们都在愉快地刷网络流,我也来写一发模板好了. Network Flow - Maximum Flow Time Limit : 1 sec, Memory Limit : 65536 KB Japanese version is here Maximum Flow A flow network is a directed graph which has a  and a . In a flow network, each edge has a capacity . Each edge

算法复习——网络流模板(ssoj)

题目: 题目描述 有 n(0<n<=1000)个点,m(0<m<=1000)条边,每条边有个流量 h(0<=h<35000),求从点 start 到点 end 的最大流. 输入格式 第一行:4 个整数,分别是 n,m,start,end .接下来有 m 行,每行四个三个整数 a,b,h,分别表示 a 到 b,流量为 h 的一条边. 输出格式 输出从点 start 到点 end 的最大流. 样例数据 1 输入 [复制] 7 14 1 7 1 2 5 1 3 6 1 4 5

hdu1532——Drainage Ditches(网络流模板)

Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of d

上下界的网络流模板

上下界网络流问题对于每一条边.都有流量上下限的限制 而普通的网络流就只有上限限制 下面分别给出几种经典上下界网络流问题的模板 参考博文Ⅰ.参考博文Ⅱ 1.无源汇的上下界可行流 实际也就是能否找出一个循环流.使得每个点的流入总流量 == 流出总流量 对于原图的每一条边在网络流中容量应当为 (上界 - 下界) 而后计算每个点流入流量的下界总和记为 in .流出流量的下界总和记为 out 抽象出超级源汇 ss 与 tt 对于原图中的每一个点 如果 in - out > 0 则 ss 与这个点连边.容量

Drainage Ditches--hdu1532(网络流 模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13790    Accepted Submission(s): 6527 Problem Description Every time it rains on F

HDU 3549 Flow Problem(网络流模板题)

记录一下模板 #include <vector> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; #define maxn 1100 #define INF 0x7f7f7f7f struct Edge { int from,to,cap,flow; }; struct Dinic { int n,m,s

Drainage Ditches - poj 1273(网络流模板)

题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************** 先用Edmonds-Karp的算法做一下试试吧 重边贡献了 1W,要加上所有的重边才算是两点间最大流量 ************************************************************************* /*********************

POJ 1273 Drainage Ditches(网络流模板)

Description: Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage