zoukankan      html  css  js  c++  java
  • UVa820 Internet Bandwidth

    最大流模板。

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <vector>
      5 #include <queue>
      6 #include <iostream>
      7 using namespace std;
      8 const int MAXN = 100 + 20;
      9 const int INF = 0x3f3f3f3f;
     10 
     11 int N, S, T, C;
     12 
     13 namespace dinic
     14 {
     15     struct edge
     16     {
     17         int to, cap, rev;
     18         edge(int v = 0, int c = 0, int r = 0) : 
     19         to(v), cap(c), rev(r) {}
     20     };
     21     vector<edge> g[MAXN];
     22     int level[MAXN], iter[MAXN];
     23 
     24     inline void init()
     25     {
     26         for(int i = 0; i <= N; i++)
     27             g[i].clear();
     28         memset(level, 0, sizeof(level));
     29         memset(iter, 0, sizeof(iter));
     30     }
     31 
     32     inline void addedge(int u, int v, int c)
     33     {
     34         g[u].push_back(edge(v, c, g[v].size()));
     35         g[v].push_back(edge(u, c, g[u].size() - 1));
     36     }
     37 
     38     inline bool bfs()
     39     {
     40         queue<int> q;
     41         memset(level, -1, sizeof(level));
     42         q.push(S);
     43         level[S] = 0;
     44 
     45         while(!q.empty())
     46         {
     47             int u = q.front(); q.pop();
     48             for(int i = 0; i < (int) g[u].size(); i++)
     49             {
     50                 edge e = g[u][i];
     51                 if(e.cap && level[e.to] == -1)
     52                 {
     53                     level[e.to] = level[u] + 1;
     54                     q.push(e.to);
     55                 }
     56             }
     57         }
     58         return ~level[T];
     59     }
     60 
     61     int dfs(int u, int flow)
     62     {
     63         if(u == T) return  flow;
     64 
     65         for(int &i = iter[u]; i < (int) g[u].size(); i++)
     66         {
     67             edge &e = g[u][i];
     68             if(e.cap && level[e.to] == level[u] + 1)
     69             {
     70                 int d = dfs(e.to, min(flow, e.cap));
     71                 if(d)
     72                 {
     73                     e.cap -= d;
     74                     g[e.to][e.rev].cap += d;
     75                     return d;
     76                 }
     77             }
     78         }
     79         return 0;
     80     }
     81 
     82     int maxflow()
     83     {
     84         int d = 0, flow = 0;
     85         while(bfs())
     86         {
     87             memset(iter, 0, sizeof(iter));
     88             while(d = dfs(S, INF), d) flow += d;
     89         }
     90         return flow;
     91     }
     92 }
     93 
     94 int main()
     95 {
     96     //freopen("820.in", "r", stdin);
     97     //freopen("820.out", "w", stdout);
     98     int cnt = 0;
     99     while(cin>>N, N)
    100     {
    101         cnt++;
    102 
    103         dinic::init();
    104         cin>>S>>T>>C;
    105         int u, v, c;
    106         for(int i = 1; i <= C; i++)
    107         {
    108             scanf(" %d %d %d", &u, &v, &c);
    109             dinic::addedge(u, v, c);
    110         }
    111 
    112         printf("Network %d
    ", cnt);
    113         printf("The bandwidth is %d.
    ", dinic::maxflow());
    114         puts("");
    115     }
    116     return 0;
    117 }
  • 相关阅读:
    练习5-3 数字金字塔 (15分)
    JSTL标签
    ssm+mysql+jsp打造在线考试系统WeKnow-学生端
    JSP常用内置对象
    mybatis入门2
    mybtis入门
    数据源的作用
    ssm动态查询向前台传json
    ssm中的注解
    ssm中的模糊查询
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9032138.html
Copyright © 2011-2022 走看看