zoukankan      html  css  js  c++  java
  • hdu 3549 Flow Problem(增广路算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549

    模板题,白书上的代码。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int INF=1<<28;
     9 int cap[30][30],flow[30][30],n;
    10 
    11 int Edmonds_Karp(int s,int t)
    12 {
    13     int a[30],p[30];
    14     int f;
    15     queue<int>q;
    16     memset(flow,0,sizeof(flow));
    17     f=0;
    18     while(1)
    19     {
    20         memset(a,0,sizeof(a));
    21         a[s]=INF;
    22         q.push(s);
    23         while(!q.empty())     //bfs找增广路
    24         {
    25             int u=q.front();
    26             q.pop();
    27             for(int v=1; v<=n; v++)
    28             if(!a[v]&&cap[u][v]>flow[u][v])  //找到新节点v
    29             {
    30                 p[v]=u;   q.push(v);         //记录v的父亲,并加入FIFO队列
    31                 a[v]=min(a[u],cap[u][v]-flow[u][v]);  //s-v路径上的最小残量
    32             }
    33         }
    34         if(a[t]==0) break;         //找不到,则当前流已经是最大流
    35         for(int u=t; u!=s; u=p[u])  //从汇点往回走
    36         {
    37             flow[p[u]][u]+=a[t];     //更新正向流量
    38             flow[u][p[u]]-=a[t];     //更新反向流量
    39         }
    40         f+=a[t];                    //更新从s流出的总流量
    41     }
    42     return f;
    43 }
    44 int main()
    45 {
    46     int t,m,x=1;
    47     int u,v,w;
    48     scanf("%d",&t);
    49     while(t--)
    50     {
    51         memset(cap,0,sizeof(cap));
    52         scanf("%d%d",&n,&m);
    53         while(m--)
    54         {
    55             scanf("%d%d%d",&u,&v,&w);
    56             cap[u][v]+=w; //考虑重边
    57         }
    58 
    59         printf("Case %d: ",x++);
    60         printf("%d
    ",Edmonds_Karp(1,n));
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    LInux设备驱动分析—— kmalloc和kzalloc函数
    g++使用总结
    Redis那些事(一) — Redis简介
    C++后台知识点总结(一)
    秋招复习-C++(三)
    C++之类成员的访问权限详解(一)
    c++内联函数解析(inline)
    论文图片
    markdown Typora学习随笔
    ROS 导航设计随笔
  • 原文地址:https://www.cnblogs.com/bfshm/p/3544999.html
Copyright © 2011-2022 走看看