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

(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. SpringCloud-Hystrix 服务降级、熔断

    Hystrix 是什么? Hystrix 是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时.异常等,Hystrix 能够保证在一个依赖出问题的情况下 ...

  2. Java中Random类

    Random:产生随机数的类 构造方法: public Random();没有给种子,用的是默认种子,是当前时间的毫秒值. public Random(long seed);给出指定的种子 //给定种 ...

  3. 基于Koa实现留言版demo

    学习node.koa,随手做了一个留言板demo. 基本功能如下: 未登录用户可以查看主题列表和主题内容. 用户注册和登录功能. 登录用户可以发表.修改.删除自己的主题. 登录用户主题列表下方有发表主 ...

  4. 理解RESTful API

    近日妹子向我求助RESTful API到底是个什么东西.原因是她们公司一个新启动的项目因为RESTful API起了争执.服务端同学坚持要用RESTful API,而前端同学则认为服务端用RESTfu ...

  5. 常用的python开发工具对比

    一名优秀的Python开发人员都有一套好用的Python开发工具,好的开发工具可以使Python开发人员的工作更高效,以下是几款比较好用的Python开发工具,Python开发人员,尤其是初学者,可以 ...

  6. 算法笔记刷题4(PAT B1009)

    这一题本来不应该有什么问题的,我很快写出来了,在dev c++里面运行也正常.但是放到pat以后出现了问题.更换了c/c++都不行通过编译. #include <cstdio> #incl ...

  7. thinkphp5 不使用form,用input+ajax异步上传图片

    不支持$this->request->file()获取图片 后台接收文件请使用$_FILE 正文开始: HTML <div class="upload"> ...

  8. 深拷贝、浅拷贝与Cloneable接口

    深拷贝与浅拷贝 浅拷贝 public class Student implements Cloneable{ Integer a; Integer b; @Override protected Obj ...

  9. HTML入门——互动式推送初尝试

    0.背景 疫情原因,导致许多大众喜闻乐见的体育活动停摆,但博主和队友们运营的体育社团公众号不能停摆.为了利用当下线上活动频率高的契机增加关注量,加之微信推送的互动性已成为趋势,博主打算和队友们尝试实现 ...

  10. socket编程-多个客户端向服务器发送人脸照片,服务器返回识别结果(服务器使用多线程)...

    recognition.py import numpy as np import face_recognition import os class recognition: def __init__( ...