POJ1273 Drainage Ditches(dinic)

1 篇文章 0 订阅
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
经典水题,纯粹是测试dinic算法的正确性而已。
建图也完全不用想。
推荐一篇文章

网络流入门—用于最大流的Dinic算法

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 100100;
using namespace std;

int head[maxn];
int dis[maxn];
int tot;
struct edge{
    int from,to,cap,next;
};
edge g[maxn];
void init(){
    tot=1;
    mes(head,-1);
}
void addEdge(int u,int v,int w){
    edge &t1=g[tot];
    t1={u,v,w,head[u]};
    head[u]=tot++;
    edge &t2=g[tot];
    t2={v,u,0,head[v]};
    head[v]=tot++;
}
//
int bfs(int s,int t){
    queue<int> que;
    que.push(s);
    mes(dis,0x3f);
    dis[s]=0;
    while(!que.empty()){
        int u=que.front();
        que.pop();
        for(int i=head[u];~i;i=g[i].next){
           int v=g[i].to;
           int w=g[i].cap;
            if(w>0&&inf==dis[v]){
                dis[v]=dis[u]+1;
                que.push(v);
            }
        }
    }
    return dis[t]!=inf; //->t
}
int dfs(int cur,int t,int maxflow){
    if(cur==t){
        return maxflow;
    }
    for(int i=head[cur];~i;i=g[i].next){
        int u=g[i].to;
        if(dis[cur]+1==dis[u]&&g[i].cap>0){
            int f=dfs(u,t,min(g[i].cap,maxflow));
            if(f){
                g[i].cap-=f;
                g[i^1].cap+=f;
                return f;
            }
        }
    }
    return 0;
}
int dinic(int s,int t){
    int ans=0;
    while(bfs(s,t)){
        ans+=dfs(s,t,inf);
    }
    return ans;
}
int main()
{
    //fin;
    int n,m,u,v,w;
    while(~scanf("%d%d",&n,&m)){
        init();
        while(n--){
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u,v,w);
        }
        cout<<dinic(1,m)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值