• uva820 Internet Bandwidth(最大流模板题)


    套的Dinic模板,权当测模板了~

      1 #include<cstdio>
      2 #include<cmath>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<map>
      6 #include<queue>
      7 #include<set>
      8 #include<vector>
      9 #include<iostream>
     10 #define PB push_back
     11 #define POB pop_back
     12 #define PII pair<int, int>
     13 #define PDD pair<double, double>
     14 #define rep(i, a, b) for(int i = a ; i < b ; i++)
     15 #pragma comment(linker, "/STACK:1024000000,1024000000")
     16 #pragma GCC push_options
     17 #pragma GCC optimize ("Os")
     18 #pragma  loop_opt(on)
     19 #ifdef WIN32
     20 #define INT64 "%I64d"
     21 #define UINT64 "%I64u"
     22 #else
     23 #define INT64 "%lld"
     24 #define UINT64 "%llu"
     25 #endif
     26 typedef long long LL;
     27 typedef unsigned long long ULL;
     28 using namespace std;
     29 const int INF = 0x3f3f3f3f;
     30 const int maxn = 110;
     31 struct Edge {
     32     int from, to, cap, flow;
     33     Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
     34 };
     35 struct Dinic {
     36     int n, m, s, t;
     37     vector<Edge> edges;
     38     vector<int> G[maxn];
     39     bool vis[maxn];
     40     int d[maxn];
     41     int cur[maxn];
     42     void AddEdge(int from, int to, int cap) {
     43         edges.PB(Edge(from, to, cap, 0));
     44         edges.PB(Edge(from, to, 0, 0));
     45         m = edges.size();
     46         G[from].PB(m - 2);
     47         G[to].PB(m - 1);
     48     }
     49 
     50     bool BFS() {
     51         memset(vis, 0, sizeof vis);
     52         queue<int> Q;
     53         Q.push(s);
     54         d[s] = 0;
     55         vis[s] = 1;
     56         while(!Q.empty()) {
     57             int x = Q.front(); Q.pop();
     58             for(int i = 0 ; i < (int)G[x].size() ; i++) {
     59                 Edge& e = edges[G[x][i]];
     60                 if(!vis[e.to] && e.cap > e.flow) {
     61                     vis[e.to] = 1;
     62                     d[e.to] = d[x] + 1;
     63                     Q.push(e.to);
     64                 }
     65             }
     66         }
     67         return vis[t];
     68     }
     69 
     70     int DFS(int x, int a) {
     71         if(x == t || a == 0) return a;
     72         int flow = 0, f;
     73         for(int& i = cur[x] ; i <(int)G[x].size() ; i++) {
     74             Edge& e = edges[G[x][i]];
     75             if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
     76                 e.flow += f;
     77                 edges[G[x][i] ^ 1].flow -= f;
     78                 flow += f;
     79                 a -= f;
     80                 if(a == 0) break;
     81             }
     82         }
     83         return flow;
     84     }
     85     int MaxFlow(int s, int t) {
     86         this->s = s;
     87         this->t = t;
     88         int flow = 0;
     89         while(BFS()) {
     90             memset(cur, 0, sizeof cur);
     91             flow += DFS(s, INF);
     92         }
     93         return flow;
     94     }
     95 
     96     void init() {
     97         edges.clear();
     98         for(int i = 0 ; i < maxn ; i++) G[i].clear();
     99         memset(d, 0, sizeof d);
    100     }
    101 } dinic;
    102 int main()
    103 {
    104     int n;
    105     int ca = 1;
    106     while(scanf("%d", &n) == 1 && n) {
    107         dinic.init();
    108         int a, b, m; scanf("%d%d%d" ,&a,  &b, &m);
    109         int from, to, cap;
    110         for(int i = 0 ; i < m ; i++) scanf("%d%d%d", &from, &to, &cap), dinic.AddEdge(from, to, cap), dinic.AddEdge(to, from, cap);
    111         printf("Network %d
    ", ca++);
    112         printf("The bandwidth is %d.
    
    ", dinic.MaxFlow(a, b));
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    JavaScript学习(二)
    javaScript学习(一)
    CSS学习(一)
    HTML学习(一)
    ES之node机器配置elasticsearch.yml
    ES之master机器配置elasticsearch.yml
    jenkins--前端依赖之 node
    jenkins--邮件插件配置
    JsonPath提取表达式
    this关键字的作用
  • 原文地址:https://www.cnblogs.com/tooyoungtoosimple/p/4756281.html
Copyright © 2020-2023  润新知