最大流自用模板(例题:HDU1532)

[译]网络流Dinic算法-最大流-HDU1532&POJ1273 POJ1273 HDU1532 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51941   Accepted: 19732 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就 阅读详情

三种模板:Edmonds_Karp,Dinic,SAP

例题:

Drainage Ditches(HDU1532)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22365    Accepted Submission(s): 10683)

 

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 drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

 

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond. 

 

Sample Input

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

 

Sample Output

50 

 

Source

HDU1532

题意:最大流模板题

方法一:Edmonds_Karp
 

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int pre[maxn];

void init(int n)
{
	for (int i=0;i<n;i++)
		G[i].clear();
	edges.clear();
}

void addedge(int from,int to,int cap)
{
	edges.push_back(Edge(from,to,cap,0));
	edges.push_back(Edge(to,from,0,0));
	int m=edges.size();
	G[from].push_back(m-2);
	G[to].push_back(m-1);
}

int Edmonds_Karp(int s,int t)
{
	int flow=0;
	while(1)
	{
		memset(a,0,sizeof(a));
		queue<int> Q;
		Q.push(s);
		a[s]=INF;
		while(!Q.empty())
		{
			int x=Q.front();
			Q.pop();
			int sz=G[x].size();
			for (int i=0;i<sz;i++)
			{
				Edge& e=edges[G[x][i]];
				if (!a[e.to]&&e.cap>e.flow)
				{
					pre[e.to]=G[x][i];
					a[e.to]=min(a[x],e.cap-e.flow);
					Q.push(e.to);
				}
			}
			if (a[t])
				break;
		}
		if (!a[t])
			break;
		for (int u=t;u!=s;u=edges[pre[u]].from)
		{
			edges[pre[u]].flow+=a[t];
			edges[pre[u]^1].flow-=a[t];
		}
		flow+=a[t];
	}
	return flow;
}

int main()
{
	int n,m,u,v,f;
	while(cin >> n >> m)
	{
		init(n);
		for (int i=0;i<n;i++)
		{
			scanf("%d%d%d",&u,&v,&f);
			addedge(u,v,f);
		}
		cout << Edmonds_Karp(1,m) << endl;
	}
	return 0;
}

方法二:Dinic

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
    int from,to,flow;
    Edge(int u,int v,int f):from(u),to(v),flow(f){}
};

vector<Edge> edges;
vector<int> G[maxn];

int dis[maxn];
int cur[maxn];

void init(int n)
{
    for (int i=0;i<n;i++)
        G[i].clear();
    edges.clear();
}


void addedge(int from,int to,int flow)
{
    edges.push_back(Edge(from,to,flow));
    edges.push_back(Edge(to,from,0));
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}

bool bfs(int s,int t)
{
    queue<int> Q;
    Q.push(s);
    memset(dis,-1,sizeof(dis));
    dis[s]=0;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        int sz=G[x].size();
        for (int i=0;i<sz;i++) 
        {
            Edge& e=edges[G[x][i]];
            if (e.flow>0) 
            {
                if (dis[e.to]<0) 
                {
                    dis[e.to]=dis[x]+1;
                    Q.push(e.to);
                }
            }
        }
    }
    return bool(~dis[t]);
}

int dfs(int s,int t,int maxflow) 
{
    if (s==t)
        return maxflow;
    int sz=int(G[s].size());
    for (int i=cur[s],num;num=G[s][i],i<sz;i++)
    {
        cur[s]=i;
        Edge &e=edges[num];
        if (dis[e.to]==dis[s]+1 && e.flow>0)
        {
            int flow=dfs(e.to,t,min(maxflow,e.flow));
            if (flow!=0)
            {
                e.flow-=flow;
                edges[num^1].flow+=flow;
                return flow;
            }
        }
    }
    return 0;
}

int Dinic(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        int flow;
        memset(cur,0,sizeof(cur));
        while((bool)(flow=dfs(s,t,INF)))
            ans+=flow;
    }
    return ans;
}

