已 AC,求助为啥我的 dinic 跑的这么慢

P3376 【模板】网络最大流

Engulf @ 2022-08-01 21:42:12

rt,如果是数据改过说下

看到 EK 跑的都比我这个快,很是不解。

明早来看。

836ms

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 205, M = 5005;
int n, m, s, t, maxflow;
int dep[N], flow[M << 1];
struct edge
{
    int to, nxt;
}e[M << 1];
int head[N], idx = 1;
void add(int x, int y, int w) {e[ ++ idx] = {y, head[x]}, head[x] = idx, flow[idx] = w;}

bool bfs()
{
    queue<int> q;
    memset(dep, 0, sizeof dep);
    q.push(s), dep[s] = 1;
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (int i = head[u]; i; i = e[i].nxt)
        {
            int v = e[i].to;
            if (flow[i] && !dep[v])
            {
                dep[v] = dep[u] + 1;
                q.push(v);
            }
        }
    }
    return dep[t];
}
int dfs(int u, int in)
{
    if (u == t) return in;
    int out = 0;
    for (int i = head[u]; i; i = e[i].nxt)
    {
        int v = e[i].to;
        if (flow[i] && dep[v] == dep[u] + 1)
        {
            int fl = dfs(v, min(flow[i], in));
            flow[i] -= fl, flow[i ^ 1] += fl;
            in -= fl, out += fl;
        }
    }
    if (!out) dep[u] = 0;
    return out;
}

signed main()
{
    scanf("%lld%lld%lld%lld", &n, &m, &s, &t);
    for (int i = 1, x, y, w; i <= m; i ++ )
        scanf("%lld%lld%lld", &x, &y, &w), add(x, y, w), add(y, x, 0);
    while (bfs()) maxflow += dfs(s, 1e18);
    printf("%lld\n", maxflow);
    return 0;
}

by Engulf @ 2022-08-02 07:32:11

感谢上面所有大佬,现在跑的总算是正常了 /bx /bx


上一页 |