HDU ACM 1532 Drainage Ditches->网络最大流模版题(EK算法实践)

分析:最大流的入门题,BFS寻找增广路,找到则更新流量,直到找不到增广路为止。

#include<iostream>
#include<queue>
using namespace std;

#define N 202
#define MAX 0x7fffffff;

class Max_Flow_EK
{
public:
	Max_Flow_EK(){}
	~Max_Flow_EK(){}
	int Run(int n,int m);

private:
	void Init();
	void Read(int n);
	int MaxFlow(int s,int d,int m);
	int Bfs(int s,int d,int m);

	int m_iCap[N][N];   //记录残留网络的流量
    int m_iFlow[N];     //从源点到当前节点还有当前流量可用
    int m_iPre[N];      //增广路中节点的前驱
	queue<int> m_q;
};

int Max_Flow_EK::Run(int n,int m)
{
	Init();
	Read(n);
	return MaxFlow(1,m,m);
}

int Max_Flow_EK::Bfs(int s,int d,int m)
{
	int i,index;

	while(!m_q.empty()) m_q.pop();  //清空队列
	for(i=0;i<=m;i++)               //初始化前驱
		m_iPre[i]=-1;

	m_iPre[s]=0;
	m_iFlow[s]=MAX;
	m_q.push(s);
	while(!m_q.empty())
	{
		index=m_q.front();  //取出队头节点
		m_q.pop();

		if(index==d)       //找到了增光路
			break;
		for(i=1;i<=m;i++)
			if(i!=s && m_iCap[index][i]>0 && m_iPre[i]==-1)
			{
				m_iPre[i]=index;
				m_iFlow[i]=m_iFlow[index]<m_iCap[index][i]?m_iFlow[index]:m_iCap[index][i];
				m_q.push(i);
			}
	}
	if(m_iPre[d]==-1) return -1;
	else
		return m_iFlow[d];
}

int Max_Flow_EK::MaxFlow(int s,int d,int m)
{
	int increasement;
	int sumflow;
	int k,last;

	sumflow=0;
	while((increasement=Bfs(s,d,m))!=-1)
	{
		k=d;
		while(k!=s)
		{
			last=m_iPre[k];
			m_iCap[last][k]-=increasement;  //改变正向边容量
			m_iCap[k][last]+=increasement;  //改变反向边容量
			k=last;
		}
		sumflow+=increasement;
	}
	return sumflow;
}

void Max_Flow_EK::Init()
{
	memset(m_iCap,0,sizeof(m_iCap));
	memset(m_iFlow,0,sizeof(m_iFlow));
}

void Max_Flow_EK::Read(int n)
{
	int i;
	int Si,Ei,Ci;

	for(i=0;i<n;i++)
	{
		scanf("%d %d %d",&Si,&Ei,&Ci);

		if(Si==Ei) continue;      //起点可能与终点相同
		m_iCap[Si][Ei]+=Ci;       //考虑重复输入
	}
}

int main()
{
	int n,m;
	Max_Flow_EK max_flow;

	while(scanf("%d %d",&n,&m)==2)
	{
		printf("%d\n",max_flow.Run(n,m));
	}
    return 0;
}
时间: 2024-11-17 11:56:18

HDU ACM 1532 Drainage Ditches->网络最大流模版题(EK算法实践)的相关文章

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

HDU ACM 3572 Task Schedule 网络最大流-&gt;dinic算法

分析: 建图:每个任务和每一天分别看做一个点,添加源和汇点.源点和每个任务连一条边,每天边的容量为完成对应任务所需处理次数.若第i个任务能够在Si至Ei天处理,则由该任务向这些天分别连一条边,容量为1,表示此任务每天只能被处理一次.最后,每一天分别连一条边到汇点,容量为机器数M,即每天可以处理M个任务.若求出的最大流等于所有任务需要处理的次数之和,说明能完成任务:否则,不能. #include<iostream> #include<vector> #include<queue

POJ 1273 Drainage Ditches【最大流模版】

题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include <cstdio> 5 #include <queue> 6 using namespace std; 7 const int N =

HDU ACM 1269迷宫城堡-&gt;有向强连通分量(tarjan算法实践)

分析:本题判断一个图是否是有向强连通分量,这里使用tarjan算法求解. #include<iostream> #include<vector> #include<stack> using namespace std; vector<int> map[10002]; stack<int> tarjan_stack; int low[10002]; int dfn[10002]; bool vis[10002]; int cnt,pos; void

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 1532||POJ1273:Drainage Ditches(最大流)

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

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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

Drainage Ditches(最大流基础_增广路算法)

 Drainage DitchesCrawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover p

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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