int main()
{
    int n,m,u,v,f;
    while(cin >> n >> m)
    {
        init(n);
        for (int i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&f);
            addedge(u,v,f);
        }
        cout << Dinic(1,m) << endl;
    }
    return 0;
}

方法三:SAP

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int maxm=maxn*maxn;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{  
    int v,w,next;  
}edge[maxm];

int dis[maxn],pre[maxn],rec[maxn],head[maxn],gap[maxn],now[maxn];  
int n,m,no,up;
queue<int> q;

void addedge(int u,int v,int w)
{
    edge[no].v=v; edge[no].w=w;
    edge[no].next=head[u]; head[u]=no++;
    edge[no].v=u; edge[no].w=0;
    edge[no].next=head[v]; head[v]=no++;
}

void pre_init()  
{  
    no=0; up=n;
    memset(head,-1,sizeof(head));  
}

void init(int s,int t)
{
    for(int i=0;i<=up;i++) 
    {
    	now[i]=head[i];
    	gap[i]=0;
        dis[i]=INF;
	}

    while(!q.empty())
        q.pop();

    dis[t]=0; q.push(t);
    while(!q.empty())
    {
        int tp=q.front();
        q.pop();
        gap[dis[tp]]++;
        int k=head[tp];
        while(k!=-1)
        {
            if(dis[edge[k].v]==INF && edge[k^1].w)  
            {
                dis[edge[k].v]=dis[tp]+1;  
                q.push(edge[k].v);  
            }
            k=edge[k].next;  
        }
    }
}

int SAP(int s,int t)  
{  
    int ans=0,flow=INF,top=s;  
    pre[s]=s;
    init(s,t);  
    while(dis[s]<up)   
    {  
        if(top==t)  
        {  
            ans+=flow;  
            while(top!=s)
            {  
                edge[rec[top]].w-=flow;  
                edge[rec[top]^1].w+=flow;  
                top=pre[top];  
            }
            flow=INF;  
        }  
        int k=now[top];  
        while(k!=-1)  
        {  
            int v=edge[k].v;  
            if(edge[k].w && dis[top]==dis[v]+1)  
            {  
                flow=min(flow, edge[k].w);  
                pre[v]=top; rec[v]=k;  
                now[top]=k;   
                top=v;  
                break;  
            }  
            k=edge[k].next;  
        }  
        if(k==-1)
        {  
            int mind=n;  
            if(--gap[dis[top]]==0) break;
            int k=now[top]=head[top];
            while(k!=-1)  
            {  
                if(edge[k].w && mind>dis[edge[k].v])
                    mind=dis[edge[k].v];  
                k=edge[k].next;  
            }  
            gap[dis[top]=mind+1]++;  
            top=pre[top];  
        }
    }
    return ans;  
}

int main()
{
    int u,v,f;
    while(cin >> n >> m)
    {
        pre_init();
        for (int i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&f);
            addedge(u,v,f);
        }
        cout << SAP(1,m) << endl;
    }
    return 0;
}

 

第5-5课:最大流问题(图文篇) 原计划这一课是要讲解最小费用最大流问题,但是图文课每一篇如果字数太多会给手机阅读带来困难,加上读者圈朋友的反馈希望多讲一些基础的内容,因此这一课我们降低点难度,只讲最大流问题。最小费用最大流问题,其实也是在最大流问题的基础上加入类似最短路径算法(SPFA)这样的思路,叠加形成算法,最后的总结部分我会简单介绍一下在最大流算法的基础上实现最小费用最大流问题的两种常用方法,但是这一课,我们只讲最大... 阅读详情

相关推荐

fpga学习——zynq图像处理中的DVP流接口封装

之前文章介绍了基于zynq的图像处理架构问题。其中,作为开发者,需要重点关注图像传感器接口、处理算法、显示接口,这些模块。现在我们一同学习用于视频数据接口的DVP模块,并将其封装成AXI-stream接口便于直接和VDMA IP通信。 DVP_AXI stream IP v1.0使用说明 1.设计概述 •用于cmos传感器视频数据采集,将cmos输出的8位视频数据拼接成RGB 565模式 •AXI_stream主机接口,用于和PS端内存的数据交互 •基于vivado 18.3软件设计 2.模块分析

