首页 > 代码库 > hdu3549 Flow Problem(裸最大流)
hdu3549 Flow Problem(裸最大流)
Flow Problem
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status
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)
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: 1Case 2: 2
题目比较简单,入门级最大流问题。
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string.h>
#define MAXN 21
#define INF 1<<28
using namespace std;
int cap[MAXN][MAXN], pre[MAXN], vis[MAXN], maxflow[MAXN];
int Maxflow;
int n, m, i, j;
void init()
{
int x, y, z;
memset(cap, 0, sizeof(cap));
memset(pre, 0, sizeof(pre));
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &z);
cap[x][y] += z;//重边
}
}
int min(int a, int b)
{
return a>b?b:a;
}
int main()
{
int T, u, k=1;
scanf("%d", &T);
while(T--)
{
init();
queue<int>q;
Maxflow = 0;
while(1)
{
memset(maxflow, 0, sizeof(maxflow));
memset(vis, 0, sizeof(vis));
maxflow[1] = INF;
q.push(1);
while(!q.empty())
{
u = q.front();
q.pop();
for(int v=1; v<=n; v++)
{
if(!vis[v] && cap[u][v]>0)
{
vis[v] = 1;
pre[v] = u;
q.push(v);
maxflow[v] = maxflow[u]<cap[u][v]?maxflow[u]:cap[u][v];
}
}
}
if(maxflow[n]==0) break;
for(int i=n; i!=1; i=pre[i])
{
cap[pre[i]][i]-=maxflow[n];
cap[i][pre[i]]+=maxflow[n];
}
Maxflow += maxflow[n];
}
printf("Case %d: %d\n", k++, Maxflow);
}
return 0;
}
#include<algorithm>
#include<queue>
#include<string.h>
#define MAXN 21
#define INF 1<<28
using namespace std;
int cap[MAXN][MAXN], pre[MAXN], vis[MAXN], maxflow[MAXN];
int Maxflow;
int n, m, i, j;
void init()
{
int x, y, z;
memset(cap, 0, sizeof(cap));
memset(pre, 0, sizeof(pre));
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &z);
cap[x][y] += z;//重边
}
}
int min(int a, int b)
{
return a>b?b:a;
}
int main()
{
int T, u, k=1;
scanf("%d", &T);
while(T--)
{
init();
queue<int>q;
Maxflow = 0;
while(1)
{
memset(maxflow, 0, sizeof(maxflow));
memset(vis, 0, sizeof(vis));
maxflow[1] = INF;
q.push(1);
while(!q.empty())
{
u = q.front();
q.pop();
for(int v=1; v<=n; v++)
{
if(!vis[v] && cap[u][v]>0)
{
vis[v] = 1;
pre[v] = u;
q.push(v);
maxflow[v] = maxflow[u]<cap[u][v]?maxflow[u]:cap[u][v];
}
}
}
if(maxflow[n]==0) break;
for(int i=n; i!=1; i=pre[i])
{
cap[pre[i]][i]-=maxflow[n];
cap[i][pre[i]]+=maxflow[n];
}
Maxflow += maxflow[n];
}
printf("Case %d: %d\n", k++, Maxflow);
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。