網路流最經典的入門題 各種網路演算法都能AC。

來源:互聯網
上載者:User

標籤:

                              Drainage Ditches

 

題目抽象:給你m條邊u,v,c。   n個定點,源點1,匯點n.求最大流。  最好的入門題,各種演算法都可以拿來練習

(1):  一般增廣路演算法  ford()

  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <string>  7 #include <vector>  8 #include <set>  9 #include <map> 10 #include <stack> 11 #include <queue> 12 #include <sstream> 13 #include <iomanip> 14 using namespace std; 15 typedef long long LL; 16 const int INF = 0x4fffffff; 17 const double EXP = 1e-5; 18 const int MS = 10005; 19 const int SIZE = 10005; 20  21 struct edge 22 { 23     int c, f; 24 }edges[MS][MS]; 25  26 int n, m; 27  28 int flag[MS]; 29 int pre[MS]; 30 int alpha[MS]; 31  32 int que[SIZE]; 33  34 int u; 35 int qs, qe; 36 int i, j; 37  38 void ford() 39 { 40     while (1) 41     { 42         //  label method 43         memset(flag, 0xff, sizeof(flag)); 44         memset(pre, 0xff, sizeof(pre)); 45         memset(alpha, 0xff, sizeof(alpha)); 46         flag[1] = 0; 47         pre[1] = 0; 48         alpha[1] = INF; 49         qs = qe = 0; 50         que[qe++] = 1; 51  52         while (qs < qe&&flag[n ] == -1) 53         { 54             u = que[qs++]; 55             for (int i = 1; i <= n; i++) 56             { 57                 if (flag[i] == -1) 58                 { 59                     if (edges[u][i].c >0&&edges[u][i].f < edges[u][i].c) 60                     { 61                         flag[i] = 0; pre[i] = u; 62                         alpha[i] = min(alpha[u], edges[u][i].c - edges[u][i].f); 63                         que[qe++] = i; 64                     } 65                     else if (edges[i][u].c>0&&edges[i][u].f>0) 66                     { 67                         flag[i] = 0; pre[i] = -u; 68                         alpha[i] = min(alpha[u], edges[i][u].f); 69                         que[qe++] = i; 70                     } 71                 } 72             } 73             flag[u] = 1; 74         }    //  END OF WHILE 75         if (flag[n ] == -1 || alpha[n ] == 0) 76             break;       //  END OF WHILE 77  78         int k1 = n , k2 = abs(pre[k1]); 79         int a = alpha[n ]; 80         while (1) 81         { 82             if (edges[k2][k1].c>0)         //用f是否==INF來判斷正向 83                 edges[k2][k1].f += a; 84             else 85                 edges[k1][k2].f -= a; 86             if (k2 == 1) 87                 break; 88             k1 = k2; 89             k2 = abs(pre[k1]); 90         }     //  END OF WHILE 91     }  //  END OF WHILE 92  93     int max_flow = 0; 94     for (int i = 1; i <=n; i++) 95     { 96         for (int j = 1; j <=n; j++) 97         { 98             if (i == 1 && edges[i][j].f >0) 99                 max_flow += edges[i][j].f;100           //  if (edges[i][j].f > 0)101            //     printf("%d-->%d: %d\n", i, j, edges[i][j].f);102         }103     }104     printf("%d\n", max_flow);105 }106 107 int main()108 {109     int u, v, c, f;110     while(scanf("%d%d",&m,&n)!=EOF)111     {112           for (int i = 0; i <= n; i++)113             for (int j = 0; j <= n; j++)114                  // edges[i][j].c = edges[i][j].f = INF;115                  edges[i][j].c=edges[i][j].f=0;116           for (int i = 0; i < m; i++)117           {118               //scanf("%d%d%d%d", &u, &v, &c, &f);119               scanf("%d%d%d",&u,&v,&c);         //   這裡是零流120               edges[u][v].c +=c;                           //  可能有重邊121              // edges[u][v].f = f;122           }123           ford();124     }125     return 0;126 }

 

網路流最經典的入門題 各種網路演算法都能AC。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.