weixin_41895751的博客 4588

网络流——最大流问题例题

P1231 教辅的组成 https://www.luogu.org/problemnew/show/P1231 这道题不难发现是网络流题,主要要想到怎么建图,然后跑遍网络流就ok了 我们先对练习册和书连一条边容量为1,然后书与答案连一条边容量为1,但是这样有问题,因为书的流量可能会很多,所以我们要把书拆成两堆,然后书一与书二之间连一条边容量为一,然后我们建一个超级源点,超级汇点,连起来,容量...

weixin_34064653的博客 2690

瑞芯微rv1126 rtsp+mpp+rga取流

瑞芯微rv1109 rtsp+mpp+rga取流

ZuoSeDiao的博客 6345

最大流模板题 HDU 1532

HDU 1532 题意:给你m条边(但向边),n个点,n个点的编号为1到n,问从1到n的最大流是多少。 最大流模板题,听说有重边,但是没管重边这个事也过了。 Dinic算法: #include<bits/stdc++.h> #define mem(a,b) memset((a),b,sizeof(a)) #define de cout<<endl<<e...

flyzer的博客 220

全国职业院校技能大赛-大数据应用赛项-数据挖掘-01

-----------------------推荐Top5结果如下--------------------------------------------第一条数据前10列结果展示为:----------------------------------------相同种类前10的id结果展示为:--------------------相似度top5(商品id:14,平均相似度:0.522356)相似度top1(商品id:1,平均相似度:0.983456)若属于该spu_id,则内容为1否则为0。

Eternity_04的博客 1961

数字加网(一)— —概念及分色合成

由于大多数图像复制设备是二值设备,因此在图像复制过程中,必须先把连续调的灰度图像或彩色图像处理成二值的半色调图像,用单位面积的网点覆盖率来表现图像的颜色深浅变化。这个过程被称之为加网(Screening) 数字加网就是根据人眼视觉的空间混色特性,用人眼无法分辨的细小黑白点的单位覆盖面积来模拟灰度图像或彩色图像的层次变化,使得到

Sin_Geek成长の迹 8598

hdu1532 sap模板

之前在做好多题 都用dicnic和E-K算法  结果连续T了好几天一道题也没过 直到知道还有SAP算法 就直接摘了kuangbin的模板 在网上找了好多的模板 发现还是kuangbin的最好用 用裸题hdu1532上代码 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory

Frozensmile的博客 520

hwasan实例分析踩内存之一:camx进程stack tag-mismatch的三种解决方案

hwasan实例分析踩内存之一:camx进程stack tag-mismatch的三种解决方案。

1308

【题解】Drainage Ditches HDU - 1532(最大流 模板题)⭐⭐

Drainage Ditches HDU - 1532 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 ...

Suprit's blog 630

VMware报错系列:虚拟机配置文件(.vmx)损坏或丢失---修复

虚拟机配置文件(.vmx)损坏或丢失---修复

weixin_54626591的博客 7021

海康威视 2024届 数字逻辑设计 实习笔试分析

记录一下 5月11日晚,做的海康威视的一场笔试。分享给需要的IC人。 岗位:数字逻辑设计工程师(浙江 杭州) 转载需要本人同意! 我的见解不一定都是准确的,欢迎评论区交流指正~~

记录所学,分享知识,结识挚友 1万+

HDU1532 - Drainage Ditches 网络流之最大流

HDU1532 - Drainage Ditches 网络流: 最大流

P_rogrammer的专栏 628

HQChart实战教程54:Renko砖形K线图实现详解

Renko图(砖形图)起源于日本,只关注价格变动而忽略时间和成交量因素: 2.2 算法说明 初始化:使用第一根K线的收盘价创建第一个砖块 变动检测:计算当前价格与最后一个砖块的差值 砖块生成: 当价格变动≥砖块大小时,生成新砖块 当价格变动不足时,更新当前砖块的高低价 方向判断:根据价格变动方向确定砖块颜色 三、完整实现代码 四、Renko图的高级应用 4.1 动态砖块大小调整 4.2 Renko交易策略 三砖反转策略:趋势跟踪策略:本文详细介绍了Renko图的实现原理和在HQChart中的应

