hdoj 3549 Flow Problem(最大网络流)

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

思路分析:该问题为裸的最大网络流问题,数据量不大,使用EdmondsKarp算法求解即可;需要注意的是该问题的点最多有15个,边的数目最多有1000个,所以该图中存在重边,需要将多个重边合为一条边;

代码如下:

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

const int MAX_N = 20;
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 case_times, case_id = 0;
    int road_num, ver_num;

    scanf("%d", &case_times);
    while (case_times--)
    {
        int ver_1, ver_2, capa;

        scanf("%d %d", &ver_num, &road_num);
        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("Case %d: %d\n", ++case_id, ans);
    }
    return 0;
}
时间: 2024-10-07 05:29:59

hdoj 3549 Flow Problem(最大网络流)的相关文章

hdoj 3549 Flow Problem【网络流最大流入门】

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 11405    Accepted Submission(s): 5418 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y

hdoj 3549 Flow Problem 【最大流】

题目:hdoj 3549 Flow Problem 题意:给出一个图,让你求最大流. 分析:这个题目用dinci写的,因为点比较少,而dinci复杂度O(m*n^2),但是还是跑了160ms,不知道15的神牛怎么写的. dinci的写法要注意的地方就是存图的时候要考虑怎么存,因为要更新网络残量,即反向的流量,所以这里要注意一下. 思想就不讲了,很多地方有讲. 代码: #include <cstdio> #include <cstring> #include <iostream

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 3549 Flow Problem(网络流模板题)

记录一下模板 #include <vector> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; #define maxn 1100 #define INF 0x7f7f7f7f struct Edge { int from,to,cap,flow; }; struct Dinic { int n,m,s

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

Geeks Ford-Fulkerson Algorithm for Maximum Flow Problem 最大网络流问题

很久之前就想攻克一下网络流的问题了,一直拖着,一是觉得这部分的内容好像非常高级,二是还有很多其他算法也需要学习,三是觉得先补补相关算法会好点 不过其实这虽然是图论比较高级的内容,但是基础打好了,那么还是不会太难的,而且它的相关算法并不多,熟悉图论之后就可以学习了,就算法不会二分图也可以学习. 这里使用Ford-Fulkerson算法,其实现的方法叫做:Edmonds-Karp Algorithm 其实两者前者是基本算法思想,后者是具体实现方法. 两个图十分清楚: 图1:原图,求这个图的最大网络流

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input