Drainage Ditches

来源:互联网 发布:阿里云大数据 编辑:程序博客网 时间:2024/06/14 11:15

链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=1532


题目:

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,汇点为n,问最大流是多少。


思路:

  模板题。


实现:

#include <bits/stdc++.h>using namespace std;const int maxn = 207, INF = 0x3f3f3f3f;int n, m, mp[maxn][maxn];//邻接矩阵int que[maxn*maxn], head, tail;//BFS队列 ,首,尾int dist[maxn];//距源点距离,分层图int ans;bool bfs() {    memset(dist,-1,sizeof dist);//dist一定要记得设置为-1    dist[1] = head = 0;    que[1] = tail = 1;    register int u,v;    while(head < tail) {        u = que[++head];        for(v=1 ; v<=n ; v++) {            if(mp[u][v] > 0 && dist[v] < 0) {                dist[v] = dist[u] + 1;                que[++tail] = v;            }        }    }    return dist[n] > 0;//汇点的DIS小于零,表明BFS不到汇点}//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广int find(int from, int low) {//low变量始终不更新    //low是源点到现在最窄的(剩余流量最小)的边的剩余流量    if (from == n) return low;//当前点是汇点    for (int i = 1, tmp = 0; i <= n; i++)        if (mp[from][i] > 0//两点之间连通            && dist[i] == dist[from] + 1//且当前点是分层图的下一层            && bool(tmp = find(i, min(low, mp[from][i])))) {//且tmp!=0,说明可以和汇点连通            //上面if中更新的是tmp            mp[from][i] -= tmp;            mp[i][from] += tmp;            return tmp;//这里return的是tmp不是low        }    return 0;}int main() {#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);#endif    ios_base::sync_with_stdio(false);cin.tie(0);    int i, u, v, f, tmp;    while(cin >> m >> n) {        memset(mp,0,sizeof mp);        for(i = ans = 0 ; i<m ; i++) {            cin >> u >> v >> f;            mp[u][v] += f;        }        while(bfs())        //要不停地建立分层图,如果BFS不到汇点才结束            while(tmp = find(1, INF), tmp != 0)            //每次BFS之后要不停地找增广路,直到找不到为止                ans += tmp;        cout << ans << '\n';    }    return 0;}