zoukankan      html  css  js  c++  java
  • HDU 3549 Flow Problem(最大流)

    Flow Problem



    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
    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1
     
     
    Sample Output
    Case 1: 1
    Case 2: 2
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 #include<vector>
     6 using namespace std;
     7 
     8 struct Edge
     9 {
    10     int from,to,cap,flow;
    11 };
    12 
    13 const int inf=0x3f3f3f;
    14 vector<Edge>edges;
    15 vector<int>G[16];
    16 int a[16],p[16];
    17 int m,n;
    18 
    19 void init()
    20 {
    21     for(int i=1;i<=n;i++)G[i].clear();
    22     edges.clear();
    23     memset(p,0,sizeof(p));
    24 }
    25 
    26 void addedge(int from,int to,int cap)
    27 {
    28     edges.push_back((Edge){from,to,cap,0});
    29     edges.push_back((Edge){to,from,0,0});
    30     int m=edges.size();
    31     G[from].push_back(m-2);
    32     G[to].push_back(m-1);
    33 }
    34 
    35 int maxflow(int s,int t)
    36 {
    37     int flow=0;
    38     while(1)
    39     {
    40         memset(a,0,sizeof(a));
    41         queue<int>q;
    42         q.push(s);
    43         a[s]=inf;
    44         while(!q.empty())
    45         {
    46             int x=q.front();
    47             q.pop();
    48             for(int i=0;i<G[x].size();i++)
    49             {
    50                 Edge& e=edges[G[x][i]];
    51                 if(!a[e.to]&&e.cap>e.flow)
    52                 {
    53                     p[e.to]=G[x][i];
    54                     a[e.to]=min(a[x],e.cap-e.flow);
    55                     q.push(e.to);
    56                 }
    57             }
    58             if(a[t])break;
    59         }
    60         if(!a[t])break;
    61         for(int u=t;u!=s;u=edges[p[u]].from)
    62         {
    63             edges[p[u]].flow+=a[t];
    64             edges[p[u]^1].flow-=a[t];
    65         }
    66         flow+=a[t];
    67     }
    68     return flow;
    69 }
    70 
    71 int main()
    72 {
    73     int T;
    74     scanf("%d",&T);
    75     for(int t=1;t<=T;t++)
    76     {
    77         init();
    78         printf("Case %d: ",t);
    79         scanf("%d%d",&n,&m);
    80         for(int i=0;i<m;i++)
    81         {
    82             int u,v,w;
    83             scanf("%d%d%d",&u,&v,&w);
    84             addedge(u,v,w);
    85         }
    86         int ans=maxflow(1,n);
    87         printf("%d
    ",ans);
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    sql_mode=only_full_group_by引起group by查询报错问题
    在Eclipse下使用ant,build项目报jpeg does not exist
    JAVAEE面试之Hibernate缓存
    页面加载中效果实现
    鼠标滑过显示图片
    VSFTP配置参数详解
    FTP上传核心方法
    java对File的特殊操作
    在mybatis下使用日期比对出错
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/homura/p/4868263.html
Copyright © 2011-2022 走看看