POJ--1273--Drainage Ditches【Dinic】网络最大流

链接:http://poj.org/problem?id=1273

题意:农夫的农场被水淹了,他建了一些排水沟来排水,最终把这些水排到小河里,现有n个点,节点1~n-1为池塘,水从1开始流,n为小河。然后有m条排水沟,每条排水沟告诉起点、终点、最大水流速度,现在求这个排水系统的最大排水速度。

网络最大流裸题,dinic模板

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 100100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson r,m+1,rt<<1|1

struct node{
    int u,w,next;
}edge[5000];
int head[2100],vis[2100],dist[2100];
int n,m,src,sink,cnt;
void add_edge(int a,int b,int c){
    edge[cnt].u = b;
    edge[cnt].w = c;
    edge[cnt].next = head[a];
    head[a] = cnt++;
}
void bfs(){
    int i, j;
    memset(dist,0,sizeof(dist));
    queue<int>q;
    vis[src] = 1;
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(i=head[u];i!=-1;i=edge[i].next){
            if(!vis[edge[i].u]&&edge[i].w){
                q.push(edge[i].u);
                dist[edge[i].u] = dist[u] + 1;
                vis[edge[i].u] = 1;
            }
        }
    }
}
int dfs(int u,int delta){
    int i,j;
    if(u==sink) return delta;
    else{
        int ret = 0;
        for(i=head[u];i!=-1&δi=edge[i].next){
            if(edge[i].w&&dist[edge[i].u]==dist[u]+1){
                int dd = dfs(edge[i].u,min(edge[i].w,delta));
                edge[i].w -= dd;
                edge[i^1].w += dd;
                delta -= dd;
                ret += dd;
            }
        }
        return ret;
    }
}
int maxflow(){
    int ret = 0;
    while(1){
        memset(vis,0,sizeof(vis));
        bfs();
        if(!vis[sink])  break;
        ret += dfs(src,INF);
    }
    return ret;
}
int main(){
    int i,j;
    int a,b,x;
    while(scanf("%d%d",&m,&n)!=EOF){
        memset(head,-1,sizeof(head));
        int cnt = 0;
        src = 1;
        sink = n;
        for(i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&x);
            add_edge(a,b,x);
            add_edge(b,a,0);
        }
        int ans = maxflow();
        printf("%d\n",ans);
    }
    return 0;
}

POJ--1273--Drainage Ditches【Dinic】网络最大流

时间: 2024-11-05 19:38:37

POJ--1273--Drainage Ditches【Dinic】网络最大流的相关文章

NYOJ 323 &amp;&amp; HDU 1532 &amp;&amp; POJ 1273 Drainage Ditches (网络流之最大流入门)

链接:click here 题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量. 思路:最裸的网络流题目  意思就是求从源点到汇点的最大流. 第一道网络流,一边看着书上的介绍,一边敲下代码: 用的是网络流算法ford-fulkerson 题目数据量小,邻接表和邻接矩阵都可以过 代码: #include <ctype.h> //最大流 入门 #include <stdio.h> #include <vector> #include <stdlib

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

POJ 1273 Drainage Ditches(我的EK算法模板)

题意:给你n条边,目标位置t:接下来是每条边,包括起点,终点,容量: 感想:第一道最大流的代码,这道题是我更深地理解了Ek算法,不过这道题有个超坑的情况,那就是出现重边的情况==! 思路:EK算法 AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 #define N

POJ 1273 Drainage Ditches 网络流基础

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

poj 1273 Drainage Ditches(最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62708   Accepted: 24150 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

[POJ 1273] Drainage Ditches &amp; 最大流Dinic模板

Drainage Ditches 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