HDU 3549(網路流入門之最大流),hdu3549

來源:互聯網
上載者:User

HDU 3549(網路流入門之最大流),hdu3549
Flow ProblemTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 10327    Accepted Submission(s): 4866


Problem DescriptionNetwork 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. 
InputThe 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) 
OutputFor 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
 
AuthorHyperHexagon 
SourceHyperHexagon's Summer Gift (Original tasks)

 


n個點m條邊,找到1到n使得流量最大。

解題思路;找出所有的增廣路徑,然後再找增廣路徑上的邊,找出最小的流量!!

#include <cstdio>#include <cmath>#include <queue>#include <iostream>#include <algorithm>#include <cstring>using namespace std;int n,m;int map1[1001][1001];int pre[1001];int vis[1001];int s,t;bool BFS(){    int i,cur;    queue<int>q;    memset(pre,0,sizeof(pre));    memset(vis,0,sizeof(vis));    vis[s]=1;    q.push(s);    while(!q.empty())    {        cur=q.front();        q.pop();        if(cur==t)            return 1;        for(i=1; i<=n; i++)        {            if(!vis[i] &&map1[cur][i])            {                q.push(i);                pre[i]=cur;                vis[i]=1;            }        }    }    return 0;}//找增廣路徑int Max_flow(){    int i,ans=0;    while(1)    {        if(!BFS())            return ans;        int Min=0x7f7f7f7f;        for(i=t; i!=s; i=pre[i])            Min=min(Min,map1[pre[i]][i]);        for(i=t; i!=s; i=pre[i])        {            map1[pre[i]][i]-=Min;            map1[i][pre[i]]+=Min;        }        ans+=Min;    }}//找增廣路徑上的最小流量int main(){    int T;    cin>>T;    int v,u,c;    int k=1;    while(T--)    {        scanf("%d%d",&n,&m);        memset(map1,0,sizeof(map1));        s=1,t=n;        while(m--)        {            scanf("%d%d%d",&u,&v,&c);            map1[u][v]+=c;        }        printf("Case %d: %d\n",k++,Max_flow());    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.