• 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
     2 #include
     3 #include
     4 #include
     5 #include
     6 using namespace std;
     7 
     8 struct Edge
     9 {
    10     int from,to,cap,flow;
    11 };
    12 
    13 const int inf=0x3f3f3f;
    14 vectoredges;
    15 vectorG[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         queueq;
    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;ie.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 
    

  • 相关阅读:
    解决splice改变原数组的BUG
    算法专题-单调栈
    基于 ceph-deploy 部署 Ceph 集群
    满足新能源三电系统气密和电性能测试的E10系列多功能电连接器
    从源码全面解析 Java SPI 的来龙去脉
    LVS-nat模式部署
    安装ZooKeeper集群
    Python之VScode基本开发环境
    开源不止,创新澎湃 | 2023开源产业生态大会六大专题抢“鲜”看!
    day41
  • 原文地址:https://blog.csdn.net/m0_62089210/article/details/127852799