zoukankan      html  css  js  c++  java
  • hdoj1523-Drainage Ditches

    Drainage Ditches

    Problem 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. 一是要设置反向边,反向边初始的容量为0,当我们给一条边增加流量时,同时我们给其反向边的容量也增加同样的流量,这样便于我们在后面的操作中可以取消先前加的流量。
    2. 其次,采用dfs遍历路径,找出当前路径的可以通过的最大流量(即路径中所含边的最大容量),然后采用回溯法将路径中的每条边都减去当前要增加的流量值,同时给反向边增加容量。

    code

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    const int MAX = 1000 + 5; 
    const int INF = 0x3f3f3f3f;
    
    class Node
    {
        public:
            int to, cap, rev;
            Node(int _to, int _cap, int _rev)    
            {
                to = _to;  //终点
                cap = _cap;  //容量
                rev = _rev;  //反向边的编号
            }
    };
    
    vector<vector<Node> > v(MAX);
    bool vis[MAX];
    int n, m;
    
    int dfs(int s, int t, int f)
    {
        if(s == t)
        {
            return f;
        }
        vis[s] = true;
        for(int i = 0; i < v[s].size(); ++ i)
        {
            Node &temp = v[s][i];            //引用 
            if(!vis[temp.to] && temp.cap > 0)
            {
                int d = dfs(temp.to, t, min(temp.cap, f));  //找出当前路径的最小值 
                if(d > 0)
                {
                    temp.cap -= d;   //采用回溯法改变路径中每条边的容量
                    v[temp.to][temp.rev].cap += d;            //反向时可抵去 
                    return d;
                }
            }
        }
        return 0;   //!!!!!
    }
    
    int ek(int s, int t)
    {
        int f = 0;
        int flow = 0;
        while(1)
        {
            memset(vis, false, sizeof(vis));
            f = dfs(s, t, INF);
            if(f == 0)
            {
                return flow;
            }
            flow += f;
        }
    }
    
    int main()
    {
        //ifstream cin("data.in");
        while(cin >> n >> m)
        {
            for(int i = 0; i < n; i ++)
            {
                int s, t, cap;
                cin >> s >> t >> cap;
                v[s].push_back(Node(t, cap, v[t].size()));
                v[t].push_back(Node(s, 0, v[s].size()-1));
            }
            cout << ek(1, m) << endl;
            for(int i = 0; i < MAX; i ++)
            {
                v[i].clear();//清空边的信息
            } 
        }
        return 0;
    }
  • 相关阅读:
    iOS
    2016-北京游线路规划
    OC 组合实现多继承
    如何把项目托管到GitHub
    iOS6和iOS7代码的适配(2)——status bar
    iOS6和iOS7代码的适配(1)
    Xcode ARC需要什么版本的环境支持
    内容页直接输出图集函数及使用方法
    vue-cli如何引入bootstrap工具
    实现Sublime Text3中vue文件高亮显示的最有效的方法
  • 原文地址:https://www.cnblogs.com/topk/p/6580087.html
Copyright © 2011-2022 走看看