HDU1532 Drainage Ditches 最大流Dinic模板

10 篇文章 0 订阅
7 篇文章 0 订阅
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16186    Accepted Submission(s): 7695


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



Source
USACO 93 

大意:
农夫约翰有一块地种了三叶草 每逢下雨就被水淹,于是约翰修了n条水管,m个交点
1是源点 m是汇点 给出n条边(s,e,c)表示从s->e 最大流量为c 问到达汇点的最大流量

裸最大流


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 200 + 5;
const int M = 2*N;

int level[N];//标号 level[i]:s到i的最短距离

struct Edge{
    int to,c,next;
}edge[M];
int head[N];

inline void add_edge(int k,int u,int v,int c){
    edge[k].to = v;
    edge[k].c = c;
    edge[k].next = head[u];
    head[u] = k;
}

bool bfs(int s,int t,int n){//标号 计算level
    deque<int>que;
    fill(level,level+n+1,-1);
    que.push_back(s);
    level[s] = 0;
    while(!que.empty()){
        int u = que.front();
        if(u == t){
            return true;
        }
        que.pop_front();
        for(int i = head[u];i!=-1;i = edge[i].next){
            if(edge[i].c > 0 && level[edge[i].to] == -1){
                level[edge[i].to] = level[u] + 1;
                que.push_back(edge[i].to);
            }
        }
    }
    return false;
}

int dfs(int u,int t,int maxf){//u:所在的点 t:汇点 maxf:能流到u的流量
    if(u == t){
        return maxf;
    }
    int sum = 0;
    for(int i = head[u];i!=-1;i = edge[i].next){
        Edge&e = edge[i];
        if(e.c>0 && level[e.to]>level[u]){
            int f = dfs(e.to,t,min(maxf - sum,e.c));
            sum += f;
            edge[i].c -= f;
            edge[i^1].c += f;
            if(sum == maxf){//流量用完了
                break;
            }
        }
    }
    level[u]=-1;
    return sum;
}

int max_flow(int s,int t,int n){//s:源点 t:汇点 n:点数
    int ans = 0;
    while(bfs(s,t,n)){
        ans += dfs(s,t,2*INF);
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int n,m,u,v,c;
    while(~scanf("%d%d",&m,&n)){
        fill(head,head + n + 1,-1);
        for(int i=0;i<m;++i){
            scanf("%d%d%d",&u,&v,&c);
            add_edge(2*i,u,v,c);
            add_edge(2*i + 1,v,u,0);
        }
        printf("%d\n",max_flow(1,n,n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值