• POJ1273 网络流-->最大流-->模板级别-->最大流常用算法总结


    一般预流推进算法:

    算法思想:

            对容量网络G 的一个预流f,如果存在活跃顶点,则说明该预流不是可行流。

            预流推进算法就是要选择活跃顶点,并通过它把一定的流量推进到它的邻接顶点,尽可能将正的赢余减少为0。

           由于算法最终目的是尽可能将流量推进到汇点Vt,因此算法总是首先寻求将流量推进到距离汇点Vt 最近的邻接顶点中。

           由于每个顶点的距离标号可以表示顶点到汇点Vt 的距离,因此算法总是将流量沿着允许弧推进。

           如果从当前活跃顶点出发没有允许弧,则增加该顶点的距离标号,使得从当前活跃顶点出发至少有一条允许弧。

    算法实现基本框架:

    (1) (预处理)取零流作为初始可行流,即f = { 0 },对源点Vs 发出的每条<Vs, u>,令f(Vs, u)= c(Vs, u);对任意的顶点v∈V,计算精确的距离标号d(v);令d(Vs) = n;

    (2) 如果残留网络G'(V', E')中不存在活跃顶点,则算法结束,已经求得最大流,否则进入第(3)步;

    (3) 在残留网络中选取活跃顶点u;如果存在顶点u 的某条出弧<u, v>为允许弧,则将min{ e(u),c'(u, v) }流量的流从顶点u 推进到顶点v;

          否则令d(u) = min{ d(v)+1 | <u, v>∈E',且c'(u, v)>0}。转第(2)步。c'(u, v)为残留网络中弧<u, v>的容量。

    名词解释:

    赢余(excess):设u 是容量网络G(V, E)中的顶点,定义顶点u 的赢余为流入顶点u 的流量之和减去从顶点u 流出的流量之和,记为e(u)。

    活跃顶点(active vertex):容量网络G 中,e(u) > 0 的顶点u(u≠Vs、Vt)称为活跃顶点。

    预流(preflow):设f = { f(u, v) }是容量网络G(V, E)上的一个网络流,如果G 的每一条弧<u, v>都满足:0 ≤ f(u, v) ≤ c(u, v),<u, v>∈E。

                                     另外,除源点Vs、汇点Vt 外每个顶点u的赢余e(u)都满足:e(u) ≥ 0,u≠Vs、Vt,则称该网络流f 为G 的预流。

    算法实现:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. #include <bits/stdc++.h>//poj不支持超级头文件  
    2. using namespace std;  
    3. #define INF 0x3f3f3f3f  
    4. const int maxn = 222;  
    5.   
    6. struct Push_Relablel//封装一般预流推进算法的结构体,时间复杂度O(n*n*m);  
    7. {  
    8.     int res[maxn][maxn];//残留网络  
    9.     int dist[maxn];//距离函数  
    10.     int e[maxn];//赢余  
    11.     int n;//结点数  
    12.     int max_flow;//最大流  
    13.   
    14.     void init(int x)  
    15.     {  
    16.         n = x;  
    17.         memset(res, 0, sizeof(res));  
    18.         memset(dist, 0, sizeof(dist));  
    19.         memset(e, 0, sizeof(e));  
    20.     }  
    21.   
    22.     void add(int u, int v, int c)  
    23.     {  
    24.         res[u][v] += c;  
    25.     }  
    26.   
    27.     int push_relablel(int s, int t)  
    28.     {  
    29.         max_flow = 0;  
    30.         queue<int> Q;  
    31.         Q.push(s);  
    32.         e[s] = INF; e[t] = INF;  
    33.         dist[s] = n;  
    34.         while(!Q.empty())  
    35.         {  
    36.             int u = Q.front(); Q.pop();  
    37.             for(int i = 1; i <= n; ++i)  
    38.             {  
    39.                 int p = res[u][i] < e[u] ? res[u][i] : e[u];  
    40.                 if(p>0 && (u == s || dist[u] == dist[i] + 1))  
    41.                 {  
    42.                     res[u][i] -=p;  
    43.                     res[i][u] +=p;  
    44.                     if(i == t) max_flow += p;  
    45.                     e[u] -= p;  
    46.                     e[i] += p;  
    47.                     if(i!=s && i!=t) Q.push(i);  
    48.                 }  
    49.             }  
    50.             if(u!=s && u!=t && e[u] > 0)  
    51.             {  
    52.                 dist[u]++;  
    53.                 Q.push(u);  
    54.             }  
    55.         }  
    56.         return max_flow;  
    57.     }  
    58. } G;  
    59.   
    60. int main()  
    61. {  
    62.     int m, n, u, v, c;  
    63.     while(~scanf("%d%d", &m, &n))  
    64.     {  
    65.         G.init(n);  
    66.         while(m--)  
    67.         {  
    68.             scanf("%d%d%d", &u, &v, &c);  
    69.             G.add(u, v, c);  
    70.         }  
    71.         cout<<G.push_relablel(1, n)<<endl;  
    72.     }  
    73.     return 0;  
    74. }  


    最高标号预流推进算法:

    算法思想:

            一般预流推进算法的缺陷在于非饱和推进。

             从具有最大距离标号的活跃节点开始预流推进,使得距离标号较小的活跃顶点累积尽可能多的来自距离标号较大的活跃顶点的流量,然后对累积的盈余进行推进,可能会减少非饱和推进的次数。

             用优先队列优化即可。

    算法实现:

    [cpp] view plain copy
    1. #include <bits/stdc++.h>  
    2. using namespace std;  
    3. #define INF 0x3f3f3f3f  
    4. const int maxn = 222;  
    5.   
    6. struct List  
    7. {  
    8.     int x, dist;  
    9.     List(int x_, int dist_): x(x_), dist(dist_){}  
    10.     bool friend operator < (const List& a, const List& b){  
    11.         return a.dist < b.dist;  
    12.     }  
    13. };  
    14.   
    15. struct Push_Relablel         //最高标号预流推进算法,时间复杂度O(n * n * sqrt(m))  
    16. {  
    17.     int res[maxn][maxn];  
    18.     int dist[maxn];  
    19.     int e[maxn];  
    20.     int n;  
    21.     int max_flow;  
    22.   
    23.     void init(int x)  
    24.     {  
    25.         n = x;  
    26.         memset(res, 0, sizeof(res));  
    27.         memset(dist, 0, sizeof(dist));  
    28.         memset(e, 0, sizeof(e));  
    29.     }  
    30.   
    31.     void add(int u, int v, int c)  
    32.     {  
    33.         res[u][v] += c;  
    34.     }  
    35.   
    36.     int push_relablel(int s, int t)  
    37.     {  
    38.         max_flow = 0;  
    39.         dist[s] = n;  
    40.         e[s] = INF; e[t] = INF;  
    41.         priority_queue<List> Q;  
    42.         Q.push(List(s, dist[s]));  
    43.   
    44.         while(!Q.empty())  
    45.         {  
    46.             List q = Q.top(); Q.pop();  
    47.             int u = q.x;  
    48.             for(int i = 1; i <= n; ++i)  
    49.             {  
    50.                 int p = res[u][i] < e[u] ? res[u][i] : e[u];  
    51.                 if(p>0 && (u==s || dist[u] == dist[i] + 1))  
    52.                 {  
    53.                     res[u][i] -= p; res[i][u] += p;  
    54.                     if(i == t) max_flow += p;  
    55.                     e[u] -= p; e[i] += p;  
    56.                     if(i != s && i != t) Q.push(List(i, dist[i]));  
    57.                 }  
    58.             }  
    59.             if(u!=s && u!=t && e[u]>0)  
    60.             {  
    61.                 dist[u]++;  
    62.                 Q.push(List(u, dist[u]));  
    63.             }  
    64.         }  
    65.         return max_flow;  
    66.     }  
    67. } G;  
    68.   
    69. int main()  
    70. {  
    71.     int m, n, u, v, c;  
    72.     while(~scanf("%d%d", &m, &n))  
    73.     {  
    74.         G.init(n);  
    75.         while(m--)  
    76.         {  
    77.             scanf("%d%d%d", &u, &v, &c);  
    78.             G.add(u, v, c);  
    79.         }  
    80.         cout<<G.push_relablel(1, n)<<endl;  
    81.     }  
    82.     return 0;  
    83. }  


    最短增广路算法(SAP):

    算法思想:

            每次在层次网络中找一个含弧最少的增广路进行增广;

    算法实现步骤:

    (1) 初始化容量网络和网络流;

    (2) 构造残留网络和层次网络,若汇点不在层次网络中,则算法结束;

    (3) 在层次网络中不断用BFS 增广,直到层次网络中没有增广路为止;每次增广完毕,在层次网络中要去掉因改进流量而导致饱和的弧;

    (4) 转步骤(2)。

    算法实现:

    [cpp] view plain copy
    1. #include <cstdio>  
    2. #include <cstring>  
    3. #include <queue>  
    4. #include <iostream>  
    5. using namespace std;  
    6. #define INF 0x3f3f3f3f  
    7. const int maxn = 222;  
    8.   
    9. struct EK              //Edmonds-Krap 算法,又称SAP算法,时间复杂度O(n*m*m);  
    10. {  
    11.     int res[maxn][maxn];  
    12.     int pre[maxn];  
    13.     int n;  
    14.   
    15.     void init(int x)  
    16.     {  
    17.         n = x;  
    18.         memset(res, 0, sizeof(res));  
    19.     }  
    20.   
    21.     void add(int u, int v, int c)  
    22.     {  
    23.         res[u][v] += c;  
    24.     }  
    25.   
    26.     bool bfs(int s, int t)  
    27.     {  
    28.         queue<int> Q;  
    29.         memset(pre, -1, sizeof(pre));  
    30.         Q.push(s);  
    31.         while(!Q.empty())  
    32.         {  
    33.             int u = Q.front(); Q.pop();  
    34.             for(int i = 1; i <= n; ++i)  
    35.             {  
    36.                 if(res[u][i] && pre[i] == -1)         //有增广量而没有加入增广路  
    37.                 {  
    38.                     pre[i] = u;  
    39.                     if(i == t) return true;  
    40.                     Q.push(i);  
    41.                 }  
    42.             }  
    43.         }  
    44.         return false;  
    45.     }  
    46.   
    47.     int sap(int s, int t)  
    48.     {  
    49.         int max_flow = 0;  
    50.         while(bfs(s, t))  
    51.         {  
    52.             int a = INF;  
    53.             for(int i = t; i != s; i = pre[i])  
    54.                 a = min(a, res[pre[i]][i]);  
    55.             for(int i = t; i != s; i = pre[i])  
    56.             {  
    57.                 res[pre[i]][i] -= a;  
    58.                 res[i][pre[i]] += a;  
    59.             }  
    60.             max_flow += a;  
    61.         }  
    62.         return max_flow;  
    63.     }  
    64. } G;  
    65.   
    66. int main()  
    67. {  
    68.     int n, m, u, v, c;  
    69.     while(~scanf("%d%d", &m, &n))  
    70.     {  
    71.         G.init(n);  
    72.         while(m--)  
    73.         {  
    74.             scanf("%d%d%d", &u, &v, &c);  
    75.             G.add(u, v, c);  
    76.         }  
    77.         cout<< G.sap(1, n) <<endl;  
    78.     }  
    79.     return 0;  
    80. }  


    连续最短增广路算法(Dinic):


    算法思想:

            Dinic 算法的思想也是分阶段地在层次网络中增广。它与最短增广路算法不同之处是:在Dinic 算法中,只需一次DFS 过程就可以实现多次增广。

    算法实现步骤:

    (1) 初始化容量网络和网络流;

    (2) 构造残留网络和层次网络,若汇点不在层次网络中,则算法结束;

    (3) 在层次网络中用一次DFS 过程进行增广,DFS 执行完毕,该阶段的增广也执行完毕;

    (4) 转步骤(2)。

    算法实现:

    [cpp] view plain copy
    1. #include <bits/stdc++.h>  
    2. using namespace std;  
    3. #define INF 0x3f3f3f3f  
    4. const int maxn = 222;  
    5.   
    6. struct Dinic                //时间复杂度O(n*n*m)  
    7. {  
    8.     int res[maxn][maxn];  
    9.     int dist[maxn];  
    10.     int n;  
    11.   
    12.     void init(int x)  
    13.     {  
    14.         n = x;  
    15.         memset(res, 0, sizeof(res));  
    16.     }  
    17.   
    18.     void add(int u, int v, int c)  
    19.     {  
    20.         res[u][v] += c;  
    21.     }  
    22.   
    23.     int bfs(int s)  
    24.     {  
    25.         memset(dist, 0xff, sizeof(dist));  
    26.         dist[s] = 0;  
    27.         queue<int> Q;  
    28.         Q.push(s);  
    29.         while(!Q.empty())  
    30.         {  
    31.             int u = Q.front(); Q.pop();  
    32.             for(int i = 1; i <= n; ++i)  
    33.             {  
    34.                 if(dist[i] < 0 && res[u][i] > 0)  
    35.                 {  
    36.                     dist[i] = dist[u] + 1;  
    37.                     Q.push(i);  
    38.                 }  
    39.             }  
    40.         }  
    41.         if(dist[n] > 0) return true;  
    42.         return false;  
    43.     }  
    44.   
    45.     int Find(int x, int low)  
    46.     {  
    47.         int a = 0;  
    48.         if(x == n) return low;  
    49.         for(int i = 1; i <= n; ++i)  
    50.         {  
    51.             if(res[x][i] > 0 && dist[i] == dist[x] + 1 && (a = Find(i, min(low, res[x][i]))))  
    52.             {  
    53.                 res[x][i] -= a;  
    54.                 res[i][x] += a;  
    55.                 return a;  
    56.             }  
    57.         }  
    58.         return 0;  
    59.     }  
    60.   
    61.     int dinic(int s, int t)  
    62.     {  
    63.         int max_flow = 0, tmp;  
    64.         while(bfs(1))  
    65.         {  
    66.             while(tmp = Find(1, INF))  
    67.                 max_flow += tmp;  
    68.         }  
    69.         return max_flow;  
    70.     }  
    71. } G;  
    72.   
    73. int main()  
    74. {  
    75.     int m, n, u, v, c;  
    76.     while(~scanf("%d%d", &m, &n))  
    77.     {  
    78.         G.init(n);  
    79.         while(m--)  
    80.         {  
    81.             scanf("%d%d%d", &u, &v, &c);  
    82.             G.add(u, v, c);  
    83.         }  
    84.         cout<< G.dinic(1, n) <<endl;  
    85.     }  
    86.     return 0;  
    87. }  

  • 相关阅读:
    突然想谈谈——我的软件测试入门
    js+rem动态计算font-size的大小,适配各种手机设备!
    iOS 如何打测试包,直接给测试人员使用(绝对的新手入门)
    去掉无用的多余的空格(string1.前后空格,2.中间空格)
    iOS 自定义键盘ToolBar(与键盘的弹出、收起保持一致)
    iOS上线...踩坑
    iOS10 导航条,这个二狗子变了...踩坑
    ios程序发布测试打包
    获取毫秒级时间戳
    弹簧动画效果(系统自带方法)
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776053.html
Copyright © 2020-2023  润新知