Dinic 清真模板(数组类型 + 当前弧优化)Poj—1273

来源:互联网 发布:华西电子盘软件下载 编辑:程序博客网 时间:2024/05/07 08:12

Dinic 清真模板(数组类型 + 当前弧优化)Poj—1273


先颓一波(´v`)miku sama 赛高

这里写图片描述


Drainage Ditches

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.

instance

data

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

answer

50


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#define INF 0x3f3f3f3f#define M 2000using namespace std;int ind[M] , dis[M] , h[M];int n , m , st , ed;int tot(1);int mi(int a , int b){return a < b ? a : b;}struct node{    int e , next , flow , cap;    node(){e = next = flow = cap = 0;}}edge[M];void add(int a , int b , int c){    edge[++tot].next = ind[a];    edge[ind[a] = tot].e = b;    edge[tot].flow = 0 , edge[tot].cap = c;}bool bfs(){    memset(dis , 0 , sizeof dis);    memcpy(h , ind , sizeof ind);    queue<int> qu;    qu.push(st);    dis[st] = 1;    while(!qu.empty()){        int now = qu.front();        qu.pop();        for(int i = ind[now] ; i ; i = edge[i].next){            int v = edge[i].e;            if(dis[v] || edge[i].cap <= edge[i].flow)continue;            dis[v] = dis[now]+1;            qu.push(v);        }    }    return dis[ed];}int dfs(int k , int a){    if(k == ed || a == 0)return a;    int f(0) , flow(0);    for(int i = h[k] ; i && a ; i = edge[i].next){        int v = edge[i].e;        if(dis[k] != dis[v] - 1)continue;        f = mi(a , dfs(v , mi( edge[i].cap - edge[i].flow , a ) ) );        flow += f;        a -= f;        edge[i].flow += f;        edge[i^1].flow -= f;        h[k] = i;    }    return flow;}int main(){    int x , y ,z;        while ( scanf( "%d%d", &m, &n ) != EOF ) {            st = 1 , ed = n , tot = 1;            memset(ind ,0 ,sizeof ind);        for ( int i = 1; i <= m; i++ ) {            scanf( "%d%d%d", &x, &y, &z );            add( x, y, z );            add( y, x, 0 );        }        int flow = 0;        while ( bfs() ) flow += dfs(1, INF);        printf( "%d\n", flow );    }    return 0;}
原创粉丝点击