zoukankan      html  css  js  c++  java
  • Drainage Ditches~网络流模板

    Description

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
    Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
    Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

    Input

    The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

    Output

    For each case, output a single integer, the maximum rate at which water may emptied from the pond.

    Sample Input

    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10
    

    Sample Output

    50

      1 #include<stdio.h>
      2 #include<string>
      3 #include<string.h>
      4 #include<vector>
      5 #include<queue>
      6 using namespace std;
      7 const int maxn = 1e5 + 10;
      8 const int INF = 999999999;
      9 struct node {
     10     int from, to, cap, flow;
     11 };
     12 struct Dinic {
     13     int n, m, s, t;
     14     vector<node>nodes;
     15     vector<int>g[maxn];
     16     int vis[maxn];
     17     int d[maxn];
     18     int cur[maxn];
     19     void clearall(int n) {
     20         for (int i = 0 ; i < n ; i++) g[i].clear();
     21         nodes.clear();
     22     }
     23     void clearflow() {
     24         int len = nodes.size();
     25         for (int i = 0 ; i < len ; i++) nodes[i].flow = 0;
     26     }
     27     void add(int from, int to, int cap) {
     28         nodes.push_back((node) {
     29             from, to, cap, 0
     30         });
     31         nodes.push_back((node) {
     32             to, from, 0, 0
     33         });
     34         m = nodes.size();
     35         g[from].push_back(m - 2);
     36         g[to].push_back(m - 1);
     37     }
     38     bool bfs() {
     39         memset(vis, 0, sizeof(vis));
     40         queue<int>q;
     41         q.push(s);
     42         d[s] = 0;
     43         vis[s] = 1;
     44         while(!q.empty()) {
     45             int x = q.front();
     46             q.pop();
     47             int len = g[x].size();
     48             for (int i = 0 ; i < len ; i++) {
     49                 node &e = nodes[g[x][i]];
     50                 if (!vis[e.to] && e.cap > e.flow ) {
     51                     vis[e.to] = 1;
     52                     d[e.to] = d[x] + 1;
     53                     q.push(e.to);
     54                 }
     55             }
     56         }
     57         return vis[t];
     58     }
     59     int dfs(int x, int a) {
     60         if  (x == t || a == 0) return a;
     61         int flow = 0, f, len = g[x].size();
     62         for (int &i = cur[x] ; i < len ; i++) {
     63             node & e = nodes[g[x][i]];
     64             if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0 ) {
     65                 e.flow += f;
     66                 nodes[g[x][i] ^ 1].flow -= f;
     67                 flow += f;
     68                 a -= f;
     69                 if (a == 0) break;
     70             }
     71         }
     72         return flow;
     73     }
     74     int maxflow(int a, int b) {
     75         s = a;
     76         t = b;
     77         int flow = 0;
     78         while(bfs()) {
     79             memset(cur, 0, sizeof(cur));
     80             flow += dfs(s, INF);
     81         }
     82         return flow;
     83     }
     84     vector<int>mincut() {
     85         vector<int>ans;
     86         int len = nodes.size();
     87         for (int i = 0 ; i < len ; i++) {
     88             node & e = nodes[i];
     89             if ( vis[e.from] && !vis[e.to] && e.cap > 0 ) ans.push_back(i);
     90         }
     91         return ans;
     92     }
     93     void reduce() {
     94         int len = nodes.size();
     95         for (int i = 0 ; i < len ; i++) nodes[i].cap -= nodes[i].flow;
     96     }
     97 } f;
     98 int main() {
     99     int n, m;
    100     while(~scanf("%d%d", &m, &n)) {
    101         f.clearall(n);
    102         f.clearflow();
    103         for (int i = 0 ; i < m ; i++) {
    104             int u, v, c;
    105             scanf("%d%d%d", &u, &v, &c);
    106             f.add(u, v, c);
    107         }
    108         printf("%d
    ", f.maxflow(1, n));
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    Oracle安装
    SSIS作业提示所请求的 OLE DB 访问接口 Microsoft.ACE.OLEDB.12.0 尚未注册
    数据库软考易混淆知识之信息化基础、项目管理
    数据库软考易混淆知识之安全性知识
    数据库软考易混淆知识之知识产权
    数据库软考易混淆知识之操作系统
    数据库软考易混淆知识之计算机网络
    数据库软考易混淆知识之软件工程
    数据库软考易混淆知识之程序语言基础
    数据库软考易混淆知识之计算机系统基础
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8870369.html
Copyright © 2011-2022 走看看