hdu 3549 Flow Problem

来源:互联网 发布:数据挖掘的技术基础 编辑:程序博客网 时间:2024/06/05 06:54
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 

【题意】还是最大流的裸题,和poj1273就是一个题,输入稍稍修改一下就行。不多说,写不出来单纯就是不懂最大流的模板,看看就行,上代码。

【代码】

#include<bits/stdc++.h>#define LL long long#define inf 1<<29#define s(a) scanf("%d",&a)#define CL(a,b) memset(a,b,sizeof(a))using namespace std;const int N=205;int n,m,u,v,val;int mat[N][N],path[N],flow[N];int start,endx;queue<int>q;int bfs(){    while(!q.empty()) q.pop();    CL(path,-1);    path[start]=0,flow[start]=inf;    q.push(start);    while(!q.empty())    {        int t=q.front();        q.pop();        if(t==endx) break;        for(int i=1; i<=m; i++)        {            if(i!=start&&path[i]==-1&&mat[t][i])            {                flow[i]=flow[t]<mat[t][i]?flow[t]:mat[t][i];                q.push(i);                path[i]=t;            }        }    }    if(path[endx]==-1) return -1;    return flow[endx];}int ans(){    int max_flow=0,step,now,pre;    while((step=bfs())!=-1)    {        max_flow+=step;        now=endx;        while(now!=start)        {            pre=path[now];            mat[pre][now]-=step;            mat[now][pre]+=step;            now=pre;        }    }    return max_flow;}int main(){//    freopen("in.txt","r",stdin);    int T;    scanf("%d",&T);    for(int z=1; z<=T; z++)    {        printf("Case %d: ",z);        scanf("%d%d",&m,&n);        CL(mat,0);        for(int i=0; i<n; i++)        {            scanf("%d%d%d",&u,&v,&val);            mat[u][v]+=val;        }        start=1,endx=m;        printf("%d\n",ans());    }    return 0;}
原创粉丝点击