• 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 
    

  • 相关阅读:
    MyBatis Generator 插件 详解自动生成代码
    [iOS开发-MVC初识]
    Java 基于 SpringBoot 的在线学习平台
    Network(四)NAT实现方式与VRRP概述
    生成扩散模型漫谈:最优扩散方差估计(上)
    VCS工具学习笔记(7)
    Docker之DockerFile解析
    学习编程的第十九天
    个人推荐讲的非常好的数据结构免费[速成 速成 速成]视频了
    小米root以及面具的使用
  • 原文地址:https://blog.csdn.net/m0_62089210/article/details/127852799