POJ 1273 Drainage Ditches

来源:互联网 发布:淘宝商家怎么贷款 编辑:程序博客网 时间:2024/06/08 11:05
Drainage Ditches
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 56993
Accepted: 21911

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 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

Source

USACO 93

#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>using namespace std;#define MAX_POINT 300  // 点的最多数目#define INF 99999999struct edge {  // 定义边,to为边所到端点,cap是容量,rev为反向边    int to;    int cap;    int rev;    edge() {}    edge(int tto, int ccap, int rrev) {        to = tto;        cap = ccap;        rev = rrev;    }};bool vis[MAX_POINT];  // 用于寻找路径的时候记录点是否被访问过int points;  // 点数vector<edge> G[MAX_POINT];  // 图void addEdge(int from, int to, int cap) {  // 往图中加入一条从from到to容量为cap的路径    G[from].push_back(edge(to, cap, G[to].size()));    G[to].push_back(edge(from, 0, G[from].size() - 1));  // 加入反向边}int makeSuperStart(vector<int> start) {  // 若有多个源点,创造一个超级源点    if (start.size() == 1) return start[0];    int s = points;    points++;    for (int i = 0; i < start.size(); i++) {        addEdge(s, start[i], INF);        addEdge(start[i], s, INF);    }    return s;}int makeSuperEnd(vector<int> end) {  // 若有多个汇点,创造一个超级汇点    if (end.size() == 1) return end[0];    int e = points;    points++;    for (int i = 0; i < end.size(); i++) {        addEdge(end[i], e, INF);        addEdge(e, end[i], INF);    }    return e;}int dfs(int from, int to, int lastCap) {  // 通过深搜找到一条路径    if (from == to) return lastCap;    vis[from] = true;    for (int i = 0; i < G[from].size(); i++) {        edge & e = G[from][i];        if (!vis[e.to] && e.cap > 0) {            int nextFlow = dfs(e.to, to, min(lastCap, e.cap));            if (nextFlow > 0) {                            e.cap -= nextFlow;                G[e.to][e.rev].cap += nextFlow;                return nextFlow;            }        }    }    return 0;}int MaxFlow(vector<int> start, vector<int> end) {  // 用于找到最大流    int maxFlow = 0;    int s = makeSuperStart(start);    int e = makeSuperEnd(end);    while (1) {        memset(vis, false, sizeof(vis));        int f = dfs(s, e, INF);        if (f == 0) return maxFlow;        maxFlow += f;    }    return maxFlow;}int main() {    std::ios::sync_with_stdio(false);    while (1) {        int n, m;        vector<int> start, end;        cin >> n >> m;        if (cin.eof()) break;        points = 6;        start.push_back(1);        end.push_back(m);        //reset        for (int i = 0; i < MAX_POINT; i++) G[i].clear();        points = n;        //reset        for (int i = 0; i < n; i++) {            int from, to, cap;            cin >> from >> to >> cap;            addEdge(from, to, cap);        }        cout << MaxFlow(start, end) << endl;    }    //getchar();    //getchar();    return 0;}                     


0 0
原创粉丝点击