QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#342374#602. 最小费用最大流(随机数据)Xiaohuba#100 ✓219ms3904kbC++237.9kb2024-03-01 10:59:122024-03-01 10:59:13

Judging History

你现在查看的是最新测评结果

  • [2024-03-01 10:59:13]
  • 评测
  • 测评结果:100
  • 用时:219ms
  • 内存:3904kb
  • [2024-03-01 10:59:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
template <class T ENABLE_IF_INT>
constexpr T INF = numeric_limits<T>::max() >> 1;
#endif

#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i)     // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
template<typename T> constexpr il T sq(const T &x) {
    return x * x;
}
template<typename T> CONSTEXPR_FUNC il void cmin(T &x, const T &y) {
    x = min(x, y);
}
template<typename T> CONSTEXPR_FUNC il void cmax(T &x, const T &y) {
    x = max(x, y);
}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y, T mod) {
    T ans = 1;
    x %= mod;

    while (y) {
        if (y & 1)
            (ans *= x) %= mod;

        (x *= x) %= mod;
        y >>= 1;
    }

    return ans;
}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y) {
    T ans = 1;

    while (y) {
        if (y & 1)
            ans *= x;

        x *= x;
        y >>= 1;
    }

    return ans;
}
template<typename T ENABLE_IF_INT> il void read(T &x) {
    x = 0;
    int f = 1;
    int c = getchar();

    while (!isdigit(c)) {
        if (c == '-')
            f = -1;

        c = getchar();
    }

    while (isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }

    x *= f;
}
template<typename T, typename ... Args> il void read(T &x, Args &... y) {
    read(x);
    read(y...);
}
// clang-format on

// File head end

class MCMF {
    struct Edge {
        int v, cap, flow, wi;
        Edge() : v(0), cap(0), flow(0), wi(0) {}
        Edge(int _v, int _c, int _f, int _w) : v(_v), cap(_c), flow(_f), wi(_w) {}
    };
    vector<int> H, dis, dep, cur;
    vector<vector<int>> G;
    vector<Edge> E;
    int s, t;

    il bool dijkstra() {
        static priority_queue<pii, vector<pii>, greater<>> pq;

        while (!pq.empty())
            pq.pop();

        fill(dis.begin(), dis.end(), INF<int>);
        pq.emplace(0, s), dis[s] = 0;

        while (!pq.empty()) {
            auto [di, u] = pq.top();
            pq.pop();

            if (di != dis[u])
                continue;

            // cerr << u << '\n';
            for (auto i : G[u]) {
                int v = E[i].v, w = E[i].wi + H[u] - H[v];

                if (E[i].cap > E[i].flow && dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    pq.emplace(dis[v], v);
                }
            }
        }

        for (int i = 0; i < H.size(); i++)
            H[i] += dis[i];

        // cerr << "> " << H[s] << ' ' << H[t] << '\n';
        return dis[t] < INF<int>;
    }
    il bool BFS() {
        static queue<int> q;

        while (!q.empty())
            q.pop();

        fill(dep.begin(), dep.end(), -1);
        q.emplace(s), dep[s] = 0;

        while (!q.empty()) {
            int u = q.front();
            q.pop();

            // cerr << "? " << u << '\n';
            for (int i : G[u]) {
                int v = E[i].v;

                if (E[i].cap > E[i].flow && H[v] == H[u] + E[i].wi && !(~dep[v])) {
                    dep[v] = dep[u] + 1;
                    q.emplace(v);
                }
            }
        }

        fill(cur.begin(), cur.end(), 0);
        return ~dep[t];
    }
    int DFS(int x, int flow) {
        if (x == t || !flow)
            return flow;

        // cerr << "D " << x << '\n';
        int ans = 0;

        for (int &i = cur[x]; i < G[x].size(); i++) {
            int e = G[x][i], v = E[e].v;

            if (E[e].cap > E[e].flow && H[v] == H[x] + E[e].wi &&
                    dep[v] == dep[x] + 1) {
                int fl = DFS(v, min(flow, E[e].cap - E[e].flow));
                ans += fl, flow -= fl;
                E[e].flow += fl, E[e ^ 1].flow -= fl;

                if (!flow)
                    break;
            }
        }

        return ans;
    }

public:
    MCMF() = default;
    MCMF(int __n) {
        resize(__n);
    }
    il void clear() {
        fill(H.begin(), H.end(), 0);
        fill(dis.begin(), dis.end(), 0);
        fill(dep.begin(), dep.end(), 0);
        fill(cur.begin(), cur.end(), 0);
        for_each(G.begin(), G.end(), [&](auto & x) {
            x.clear();
        });
        E.clear();
    }
    il void resize(int __n) {
        H.resize(__n), dis.resize(__n), dep.resize(__n), cur.resize(__n),
                 G.resize(__n);
        clear();
    }
    il void addEdge(int u, int v, int cap, int wi) {
        assert(u >= 0 && u < G.size());
        assert(v >= 0 && v < G.size());
        G[u].eb(E.size()), E.eb(v, cap, 0, wi);
        G[v].eb(E.size()), E.eb(u, 0, 0, -wi);
    }
    il void set_initial_H(int L, int R, int val) {
        fill(H.begin() + L, H.begin() + R, val);
    }
    il vector<int> solve(int __s, int __t, int val_range) {
        // cerr << "S " << __s << ' ' << __t << '\n';
        s = __s, t = __t;
        assert(s >= 0 && s < G.size());
        assert(t >= 0 && t < G.size());
        vector<int> ans(val_range + 1);
        dijkstra();

        while (dijkstra()) {
            int res = 0;

            while (BFS())
                res += DFS(s, INF<int>);

            ans[H[s] - H[t]] += res;
        }

        return ans;
    }
    il pii solve2(int __s, int __t) {
        s = __s, t = __t;
        assert(s >= 0 && s < G.size());
        assert(t >= 0 && t < G.size());
        bool qwq = dijkstra();
        int flow = 0, cost = 0;

        while (dijkstra()) {
            int res = 0;

            while (BFS())
                res += DFS(s, INF<int>);

            flow += res, cost += (H[t] - H[s]) * res;
        }

        return {flow, cost};
    }
};

