您的位置:首页 > 其它

HDU 3549 Flow Problem(最大流)

2016-06-10 15:45 281 查看

Flow Problem

Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 12625 Accepted Submission(s): 6004



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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)

[align=left]Output[/align]
For each test cases, you should output the maximum flow from source 1 to sink N.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

Case 1: 1
Case 2: 2


[align=left]Author[/align]
HyperHexagon

[align=left]Source[/align]
HyperHexagon's
Summer Gift (Original tasks)

题意:n个点,m条有向边,每条边有一个容量c;求1到n的最大流。

分析:这是一题很基础,很经典的最大流问题,算的上是PFS算法的模板题。

Ps:网络流学习

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9+7;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define N 1005

int pre
;///保存增光路经上的点的前驱顶点
int mat

;///残留网络容量
bool vis
;
int s,t;
int n,m;

bool bfs()
{
int cur;
queue<int> Q;
CL(vis, 0);
CL(pre, 0);
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
cur = Q.front();
Q.pop();
if(cur == t) return true;///如果已经到达t,表示已经找到一条增光路经,返回
for(int i=1; i<=n; i++)
{
if(!vis[i] && mat[cur][i])///只有残余容量大于0时才存在边
{
Q.push(i);
pre[i] = cur;
vis[i] = true;
}
}
}
return false;
}

int max_flow()
{
int ans = 0;
while(1)
{
if(!bfs()) return ans;///找不到增光路经表示已经是最大流,返回
int Min = INF;
for(int i=t; i!=s; i=pre[i])///通过pre[]数组查找增光路经上的边,求出残余容量的最小值
Min = min(Min, mat[pre[i]][i]);
for(int i=t; i!=s; i=pre[i])
{
mat[pre[i]][i] -= Min;
mat[i][pre[i]] += Min;
}
ans += Min;
}
}

int main()
{
int T,cas=1;
int u,v,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
s = 1;
t = n;
CL(mat, 0);
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
mat[u][v] += c;
}
printf("Case %d: %d\n",cas++,max_flow());
}
return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: