HDU 1532 Drainage Ditches EK算法

题意:输入m n,   m是边数,n是点数。 接下来m行:   起点,终点,容量。求以 1 为源点, n为汇点的最大流。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

const int INF = 0xfffffff;
const int MAXN = 200 + 10;

//邻接矩阵存放图。
int flow[MAXN][MAXN];
//mark[]标记是否访问过,pre[]记录增广路。
int mark[MAXN], pre[MAXN];
int m, n, f; //f为最大流。

void max_flow()
{
    //不断寻找增广路,知道找不到为止,找不到的标志为 mark[n]==0.
    while(1) {
        memset(mark, 0, sizeof(mark));
        memset(pre, 0, sizeof(pre));

        queue<int> Q;
        mark[1] = 1;
        Q.push(1);
        while( !Q.empty() ) {
            int cnt = Q.front();
            Q.pop();
            //找到增广路,跳出。
            if(cnt == n) {
                break;
            }

            for(int i = 1; i <= n; i++) {
                if(!mark[i] && flow[cnt][i] > 0) {
                    mark[i] = 1;
                    Q.push(i);
                    pre[i] = cnt;
                }
            }
        }

        //如果没找到可增广的路,直接跳出.
        if( !mark[n] ) {
            break;
        }

        //计算该增广路最大可增加的流量.
        int minx = INF;
        for(int i = n; i != 1; i = pre[i]) {
            minx = min( flow[pre[i]][i], minx );
        }

        for(int i = n; i != 1; i = pre[i]) {
            flow[pre[i]][i] -= minx; //更新正向流量。
            flow[i][pre[i]] += minx; //更新反向流量。
        }
        f += minx;
    }
}

int main()
{
    while(scanf("%d %d", &m, &n) != EOF) {
        memset(flow, 0, sizeof(flow));
        for(int i = 0; i < m; i++) {
            int u, v, len;
            scanf("%d %d %d", &u, &v, &len);
            //这个题有重边的情况,所以要flow[u][v] += len;直接等于过不去。
            flow[u][v] += len;
        }
        f = 0;
        max_flow();
        printf("%d\n", f);
    }
    return 0;
}
时间: 2024-12-13 02:23:03

HDU 1532 Drainage Ditches EK算法的相关文章

HDU 1532 Drainage Ditches 最大排水量 网络最大流 Edmonds_Karp算法

题目链接:HDU 1532 Drainage Ditches 最大排水量 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9641    Accepted Submission(s): 4577 Problem Description Every time it rains on Farmer John

hdu 1532 Drainage Ditches(最大流)

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

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10875    Accepted Submission(s): 5131 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

HDU 1532 Drainage Ditches (网络流)

A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 wate

HDU 1532 Drainage Ditches (最大网络流)

Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Every time it rains on

HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ

hdu 1532 Drainage Ditches(edmond-karp最大流算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 #include <stdio.h> #include <queue> #include <string.h> #include <algorithm> #define INT_MAX (int)1e9 using namespace std; const int N = 207; int network[N][N], pre[N], used[N], f

hdu - 1532 Drainage Ditches (最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=1532 求最大的流量,用dinic算法就好. 1 // Rujia Liu 2 // 因为图较大,所以采用Dinic而不是EdmondsKarp 3 // 得益于接口一致性,读者无须理解Dinic就能使用它. 4 #include<cstdio> 5 #include<cstring> 6 #include<queue> 7 #include<algorithm> 8 us

HDU 1532 Drainage Ditches

http://acm.hdu.edu.cn/showproblem.php?pid=1532 基础题. 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 int n, m, flow; 9 int vis[205]; 10 //路径记录 11 i