POJ 1273 --Drainage Ditches【最大流模板 && dinic】

来源:互联网 发布:美工学徒招聘是真的吗 编辑:程序博客网 时间:2024/05/22 04:46

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 62111 Accepted: 23859

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

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#define maxn 1000 + 100#define maxm 2000 + 200#define INF 0x3f3f3f3fusing namespace std;int cnt, n, m;int dist[maxn];int cur[maxn], vis[maxn];int head[maxn];struct node {    int u, v, cap, flow, next;};node edge[maxm];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u,int v, int w){    edge[cnt] = {u, v, w, 0, head[u]}; //正建边    head[u] = cnt++;    edge[cnt] = {v, u, 0, 0, head[v]}; //反建边    head[v] = cnt++;}void getmap(){    int u, v, w;    while(m--){        scanf("%d%d%d", &u, &v, &w);        add(u, v, w);    }    return ;}bool BFS(int st,int ed){    queue<int>q;    memset(dist, -1, sizeof(dist));    memset(vis, 0, sizeof(vis));    q.push(st);    dist[st] = 0;    vis[st] = 1;    while(!q.empty()){        int u = q.front();        q.pop();        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(!vis[E.v] && E.cap > E.flow){                vis[E.v] = 1;                dist[E.v] = dist[u] + 1;                if(E.v == ed) return true;                q.push(E.v);            }        }    }    return false;}int DFS(int x, int ed, int a){    if(x == ed || a == 0)        return a;        int flow = 0, f;    for(int &i = cur[x]; i != -1; i = edge[i].next){        node &E = edge[i];        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){            E.flow += f;            edge[i ^ 1].flow -= f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int maxflow(int st, int ed){    int flow = 0;;    while(BFS(st, ed)){        memcpy(cur, head, sizeof(head));        flow += DFS(st, ed, INF);    }    return flow;}int main (){    while(scanf("%d%d", &m, &n) != EOF){        init();        getmap();        printf("%d\n",maxflow(1, n));    }    return 0;}


0 0
原创粉丝点击