namespace {
int n, m, s, t;
il void Main2() {
    read(n, m), s = 2 * n, t = 2 * n + 1;
    MCMF solver(2 * n + 2);
    For(i, 0, m - 1) {
        int u, v, w;
        read(u, v, w);
        solver.addEdge(u - 1, n + v - 1, 1, -w);
    }
    For(i, 0, n - 1) solver.addEdge(s, i, 1, 0), solver.addEdge(i + n, t, 1, 0);
    auto ans = solver.solve(s, t, 5);
    int res = n, tmp = 0;

    ForDown(i, 5, 1) while (ans[i]--)
        printf("%d\n", (tmp += i)), res--;

    while (res--)
        printf("%d\n", tmp);
}
il void Main() {
    read(n, m), s = 0, t = n - 1;
    MCMF solver(n);
    For(i, 0, m - 1) {
        int u, v, w, c;
        read(u, v, c, w);
        solver.addEdge(u - 1, v - 1, c, w);
    }
    auto ans = solver.solve2(s, t);
    cout << ans.fi << ' ' << ans.se << '\n';
}
} // namespace

signed main() {
    return Main(), 0;
}

詳細信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 3592kb

input:

8 27
2 3 2147483647 100
1 3 1 100
2 4 2147483647 10
1 4 1 10
2 4 2147483647 10
1 4 1 10
2 8 3 0
3 5 2147483647 100
1 5 1 100
3 8 1 0
3 2 2147483647 0
4 5 2147483647 10
1 5 1 10
4 8 1 0
4 2 2147483647 0
5 6 2147483647 1
1 6 1 1
5 6 2147483647 1
1 6 1 1
5 7 2147483647 1
1 7 1 1
5 8 3 0
5 2 2147483647 ...

output:

8 243

result:

ok 2 number(s): "8 243"

Test #2:

score: 10
Accepted
time: 1ms
memory: 3828kb

input:

12 49
2 10 2147483647 5
1 10 1 5
2 5 2147483647 50
1 5 1 50
2 9 2147483647 8
1 9 1 8
2 8 2147483647 47
1 8 1 47
2 11 2147483647 17
1 11 1 17
2 12 5 0
3 12 0 0
3 2 2147483647 0
4 6 2147483647 18
1 6 1 18
4 11 2147483647 12
1 11 1 12
4 9 2147483647 14
1 9 1 14
4 12 3 0
4 2 2147483647 0
5 11 2147483647...

output:

15 436

result:

ok 2 number(s): "15 436"

Test #3:

score: 10
Accepted
time: 1ms
memory: 3644kb

input:

27 169
2 15 2147483647 24
1 15 1 24
2 19 2147483647 96
1 19 1 96
2 12 2147483647 49
1 12 1 49
2 13 2147483647 75
1 13 1 75
2 24 2147483647 2
1 24 1 2
2 27 5 0
3 27 0 0
3 2 2147483647 0
4 11 2147483647 99
1 11 1 99
4 3 2147483647 85
1 3 1 85
4 27 2 0
4 2 2147483647 0
5 27 0 0
5 2 2147483647 0
6 9 214...

