HDU-1532 Drainage Ditches (最大流,EK算法模板)

题目大意:最大流的模板题。。。源点是0,汇点是n-1。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<string>
# include<vector>
# include<list>
# include<set>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;

# define LL long long
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b)

const double inf=1e30;
const int INF=1<<30;
const int N=205;

struct Edge
{
	int fr,to,c,f;
	Edge(int _fr,int _to,int _c,int _f):fr(_fr),to(_to),c(_c),f(_f){}
};

int n,m;
vector<Edge>e;
vector<int>G[N*2];
int a[N*2],p[N*2];

void init()
{
	REP(i,0,n) G[i].clear();
	e.clear();
}

void addEdge(int fr,int to,int cap)
{
	e.push_back(Edge(fr,to,cap,0));
	e.push_back(Edge(to,fr,0,0));
	int len=e.size();
	G[fr].push_back(len-2);
	G[to].push_back(len-1);
}

int maxFlow()
{
	int flow=0;
	while(1)
	{
		CL(a,0);
		queue<int>q;
		q.push(0);
		a[0]=INF;
		while(!q.empty())
		{
			int x=q.front();
			q.pop();
			REP(i,0,G[x].size()){
				Edge &edge=e[G[x][i]];
				if(!a[edge.to]&&edge.c>edge.f){
					p[edge.to]=G[x][i];
					a[edge.to]=min(a[x],edge.c-edge.f);
					q.push(edge.to);
				}
			}
			if(a[n-1]) break;
		}
		if(!a[n-1]) break;
		for(int u=n-1;u;u=e[p[u]].fr){
			e[p[u]].f+=a[n-1];
			e[p[u]^1].f-=a[n-1];
		}
		flow+=a[n-1];
	}
	return flow;
}

int main()
{
	int a,b,c;
	while(~scanf("%d%d",&m,&n))
	{
	    init();
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			addEdge(a-1,b-1,c);
		}
		printf("%d\n",maxFlow());
	}
	return 0;
}

  

时间: 2024-12-25 07:44:19

HDU-1532 Drainage Ditches (最大流,EK算法模板)的相关文章

HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches 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 drai

hdu - 1532 Drainage Ditches (最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=1532 求最大的流量,用dinic算法就好. 1 // Rujia Liu 2 // 因为图较大,所以采用Dinic而不是EdmondsKarp 3 // 得益于接口一致性,读者无须理解Dinic就能使用它. 4 #include<cstdio> 5 #include<cstring> 6 #include<queue> 7 #include<algorithm> 8 us

HDU 1532 Drainage Ditches 最大排水量 网络最大流 Edmonds_Karp算法

题目链接:HDU 1532 Drainage Ditches 最大排水量 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9641    Accepted Submission(s): 4577 Problem Description Every time it rains on Farmer John

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

HDU 1532 Drainage Ditches (网络流)

A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 wate

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10875    Accepted Submission(s): 5131 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

HDU 1532 Drainage Ditches (最大网络流)

Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Every time it rains on

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

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

hdu 1532 Drainage Ditches(edmond-karp最大流算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 #include <stdio.h> #include <queue> #include <string.h> #include <algorithm> #define INT_MAX (int)1e9 using namespace std; const int N = 207; int network[N][N], pre[N], used[N], f