QQ_778132974的博客 882

hdu 1532 Network Flow Drainage Ditches

Using the code solve max_flow problem.I just using the mould.I don't know the details of isap algorithm. ps:This is my first solve Network Flow problem. The portal : http://acm.hdu.edu.cn/showprob

NM的专栏 439

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

HDU1532(最大流EK算法模板题)

Drainage DitchesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17484 Accepted Submission(s): 8238Problem Description Every time it rains o

creatorx的博客 1685

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

最大流【EK算法】HDU 1532模板题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 定义: 网络:网络是一个有向带权图,包含一个源点和一个汇点,没有反向平行边。 网络流:网络流即网上的流,是定义在网络边集E上的一个非负函数flow={flow(u,v)}, flow(u,v)是边上的流量。 可行流:满足以下两个性质的网络流flow称为可行流。 容量约束:每条边的实际流...

小康的博客 466

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【最大流之EdmondsKarp算法】【HDU1532】模板题

题意:裸的最大流,什么是最大流,参考别的博客 运用复杂度最高的EK算法 O(M*N),模板来自紫书 #include #include #include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f #define oo 0x1313

学习之路 582

Drainage Ditches HDU - 1532

Drainage Ditches HDU - 1532   网络流模板题,用Dinic //Dinic #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<iomanip> #include<queue> usin...

qq_43560101的博客 180

HDU 1532 Drainage Ditches(最大流)

HDU 1532 Drainage Ditches 题目链接 最大流模板题 代码: #include #include #include #include using namespace std; const int MAXNODE = 205; const int MAXEDGE = 405; typedef int Type; const Type INF

Remilia's 666

Drainage Ditches (HDU - 1532)(最大流)

HDU - 1532 题意:有m个点,n条管道,问从1到m最大能够同时通过的水量是多少? 题解:最大流模板题。 #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;queue&gt; using namespace...

Mercury_Lc的博客 427

最大流

[HDU 1532] (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): 12521

而濡木染 521

HDU1532 Drainage Ditches 【最大流】

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9715    Accepted Submission(s): 4623 Problem Description Every tim

长风的专栏 1038

HDU 1532 Drainage Ditches【最大流 | Edmonds-Karp模板】

题目链接 题目描述: 最大流Edmonds-Karp算法模板题。 最大流 最大流问题是基于有向图的,就是求两点 (分别称为源点、汇点) 间的最大流量,图中任何点都可以作为它们的中转。在求最大流时需要满足以下 3 个性质: 流量守恒。从源点 s 流出的流量和到达汇点 t 的流量相等;其他所有中转点,流入和流出相等。 反对称性。设从 uuu 到 vvv 的流量是 f(u,v)f(u,v)f(u,v),vvv 到 uuu 流量是 f(v,u)f(v,u)f(v,u),那么 f(u,v)=−f(v,u)f(u

weixin_44258590的博客 254

算法模板之Dinic最大流(HDU 1532 Drainage Ditches)

模板总结归纳://src 源点, sink汇点, g、e存边的邻接表 const int inf = 1000000000; //1e9 const int maxn = 20000, maxm = 500000; struct Egde { int v, f, nxt; }; int n, src, sink; int g[maxn + 10]; int nume; struct Ed...

摸不着脑子的博客 262

HDU_1532 && HDU_3549(最大流EK算法模板)

题意:从1开始到终点n,最多能通过多少的流量,就比如说1到2的流量是20,2到3的流量是10,那么到达终点3的流量为10,而1到2还能经过10的流量,2到3不能再通过。 分析:完全是照刘汝佳的紫书模板写的,EdmondsKarp算法,简称EK算法。

一点也不二 1203
上一篇: Sublime写cpp时无法定位程序输入点__gxx_personality_v0的问题
下一篇: 一次失败的做题经历(HDU2112)
RadiumYang
博客等级 码龄9年 68粉丝 152原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值