Dinic算法_网络最大流

Dinic算法_网络最大流

Python version

class Dinic:
    '''Dinic algorithm'''
    def __init__(self, V:int, E:int, graph:'List[List[int]]', S:int, T:int):
        self.n, self.m = V, E  # V = |Vertices|, E = |Edges|
        self.S, self.T = S, T
        self.w = [0 for _ in range(2 * (E + 1))]
        self.nxt = [0 for _ in range(2 * (E + 1))]
        self.g = [[] for _ in range(V + 1)]
        idx = 0
        for u, v, w in graph:
            self.w[idx] = w
            self.nxt[idx] = v
            self.g[u].append(idx)
            idx += 1
            self.w[idx] = 0
            self.nxt[idx] = u
            self.g[v].append(idx)
            idx += 1
        self.dis = None
        self.ans = 0


    def BFS(self) -> bool:
        self.dis = [0 for _ in range(self.n + 1)]
        self.dis[self.S], q = 1, [self.S]
        while q:
            now = q.pop(0)
            for to in self.g[now]:
                if not self.dis[self.nxt[to]] and self.w[to]:
                    q.append(self.nxt[to])
                    self.dis[self.nxt[to]] = self.dis[now] + 1
        return bool(self.dis[self.T])


    def DFS(self, u:int, flow:int):
        if u == self.T:
            return flow
        for to in self.g[u]:
            if self.dis[self.nxt[to]] == self.dis[u] + 1 and self.w[to]:
                minFlow = self.DFS(self.nxt[to], min(flow, self.w[to]))
                if minFlow:
                    self.w[to] -= minFlow
                    self.w[to ^ 1] += minFlow
                    return minFlow
        return 0


    def dinic(self):
        sum, inf = 0, int(1e9)
        while self.BFS():
            sum += self.DFS(self.S, inf)
        print("Maximum Flow:", sum)


if __name__ == '__main__':
    n, m = 6, 9
    g = [[1, 2, 16], [1, 3, 13], [3, 2, 4],
         [2, 4, 12], [4, 3, 9], [3, 5, 14],
         [5, 4, 7], [4, 6, 20], [5, 6, 4]]
    dinic = Dinic(n, m, g, 1, n)
    dinic.dinic()

在这里插入图片描述

C++ version

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

const int N = 10005, M = 20005, INF = 0x3f3f3f3f;
int n, m, x, y, weight, idx = 0;
int w[M * 2], nxt[M * 2], dis[N], S = 1, T, ans = 0;
vector<int> g[N];//存储边的序号:g[u]代表从结点u出发的边的序号
//w[i]存储第i条边e_i的权重,w[i ^ 1]存储e_i的反向边的权重
//dis存储分层的序号,nxt[i]代表第i条边的指向结点

bool BFS()
{
    memset(dis, 0, sizeof(dis));
    queue<int> q;
    q.push(S);
    dis[S] = 1;
    while (!q.empty()){
        int from = q.front();
        q.pop();
        for (int i = 0; i < g[from].size(); ++i) {
            int to = g[from][i];
            if (!dis[nxt[to]] && w[to]){
                q.push(nxt[to]);
                dis[nxt[to]] = dis[from] + 1;
            }
        }
    }
    return bool(dis[T]);
}


int Dinic(int u, int flow)
{
    if (u == T)
        return flow;
    for (int i = 0; i < g[u].size(); ++i) {
        int to = g[u][i];
        if (dis[nxt[to]] == dis[u] + 1 && w[to]){
            int minFlow = Dinic(nxt[to], min(flow, w[to]));
            if (minFlow){
                w[to] -= minFlow;
                w[to ^ 1] += minFlow;
                return minFlow;
            }
        }
    }
    return 0;
}



int main()
{
    ios_base::sync_with_stdio(false);
    cin >> m >> n;
    S = 1, T = n;
    for (int i = 0; i < m; ++i) {
        cin >> x >> y >> weight;
        w[idx] = weight;
        nxt[idx] = y;
        g[x].push_back(idx++);
        w[idx] = 0; //构造反向边
        nxt[idx] = x;
        g[y].push_back(idx++);
    }//create graph
    while (BFS())
        ans += Dinic(S, INF);
    cout << "Maximum Flow: " << ans << endl;
    return 0;
}

测试数据

9 6
1 2 16
1 3 13
3 2 4
2 4 12
4 3 9
3 5 14
5 4 7
4 6 20
5 6 4
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值