裸最大流,求最大流一般步骤如下:

(1)所有正向边权初始化为容量,反向边权初始化为0

(2)找增广路

(3)找到则进入(4),否则得到最大流并退出

(4) 增广路上所有边减去最小边权,相应的方向边加上最小边权,然后返回(2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <ctime>
#include <deque>
#include <queue>
using namespace std;
 
struct MaxFlow {
private:
    const static int maxn = 2e2 + 7;
    struct Edge {
        int u, v, w;
        Edge(int u = 0, int v = 0, int w = 0): u(u), v(v), w(w) {}
    };
    vector<vector<int> > G;
    vector<Edge> E;
    int S, T, maxflow;
    bool vis[maxn];
    int Q[maxn], fa[maxn], Y[maxn], head, tail, flow[maxn];
 
public:
    void init(int s, int t, int n) {
        G.clear();
        G.resize(n + 2);
        E.clear();
        S = s;
        T = t;
        maxflow = 0;
    }
    void add(int u, int v, int w) {
        E.push_back(Edge(u, v, w));
        E.push_back(Edge(v, u, 0));
        int sz = E.size();
        G[u].push_back(sz - 2);
        G[v].push_back(sz - 1);
    }
    bool bfs(int src) {
        head = tail = 0;
        memset(vis, 0, sizeof(vis));
        Q[tail ++] = src;
        flow[0] = 0x7fffffff;
        vis[src] = true;
        while (head < tail) {
            int node = Q[head ++];
            if (node == T) {
                maxflow += flow[head - 1];
                int p = head - 1;
                while (p) {
                    E[Y[p]].w -= flow[head - 1];
                    E[Y[p] ^ 1].w += flow[head - 1];
                    p = fa[p];
                }
                return true;
            }
            for (int i = 0; i < G[node].size(); i ++) {
                int e = G[node][i];
                if (!vis[E[e].v] && E[e].w) {
                    vis[E[e].v] = true;
                    fa[tail] = head - 1;
                    Y[tail] = e;
                    flow[tail] = min(flow[head - 1], E[e].w);
                    Q[tail ++] = E[e].v;
                }
            }
        }
        return false;
    }
    int solve() {
        while (bfs(S));
        return maxflow;
    }
} ;
MaxFlow solver;
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
#endif // ONLINE_JUDGE
    int n, m;
    while (cin >> m >> n) {
        solver.init(1, n, n);
        for (int i = 0; i < m; i ++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            solver.add(u, v, w);
        }
        cout << solver.solve() << endl;
    }
    return 0;
}

[hdu1532]最大流的更多相关文章

  1. HDU1532最大流 Edmonds-Karp,Dinic算法 模板

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  2. hdu1532 最大流板子题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目给出源点和漏点,还有一些边,要求源与漏之间的最大流,我采用了Edmonds Karp算法,该 ...

  3. hdu1532 (最大流入门,EK算法)

    看着这个博客 然后敲了hdu1532这个入门题,算是对最大流有点理解了 #include <stdio.h> #include <string.h> #include < ...

  4. 【最大流之EdmondsKarp算法】【HDU1532】模板题

    题意:裸的最大流,什么是最大流,参考别的博客 运用复杂度最高的EK算法 O(M*N),模板来自紫书 #include <cstdio> #include <cstdlib> # ...

  5. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

  6. 【最大流之ek算法】HDU1532 求最大流

    本来是继续加强最短路的训练,但是遇到了一个最短路 + 最大流的问题,最大流什么鬼,昨天+今天学习了一下,应该对ek算法有所了解,凭借学习后的印象,自己完成并ac了这个最大流的模板题 题目大意:都是图论 ...

  7. HDU-1532 Drainage Ditches (最大流,EK算法模板)

    题目大意:最大流的模板题...源点是0,汇点是n-1. 代码如下: # include<iostream> # include<cstdio> # include<cma ...

  8. POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu-1532 Drainage Ditches---最大流模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...

随机推荐

  1. Laravel项目Linux服务器部署

    laravel项目本地开发,一切正常.部署到服务器,首页都加载不出来,查了n多教程,各种方法姿势都试过了,还是不行. 功夫不负有心人,最后终于找到了问题所在,在此做个记录,铭记教训. 排查错误一定要: ...

  2. mongo基础

    以下如有任何问题,直接到官方操作文档左上角搜索框搜索 安装 On Windows, this path is on the drive from which you start MongoDB. Fo ...

  3. testNG 断言

    testNG提供一个Assert类,来判断输出值是否与预期值一致,Assert常用的方法有: Assert.assertEquals():此方法可以有两个参数值,也可以有3个参数值,参数的顺序是 ac ...

  4. 使用hexo和coding建立静态博客站点

    背景 由于工作性质的原因,做技术的总想记录和分享一下自己的学习和成长历程,向这世界证明我来过.写文章,发博客,一开始使用51cto,广告太多,看起来让人很痛苦:接着试用了博客园,广告少一些,但感觉还是 ...

  5. React Hooks: use modal

    useModal: export const useModal = (initTitle: string, initContent: string | React.ReactElement) => ...

  6. tensorflow1.0 placeholder占位符

    import tensorflow as tf #(tf.float32,[2,2]) input1 = tf.placeholder(tf.float32) input2 = tf.placehol ...

  7. Solr搜索结果高级设置

    一.选择响应格式 XML是Solr的默认响应格式.从Solr的角度看,什么样的响应格式并不重要.Solr可以返回XML.JSON.Ruby.Python.PHP.二进制Java等,甚至是自定义格式.使 ...

  8. <string>头文件常用成员函数

    之前说过 string和vector一样,也是一种顺序容器,并且它也自带成员函数,用法和vector的成员函数差不多,不过它只能用来存放字符,也就是字符串. 在c++中,<string>基 ...

  9. thinkphp5.0 配置文件加载路径说明

    在thinphp5.0框架里,js,css等配置文件都是加载在/public/static的目录下,所以要引用这些文件,路径必须是要写好的,代码如图: return [ // 默认模块名 'defau ...

  10. umditor删除域名,配置为绝对路径

    getAllPic: function (sel, $w, editor) { var me = this, arr = [], $imgs = $(sel, $w); $.each($imgs, f ...