网络流Ek算法

详细解释待.

紫书模板:

struct Edge {
    int from, to, cap, flow;
    Edge (int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) { }
};

struct EdmondsKarp {
    int n, m;
    vector<Edge> edges;
    vector<int>  G[maxn];
    int a[maxn];
    int p[maxn];

    void init(int n) {
        for (int i=0; i<n; ++i) G[i].clear();
        edges.clear();
    }

    void add(int u, int v , int val) {
        edges.push_back(Edge(u, v, val, 0));
        edges.push_back(Edge(u, v, 0, 0));
        m = edges.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }

    int Maxflow(int s, int t) {
        int flow = 0;
        while (1) {
            memset(a, 0, sizeof(a));
            queue<int> Q;
            Q.push(s);
            a[s] = inf;
            while (!Q.empty()) {
                int x = Q.front(); Q.pop();
                for (int i=0; i<G[x].size(); ++i) {
                    Edge &e = edges[G[x][i]];
                    if (!a[e.to] && e.cap > e.flow) {
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x], e.cap - e.flow);
                        Q.push(e.to);
                    }
                }
                if (a[t]) break;
            }
            if (!a[t]) break;
            for (int u = t; u!=s; u =edges[p[u]].from) {
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
            }
            flow += a[t];
        }
        return flow;
    }
}Ek;

原文地址:https://www.cnblogs.com/cgjh/p/9497453.html

时间: 2024-08-30 01:06:28

网络流Ek算法的相关文章

HDU 3549 基础网络流EK算法 Flow Problem

欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 10184    Accepted Submission(s): 4798 Problem Description Network flow is a well-known difficult pro

ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)

题意有点难读懂 //网络流-EK算法-ACM计算机工厂-构图重点 //Time:0Ms Memory:208K #include <iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #define MAXN 55 #define INF 0x3f3f3f3f int p,n; int s,t;

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流 Dinic EK 算法

E. Soldier and Traveling time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th ci

【转】网络最大流——EK算法详解

原文  http://blog.csdn.net/a1dark/article/details/11177907 EdmondsKarp算法,简称EK算法,O(m^2n) 因为是初学教程,所以我会尽量避免繁杂的数学公式和证明.也尽量给出了较为完整的代码.本文的目标群体是网络流的初学者,尤其是看了各种NB的教程也没看懂怎么求最大流的小盆友们.本文的目的是,解释基本的网络流模型,最基础的最大流求法,即bfs找增广路法,也就是EK法,全名是Edmond-Karp,其实我倒是觉得记一下算法的全名和来历可

POJ 1459 EK算法

题意: 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条:(0,1)20 (1,0)10 代表(起点,终点)最大传递的能量 (0)15 (产生能量的点)产生的最大能量(1)20 (消费能量的点)消费的最大能量 初学网络流,我想从基础练起:就先用EK算法写一遍 这道题看似很难,但其实只要加一个源点以及汇点,让所有的产生能量的点指向源点,让所有的消费能量的点指向汇点: #include

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

图论 最大流EK算法

今天接触了最大流,网上有很多ppt,耐心看下,再敲几遍代码大概就能懂意思了 EK 算法 关键是要理解要理解反悔的这个意思,因为每次当你选择了一种方式,但是这种方式不一定是最优的所以我们要再来建立一条反向边, 来完成反悔的策略 然后就是大概一直找增广路,改变最大的值,一直到找不到增广路为止 现在把模板的代码附上,并且给予注释 下面有两种方式一种是紫书上刘汝佳的代码,还有种是用链式前向星,还有种是dfs的方式,之后会继续补充 分别给出两种代码 struct Edge{ int from,to,cap

POJ1149_PIGS(网络流/EK)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15721   Accepted: 7021 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t