(网络流 模板 Dinic) Drainage Ditches --POJ --1273

链接:

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

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

const int MAXN = 1005;
const int oo = 1e9+7;

struct Edge
{
    int v, flow, next;
}edge[MAXN];
int Head[MAXN], cnt;
int Layer[MAXN];

void InIt()
{
    cnt = 0;
    memset(Head, -1, sizeof(Head));
}

void AddEdge(int u, int v, int flow)
{
    edge[cnt].v = v;
    edge[cnt].flow = flow;
    edge[cnt].next = Head[u];
    Head[u] = cnt++;

    swap(u, v);

    edge[cnt].v = v;
    edge[cnt].flow = 0;
    edge[cnt].next = Head[u];
    Head[u] = cnt++;

}

bool BFS(int start, int End)   // 分层
{
    memset(Layer, 0, sizeof(Layer));
    queue<int> Q;
    Q.push(start);
    Layer[start] = 1;

    while(Q.size())
    {
        int u = Q.front();Q.pop();

        if(u == End)return true;

        for(int j=Head[u]; j!=-1; j=edge[j].next)
        {
            int v = edge[j].v;

            if(Layer[v] == 0 && edge[j].flow) // 判断Layer[v]==0,可以减少循环
            {
                Layer[v] = Layer[u] + 1;
                Q.push(v);
            }
        }
    }

    return false;
}

int DFS(int u, int MaxFlow, int End)  // DFS返回的是这条路上的最大流(最小值)
{
    if(u == End)return MaxFlow;

    int uflow = 0;

    for(int j=Head[u]; j!=-1; j=edge[j].next)
    {
        int v = edge[j].v;

        if(Layer[v]==Layer[u]+1 && edge[j].flow)
        {
            int flow = min(edge[j].flow, MaxFlow-uflow);
            flow = DFS(v, flow, End);

            edge[j].flow -= flow;
            edge[j^1].flow += flow;
            uflow += flow;

            if(uflow == MaxFlow)
                break;
        }
    }

    if(uflow == 0)
        Layer[u] = 0;

    return uflow;
}

int Dinic(int start, int End)
{
    int MaxFlow = 0;

    while(BFS(start, End) == true)  // 先找出一条从Start到End的路
        MaxFlow += DFS(start, oo, End); 

    return MaxFlow;
}

int main()
{
    int N, M;

    while(scanf("%d%d", &M, &N) != EOF)
    {
        int u, v, flow;

        InIt();

        while(M--)
        {
            scanf("%d%d%d", &u, &v, &flow);
            AddEdge(u, v, flow);
        }

        printf("%d\n", Dinic(1, N));
    }

    return 0;
}
时间: 2024-10-18 23:02:22

(网络流 模板 Dinic) Drainage Ditches --POJ --1273的相关文章

Drainage Ditches (poj 1273 &amp;&amp; hdu 1532 网络流之Ford-Fulkerson)

Language: Default Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59676   Accepted: 22909 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clo

Drainage Ditches - poj 1273(网络流模板)

题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************** 先用Edmonds-Karp的算法做一下试试吧 重边贡献了 1W,要加上所有的重边才算是两点间最大流量 ************************************************************************* /*********************

XTU 二分图和网络流 练习题 J. Drainage Ditches

J. Drainage Ditches Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is cover

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)

UVA 820 --- POJ 1273 最大流

找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的时候注意一下. 而且我居然犯了更愚蠢的错误,以为重边的时候需要选最大的,正解应该是累加.... 1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 #define INF 999999 5 using n

POJ 1273 Drainage Ditches (网络流Dinic模板)

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(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

[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

POJ 1273 Drainage Ditches (dinic模板)

题目链接:http://poj.org/problem?id=1273 很经典的最大流问题,用此总结dinic模板 dinic比E-K多了个DFS,只要明白什么是把图分层了,就不难理解了.BFS找增广路的同时把图分层,相当于记录了多条增广路,可以让每次dinic能处理尽量多的增广路. 模板: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include &