hdoj 1532 Drainage Ditches(最大网络流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532

思路分析:问题为最大网络流问题,给定一个有向图,需要求解该有向图的最大网络流,使用EdmondsKarp算法求解;

需要注意输入的边中可能有重边的存在;

代码如下:

#include <queue>
#include <vector>
#include <cstdio>
#include <climits>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 200 + 10;
int cap[MAX_N][MAX_N], flow[MAX_N][MAX_N];
int a[MAX_N], p[MAX_N];

inline int Min(int a, int b) { return a < b ? a : b; }
int EdmondsKarp(int ver_num)
{
    queue<int> q;
    int max_flow = 0;

    memset(flow, 0, sizeof(flow));
    for (;;)
    {
        memset(a, 0, sizeof(a));
        a[1] = INT_MAX;
        q.push(1);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            for (int v = 1; v <= ver_num; ++v)
            {
                if (!a[v] && cap[u][v] > flow[u][v])
                {
                    p[v] = u;
                    q.push(v);
                    a[v] = Min(a[u], cap[u][v] - flow[u][v]);
                }
            }
        }
        if (a[ver_num] == 0)  break;
        for (int u = ver_num; u != 1; u = p[u])
        {
            flow[p[u]][u] += a[ver_num];
            flow[u][p[u]] -= a[ver_num];
        }
        max_flow += a[ver_num];
    }
    return max_flow;
}

int main()
{
    int road_num, ver_num;

    while (scanf("%d %d", &road_num, &ver_num) != EOF)
    {
        int ver_1, ver_2, capa;

        memset(cap, 0, sizeof(cap));
        for (int i = 0; i < road_num; ++i)
        {
            scanf("%d %d %d", &ver_1, &ver_2, &capa);
            cap[ver_1][ver_2] += capa;
        }
        int ans = EdmondsKarp(ver_num);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-30 06:23:37

hdoj 1532 Drainage Ditches(最大网络流)的相关文章

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 最大排水量 网络最大流 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 &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'

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

HDU1532 Drainage Ditches (网络流)

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

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

hdu1532——Drainage Ditches(网络流模板)

Problem 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 d

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