hdu 3549 Flow Problem最大流(裸)

来源:互联网 发布:小鱼儿主页域名 编辑:程序博客网 时间:2024/05/08 17:40
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
23 21 2 12 3 13 31 2 12 3 11 3 1

Sample Output
Case 1: 1Case 2: 2
 

首先了解一下残余网络,增广路就是残余网络中的s-t路径,通过不断地找到图中的增广路来增加原本有的流量,直到图中的增广路不存在,此时的流量和就是最大流量

way1 :Ford_Fulkerson算法
#pragma comment(linker,"/STACK:1024000000,1024000000")#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>using namespace std;struct edge{    int b,c,rev;    edge(int b1,int c1,int rev1):b(b1),c(c1),rev(rev1) {}};const int inf=10000001;bool used[1000];vector<edge> G[1000];int dfs(int v,int t,int f){//寻找一条增广路,更新    if(v==t) return f;    used[v]=true;    for(int i=0; i<G[v].size(); i++)    {        edge& e=G[v][i];        if(!used[e.b] && e.c>0)        {            int d=dfs(e.b,t,min(f,e.c));            if(d>0)            {                e.c-=d;                G[e.b][e.rev].c+=d;                return d;            }        }    }    return 0;}int answer(int s,int t) //源点s,汇点t{    int flw=0;    while(1)    {        memset(used,false,sizeof(used));        int f= dfs(s,t,inf);        if(f==0) return flw;        flw+=f;    }}int main(){    int T,n,t,o=1;    cin>>T;    while(T--)    {        cin>>n>>t;        for(int i=1; i<=n; i++)            G[i].clear();        int a,b,c;        for(int i=1; i<=t; i++)        {            scanf("%d%d%d",&a,&b,&c);            G[a].push_back(edge(b,c,G[b].size())); //正向            G[b].push_back(edge(a,0,G[a].size()-1));//逆向边,流量初始为0        }        int f=answer(1,n);        printf("Case %d: %d\n",o++,f);    }}


0 0
原创粉丝点击