POJ1273【网络流】

    Drainage Ditches
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 91824   Accepted: 35588

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

Source

 
思路:给出点之间的容量,求整个网络的最大流,最大流板子题,这里用的是ISAP模板。
 
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxnE = 1e6 + 7; const int maxn  = 1e5 + 7; const int maxnQ = 1e6 + 7; const int inf   = 0x3f3f3f3f; struct edge { int v;///弧尾 int cap;///容量 int nxt;///指向下一条从同一个弧头出发的弧 }e[maxnE]; int head[maxn],cnt; int d[maxn],cur[maxn],pre[maxn],num[maxn]; int source,sink;///超级源、超级汇 int nv;///编号修改的上限 int n,m;

queue <int> q; void add(int u, int v, int capacity) {
    e[cnt].v = v;
    e[cnt].cap = capacity;
    e[cnt].nxt = head[u];
    head[u] = cnt++; //正向边  e[cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].nxt = head[v];
    head[v] = cnt++; //反向边 } void rev_bfs() {///反向bfs memset(num, 0, sizeof(num));
    memset(d, -1, sizeof(d));
    d[sink] = 0;///超级汇直接标记 num[0] = 1;
    q.push(sink); while(!q.empty()) { int u = q.front();
        q.pop(); for(int i = head[u]; ~i; i = e[i].nxt) { int v = e[i].v; if(~d[v]) continue;///已经标过号 d[v] = d[u] + 1;
            q.push(v);
            num[d[v]]++;
        }
    }
} int ISAP() {
    memcpy(cur, head, sizeof(cur));///当前弧优化  rev_bfs(); int flow = 0, u = pre[source] = source; int i; while(d[sink] < nv) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层 //printf("flow:%d\n",flow); if(u == sink) {///如果找到一条增广路,则沿着此条路修改flow int f = inf, neck; for(i = source; i != sink; i = e[cur[i]].v) {///修改流量 if(f > e[cur[i]].cap) {
                    f = e[cur[i]].cap;///不断减少所需要的流量 neck = i;///记录回退点,不用回到起点再找  }
            } for(i = source; i != sink; i = e[cur[i]].v) {///修改流量 e[cur[i]].cap -= f;
                e[cur[i] ^ 1].cap += f;
            }
            flow += f;
            u = neck;///回退  } for(i = cur[u]; ~i; i = e[i].nxt) { if(d[e[i].v] + 1 == d[u] && e[i].cap) break;
        } if(~i) { //如果存在可行的增广路 cur[u] = i;
            pre[e[i].v] = u;
            u = e[i].v;
        } else {///否则回退,重新找增广路 if(0 == (--num[d[u]])) break; int mind = nv; for(i = head[u]; ~i; i = e[i].nxt) { if(e[i].cap && mind > d[e[i].v]) {///寻找可以增广的最小下标 cur[u] = i;
                    mind = d[e[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];///回退  }
    } return flow;
} void init() {///初始化 memset(head, -1, sizeof(head));
    cnt = 0;
} void solve()
{ int u,v,c;
    init(); for(int i = 0; i < m; ++i) {
        scanf("%d %d %d",&u, &v, &c);
        add(u,v,c);
    }
    source = 1, sink = n, nv = sink + 1;
    printf("%d\n",ISAP());
} int main()
{ while(scanf("%d %d", &m, &n) != EOF) {
        solve();
    } return 0;
}

 

全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务