第一节 网络最大流
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e4 + 50;
const int M = 2e5 + 50;
const int INF = 1e9;
int n, m, S, T;

int head[N], tot = 1;
struct Edge {
    int nex, to, w;
}e[M];
void addedge(int a, int b, int c) {
    e[++tot] = (Edge){head[a], b, c};
    head[a] = tot;
}
void add(int a, int b, int c) {
    addedge(a, b, c);
    addedge(b, a, 0);
}

struct DINIC {
    queue<int> q;
    int n, S, T;
    int dep[N], cur[N];

    void init(int _n, int _S, int _T) {
        n = _n; S = _S; T = _T;
        memset(head, 0, sizeof(head));
        tot = 1;
    }

    int bfs() {
        while (!q.empty()) q.pop();
        memset(dep, 0, sizeof(dep)); dep[S] = 1;
        q.push(S);
        while (!q.empty()) {
            int now = q.front(); q.pop();
            for (int i = head[now]; i; i = e[i].nex) {
                int to = e[i].to;
                if (!dep[to] && e[i].w) {
                    dep[to] = dep[now] + 1;
                    q.push(to);
                }
            }
        }
        return dep[T] > 0;
    }
    int dfs(int rt, int flow) {
        if (rt == T || !flow) return flow;
        for (int i = cur[rt]; i; i = e[i].nex) {
            cur[rt] = i;
            int to = e[i].to;
            if (dep[to] == dep[rt] + 1 && e[i].w) {
                int f = dfs(to, min(flow, e[i].w));
                if (f) {
                    e[i].w -= f;
                    e[i ^ 1].w += f;
                    return f;
                }
            }
        }
        return 0;
    }
    int Dinic() {
        int ans = 0;
        while (bfs()) {
            for (int i = 1; i <= n; i++) cur[i] = head[i];
            while (int tmp = dfs(S, INF)) {
                ans += tmp;
            }
        }
        return ans;
    }
}dinic;

int main() {
    scanf("%d%d%d%d", &n, &m, &S, &T);
    dinic.init(n, S, T);
    for (int i = 1, u, v, w; i <= m; i++) {
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w); add(v, u, 0);
    }
    printf("%d\n", dinic.Dinic());
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