hdu 3549 Flow Problem 网络流

时间:2023-04-25 20:18:20

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

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.
题意:不能再简单了这题意,很裸的告诉你根据输入来求解最大流。
解法:网络流算法求解最大流,模板题,Dinic。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=; int n,m;
int graph[maxn][maxn],d[maxn]; int bfs()
{
memset(d,,sizeof(d));
d[]=;
queue<int> Q;
Q.push();
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int v= ;v<=n ;v++)
{
if (!d[v] && graph[u][v]>)
{
d[v]=d[u]+;
Q.push(v);
if (v==n) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==n || flow==) return flow;
int cap=flow;
for (int v= ;v<=n ;v++)
{
if (d[v]==d[u]+ && graph[u][v]>)
{
int x=dfs(v,min(cap,graph[u][v]));
cap -= x;
graph[u][v] -= x;
graph[v][u] += x;
if (cap==) return flow;
}
}
return flow-cap;
} int Dinic()
{
int sum=;
while (bfs()) sum += dfs(,inf);
return sum;
} int main()
{
int t,ncase=;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
int a,b,c;
memset(graph,,sizeof(graph));
for (int i= ;i<m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
graph[a][b] += c;
}
printf("Case %d: %d\n",ncase++,Dinic());
}
return ;
}