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 <algorithm>
#define MAX 65535

struct node
{
    int e;
    int w;
    int fro;
}eg[400];

int head[400];     //正儿八经的前向星

using namespace std;

int cont;
void add(int s,int e,int w)   //加边
{
    eg[cont].e = e;                    //前向弧
    eg[cont].w = w;
    eg[cont].fro = head[s];
    head[s] = cont++;

    eg[cont].e = s;                     //反向弧
    eg[cont].w = 0;
    eg[cont].fro = head[e];
    head[e] = cont++;
    return ;
}

int dis[400];
int BFS(int s,int e)            //BFS找增光路 并且分层次
{
    int now,tmp;
    memset(dis,-1,sizeof (dis));

    queue<int> que;
    que.push(s);
    dis[s] = 0;

    while (!que.empty())
    {
        now = que.front();
        que.pop();

        for (int i = head[now];i != -1;i = eg[i].fro)
        {
            int v = eg[i].e;
            if (dis[v] == -1 && eg[i].w > 0)
            {
                dis[v] = dis[now] + 1;                  //分层
                que.push(v);
            }
        }
    }

    if (dis[e] != -1)
        return 1;

    return 0;
}

int dinic(int s,int e,int t)
{
    if (s == e)
        return t;

    int tmp = t;
    for (int i = head[s];i != -1;i = eg[i].fro)
    {
        int v = eg[i].e;
        if(dis[v] == dis[s] + 1 && eg[i].w > 0)
        {
            int imin = dinic(v,e,min(t,eg[i].w));                //递归找最小容量,其实就是个DFS
            eg[i].w -= imin;
            eg[i ^ 1].w += imin;

            t -= imin;
        }
    }

    return tmp - t;
}

int main()
{
    int n,m;

    while (~scanf ("%d%d",&n,&m))
    {
        int i;
        cont  = 0;
        memset(head,-1,sizeof (head));

        for (i = 0;i < n;i++)
        {
            int a,b,c;
             scanf ("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }

        int ans = 0;
        while (BFS(1,m))
            ans += dinic(1,m,MAX);

        printf ("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-05 03:39:18

POJ 1273 Drainage Ditches (dinic模板)的相关文章

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(网络流 最大流)

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 (网络最大流)

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 最大流

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

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(初识网络流)

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

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(最大流)

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 (网络流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 &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