HDU3549 Flow Problem【网络流 最大流】

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 23036    Accepted Submission(s): 10756


 

Problem Description

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

Input

The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input

2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

Sample Output

Case 1: 1
Case 2: 2

Author

HyperHexagon

 问题链接:HDU3549 Flow Problem

问题描述:给你一个N个顶点M条边的有向图,要你求1号点到N号点的最大流。

解题思路:最大流模板题,使用EdmondsKarp算法(邻接矩阵形式)

Source

HyperHexagon's Summer Gift (Original tasks)

AC的C++程序:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int N=20;
const int INF=0x3f3f3f3f;

int g[N][N];
int pre[N];
bool vis[N];

int n,m;

int EdmondsKarp()
{
	queue<int>q;
	memset(pre,-1,sizeof(pre));
	memset(vis,false,sizeof(vis));
	vis[1]=true;
	pre[1]=0;
	q.push(1);
	int v;
	bool flag=false;
	while(!q.empty())
	{
		v=q.front();
		q.pop();
		for(int i=1;i<=n;i++)
		  if(g[v][i]>0&&!vis[i])
		  {
			  pre[i]=v;		  	
			  vis[i]=true;
		  	  if(i==n)
		  	  {
		  	      flag=true;
		  	      break;
			  }
			  else
			    q.push(i);
		  }
	}
	if(!flag)//找不到就返回 
	  return 0;
	int ans=INF;
	for(v=n;v!=1;v=pre[v])
	  ans=min(ans,g[pre[v]][v]);
	for(v=n;v!=1;v=pre[v])
	{
		g[pre[v]][v]-=ans;
		g[v][pre[v]]+=ans;
	} 
	return ans;
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int t=1;t<=T;t++)
	{
		scanf("%d%d",&n,&m);
		int a,b,c;
		memset(g,0,sizeof(g));
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			g[a][b]+=c;
		}
		int ans=0,temp;
		while(temp=EdmondsKarp())
		  ans+=temp;
		printf("Case %d: %d\n",t,ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/84722360