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;
}
發佈了60 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章