POJ-1273-Drainage Ditches:网络流入门第一题

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define Size 200

int Gra[Size+1][Size+1];// 由于是单向边 所以一个矩阵可以表示一个带有反向边的残余网络
bool Visited[Size+1];
int Pre[Size+1]; // 前驱节点 用来形成一条从源点到汇点的路径
int N, M, S, E, C;// 输入数据

int EK()
{
        memset( Visited, 0, sizeof(Visited) );

        Pre[1] = 0;
        Visited[1] = true;
        queue<int>Que;
        bool FindPath = false; //  标记是否找到 一条增广路径
        Que.push(1);

        while( !Que.empty() )
        {
                int T = Que.front();
                Que.pop();

                for( int i=1; i<=M; i++ )// 广搜遍历所有顶点
                        if( Gra[T][i]>0 && !Visited[i] )
                        {
                                Pre[i] = T;
                                Visited[i] = true;
                                if( i==M )
                                {
                                        FindPath = true;
                                        Que.empty(); // 清空 以便 跳出while
                                        break;
                                }
                                else
                                    Que.push(i);
                        }
        }
        if( !FindPath )
            return 0;   // 未找到一条增广路径

        int RateFlow = 1<<30;
        int V = M;
        while( Pre[V] )  // 追溯 路径上的最小值 即最终汇流量
        {
                RateFlow = min( RateFlow, Gra[Pre[V]][V] );
                V = Pre[V];
        }
        V = M;
        while( Pre[V] )
        {
                Gra[Pre[V]][V] -= RateFlow;
                Gra[V][Pre[V]] += RateFlow;
                V = Pre[V];
        }

        return RateFlow;
}

int main()
{
        while( ~scanf( "%d%d", &N, &M ) )
        {
                memset( Gra, 0, sizeof(Gra) );
                for( int i=0; i<N; i++ )
                {
                        scanf( "%d%d%d", &S, &E, &C );
                        Gra[S][E] += C; // 有重边
                }
                int ans = 0;
                int n;
                while( n=EK() )
                    ans += n;
                printf( "%d\n", ans );
        }
        return 0;
}
时间: 2024-10-03 14:56:08

POJ-1273-Drainage Ditches:网络流入门第一题的相关文章

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 网络流基础

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 (网络流最大流基础 Edmonds_Karp算法)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59219   Accepted: 22740 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 (网络流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 网络流最大流基础

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

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

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足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 (网络最大流)

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