output:

60 4338

result:

ok 2 number(s): "60 4338"

Test #4:

score: 10
Accepted
time: 7ms
memory: 3660kb

input:

77 2149
2 42 2147483647 33
1 42 1 33
2 68 2147483647 30
1 68 1 30
2 76 2147483647 13
1 76 1 13
2 51 2147483647 93
1 51 1 93
2 12 2147483647 39
1 12 1 39
2 57 2147483647 74
1 57 1 74
2 70 2147483647 21
1 70 1 21
2 73 2147483647 24
1 73 1 24
2 52 2147483647 54
1 52 1 54
2 15 2147483647 99
1 15 1 99
2 ...

output:

1000 74606

result:

ok 2 number(s): "1000 74606"

Test #5:

score: 10
Accepted
time: 23ms
memory: 3656kb

input:

102 4199
2 48 2147483647 42
1 48 1 42
2 85 2147483647 50
1 85 1 50
2 22 2147483647 83
1 22 1 83
2 95 2147483647 97
1 95 1 97
2 82 2147483647 34
1 82 1 34
2 25 2147483647 72
1 25 1 72
2 4 2147483647 17
1 4 1 17
2 47 2147483647 10
1 47 1 10
2 71 2147483647 12
1 71 1 12
2 68 2147483647 39
1 68 1 39
2 2...

output:

2000 161420

result:

ok 2 number(s): "2000 161420"

Test #6:

score: 10
Accepted
time: 22ms
memory: 3904kb

input:

102 4199
2 79 2147483647 13
1 79 1 13
2 83 2147483647 73
1 83 1 73
2 75 2147483647 90
1 75 1 90
2 30 2147483647 92
1 30 1 92
2 54 2147483647 25
1 54 1 25
2 66 2147483647 53
1 66 1 53
2 52 2147483647 37
1 52 1 37
2 63 2147483647 46
1 63 1 46
2 11 2147483647 20
1 11 1 20
2 55 2147483647 53
1 55 1 53
2...

output:

2000 143072

result:

ok 2 number(s): "2000 143072"

Test #7:

score: 10
Accepted
time: 21ms
memory: 3904kb

input:

102 4199
2 39 2147483647 45
1 39 1 45
2 51 2147483647 11
1 51 1 11
2 86 2147483647 63
1 86 1 63
2 23 2147483647 46
1 23 1 46
2 48 2147483647 63
1 48 1 63
2 87 2147483647 8
1 87 1 8
2 73 2147483647 63
1 73 1 63
2 5 2147483647 52
1 5 1 52
2 80 2147483647 21
1 80 1 21
2 31 2147483647 44
1 31 1 44
2 101...

output:

2000 146132

result:

ok 2 number(s): "2000 146132"

Test #8:

score: 10
Accepted
time: 163ms
memory: 3716kb

input:

302 10599
2 72 2147483647 169
1 72 1 169
2 260 2147483647 165
1 260 1 165
2 12 2147483647 108
1 12 1 108
2 16 2147483647 26
1 16 1 26
2 28 2147483647 148
1 28 1 148
2 7 2147483647 74
1 7 1 74
2 139 2147483647 199
1 139 1 199
2 231 2147483647 9
1 231 1 9
2 287 2147483647 123
1 287 1 123
2 135 2147483...

output:

5000 1106316

result:

ok 2 number(s): "5000 1106316"

Test #9:

score: 10
Accepted
time: 219ms
memory: 3804kb

input:

302 10599
2 222 2147483647 132
1 222 1 132
2 17 2147483647 7
1 17 1 7
2 177 2147483647 253
1 177 1 253
2 90 2147483647 195
1 90 1 195
2 128 2147483647 289
1 128 1 289
2 42 2147483647 193
1 42 1 193
2 213 2147483647 133
1 213 1 133
2 263 2147483647 293
1 263 1 293
2 50 2147483647 155
1 50 1 155
2 228...

output:

5000 1290871

result:

ok 2 number(s): "5000 1290871"

Test #10:

score: 10
Accepted
time: 211ms
memory: 3768kb

input:

302 10599
2 176 2147483647 289
1 176 1 289
2 190 2147483647 99
1 190 1 99
2 10 2147483647 96
1 10 1 96
2 240 2147483647 165
1 240 1 165
2 273 2147483647 205
1 273 1 205
2 248 2147483647 194
1 248 1 194
2 220 2147483647 122
1 220 1 122
2 194 2147483647 167
1 194 1 167
2 8 2147483647 67
1 8 1 67
2 227...

output:

5000 1395897

result:

ok 2 number(s): "5000 1395897"