zoukankan      html  css  js  c++  java
  • [USACO09JAN]Total Flow【网络流】

    Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an apparently haphazard way. He wants to calculate the flow through the pipes.

    Two pipes connected in a row allow water flow that is the minimum of the values of the two pipe's flow values. The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can be reduced logically to a single pipe of flow capacity 3:

    +---5---+---3---+ -> +---3---+

    Similarly, pipes in parallel let through water that is the sum of their flow capacities:

    +---5---+

    ---+ +--- -> +---8---+

    +---3---+

    Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:

    +---5---+

    ---+ -> +---3---+

    +---3---+--

    All the pipes in the many mazes of plumbing can be reduced using these ideas into a single total flow capacity.

    Given a map of the pipes, determine the flow capacity between the well (A) and the barn (Z).

    Consider this example where node names are labeled with letters:

    +-----------6-----------+

    A+---3---+B +Z

    +---3---+---5---+---4---+

    C D

    Pipe BC and CD can be combined:

    +-----------6-----------+

    A+---3---+B +Z

    +-----3-----+-----4-----+

    D Then BD and DZ can be combined:

    +-----------6-----------+

    A+---3---+B +Z

    +-----------3-----------+

    Then two legs of BZ can be combined:

    B A+---3---+---9---+Z

    Then AB and BZ can be combined to yield a net capacity of 3:

    A+---3---+Z

    Write a program to read in a set of pipes described as two endpoints and then calculate the net flow capacity from 'A' to 'Z'. All

    networks in the test data can be reduced using the rules here.

    Pipe i connects two different nodes a_i and b_i (a_i in range

    'A-Za-z'; b_i in range 'A-Za-z') and has flow F_i (1 <= F_i <= 1,000). Note that lower- and upper-case node names are intended to be treated as different.

    The system will provide extra test case feedback for your first 50 submissions.

    思路:建立源点为A,汇点为Z的网络跑最大流

    注意读入格式

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<queue>
      5 #include<vector>
      6 #include<algorithm>
      7 
      8 using namespace std;
      9 
     10 const int maxn=1e4 + 7;
     11 const int inf=0x3f3f3f3f;
     12 
     13 template<class T>inline void read(T &res)
     14 {
     15     char c;T flag=1;
     16     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
     17     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
     18 }
     19 
     20 struct edge{int from,to,cap,flow;};
     21 
     22 struct isap
     23 {
     24     int n,s,t,p[maxn],d[maxn],cur[maxn],num[maxn];
     25     bool vis[maxn];
     26     vector<int>g[maxn];
     27     vector<edge>edges;
     28     void init(int n,int s,int t) {
     29         this->n = n;
     30         this->s = s;
     31         this->t = t;
     32         for(int i = 1;i <= n;i++) g[i].clear();
     33         edges.clear();
     34     }
     35     void addegde(int from,int to,int cap) {
     36         edges.push_back((edge){from, to, cap, 0});
     37         edges.push_back((edge){to, from, 0, 0});
     38         int m = edges.size();
     39         g[from].push_back(m-2);
     40         g[to].push_back(m-1);
     41     }
     42 
     43     int augment() {///找增广路
     44         int x = t,a = inf;
     45         while(x!=s) {
     46             a = min(a, edges[p[x]].cap - edges[p[x]].flow);
     47             x = edges[p[x]].from;
     48         }
     49         x=t;
     50         while(x != s) {
     51             edges[p[x]].flow += a;
     52             edges[p[x]^1].flow = -a;
     53             x = edges[p[x]].from;
     54         }
     55         return a;
     56     }
     57     int maxflow() {///更新最大流
     58         int flow = 0;
     59         memset(num, 0, sizeof(num));
     60         memset(cur, 0, sizeof(cur));
     61         for(int i = 1; i <= n; i++) num[d[i]]++;
     62         int x = s;
     63         while(d[s] < n) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
     64             if(x == t) {
     65                 flow += augment();
     66                 x = s;//回退
     67             }
     68             bool ok = 0;
     69             for(int i = cur[x]; i < g[x].size(); i++) {
     70                 edge &e = edges[g[x][i]];
     71                 if(d[x] == d[e.to] + 1 && e.cap > e.flow) {
     72                     p[e.to] = g[x][i];
     73                     cur[x] = i;x = e.to;
     74                     ok = 1;
     75                     break;
     76                 }
     77             }
     78             if(!ok) {
     79                 int m = n-1;
     80                 for(int i = 0; i < g[x].size();i++) {
     81                     edge &e=edges[g[x][i]];
     82                     if(e.cap>e.flow) m=min(m,d[e.to]);
     83                 }
     84                 num[d[x]]--;
     85                 if(!num[d[x]]) break;
     86                 d[x] = m+1;
     87                 num[d[x]]++;
     88                 cur[x] = 0;
     89                 if(x != s) x = edges[p[x]].from;
     90             }
     91         }
     92         return flow;
     93     }
     94 }ISAP;
     95 
     96 int main()
     97 {
     98     int n,m,s,t,u,v,w;
     99     n = 1007;
    100     s = 1,t = 26;
    101     cin >> m;
    102     ISAP.init(n,s,t);
    103     for(int i = 1; i <= m; i++) {
    104         char u,v;
    105         int c;
    106         cin >> u >> v >> c;
    107         ISAP.addegde(u - 'A' + 1,v - 'A' + 1,c);
    108     }
    109     printf("%d
    ",ISAP.maxflow());
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    开源跨平台数据格式化框架概览
    (12) MVC5 EF6 Bootstrap3
    前端构建利器Grunt—Bower
    深入理解JavaScript系列(33):设计模式之策略模式(转)
    为什么MVC不是一种设计模式(转)
    java Double保留小数点位数
    网线直接连接电脑可以上网,但通过无线路由器时却上不了网(转)
    How to install PL/SQL developer on linux (转)
    自己动手写CPU之第八阶段(4)——转移指令实现过程2
    Eclipse中SVN的安装步骤(两种)和用法
  • 原文地址:https://www.cnblogs.com/orangeko/p/11930510.html
Copyright © 2011-2022 走看看