QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577395#9315. Rainbow Bracket SequenceKeeperHihiWA 1ms3672kbC++204.1kb2024-09-20 11:04:532024-09-20 11:04:55

Judging History

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

  • [2024-09-20 11:04:55]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3672kb
  • [2024-09-20 11:04:53]
  • 提交

answer

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

constexpr int inf = 1e9;
struct MCFGraph {
    struct Edge {
        int v, c, f;
        Edge(int v, int c, int f) : v(v), c(c), f(f) {}
    };
    const int n;
    vector<Edge> e;
    vector<vector<int>> g;
    vector<i64> h, dis;
    vector<int> pre;
    bool dijkstra(int s, int t) {
        dis.assign(n, numeric_limits<i64>::max());
        pre.assign(n, -1);
        priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> que;
        dis[s] = 0;
        que.emplace(0, s);
        while (!que.empty()) {
            i64 d = que.top().first;
            int u = que.top().second;
            que.pop();
            if (dis[u] < d) continue;
            for (int i : g[u]) {
                int v = e[i].v;
                int c = e[i].c;
                int f = e[i].f;
                if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
                    dis[v] = d + h[u] - h[v] + f;
                    pre[v] = i;
                    que.emplace(dis[v], v);
                }
            }
        }
        return dis[t] != numeric_limits<i64>::max();
    }
    MCFGraph(int n) : n(n), g(n) {}
    // c 是流量限制,f 是单位费用
    void addEdge(int u, int v, int c, int f) {
        if (f < 0) {
            g[u].push_back(e.size());
            e.emplace_back(v, 0, f);
            g[v].push_back(e.size());
            e.emplace_back(u, c, -f);
        } else {
            g[u].push_back(e.size());
            e.emplace_back(v, c, f);
            g[v].push_back(e.size());
            e.emplace_back(u, 0, -f);
        }
    }

    pair<int, i64> flow(int s, int t) {
        int flow = 0;
        i64 cost = 0;
        h.assign(n, 0);
        while (dijkstra(s, t)) {
            for (int i = 0; i < n; ++i) h[i] += dis[i];
            int aug = numeric_limits<int>::max();
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) aug = min(aug, e[pre[i]].c);
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
                e[pre[i]].c -= aug;
                e[pre[i] ^ 1].c += aug;
            }
            flow += aug;
            cost += i64(aug) * h[t];
        }
        return make_pair(flow, cost);
    }
};
// 点数 5e3,边数 5e4

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> l(m);
    vector<int> cnt(m);
    int tot = 0;
    for (int i = 0; i < m; i++) {
        cin >> l[i];
        tot += l[i];
    }
    vector<int> c(2 * n), v(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        cin >> c[i];
        c[i]--;
        cnt[c[i]]++;
    }
    int ans = 0;
    for (int i = 0; i < 2 * n; i++) {
        cin >> v[i];
        ans += v[i];
    }
    if (tot > n) {
        cout << "-1\n";
        return;
    }
    for (int i = 0; i < m; i++) {
        if (cnt[i] < l[i]) {
            cout << "-1\n";
            return;
        }
    }
    // 4 * n + 2 * n + m + 2
    int point = 4 * n + m + 50;
    MCFGraph F(point);
    int Z = 2 * n + 10;
    int C = 4 * n + 20;
    int S = point - 2, T = point - 1;
    for (int i = 0; i < m; i++) {
        F.addEdge(S, C + i, cnt[i] - l[i], 0);
        for (int j = 0; j < 2 * n; j++) {
            if (c[j] == i) {
                F.addEdge(C + i, j, 1, 0);
            }
        }
    }
    // for (int j = 0; j < 2 * n; j++) {
    //     F.addEdge(2 * j, 2 * j + 1, 1, v[j]);
    // }
    for (int i = 0; i < 2 * n; i++) {
        // for (int j = i; j < 2 * n; j++) {
        //     F.addEdge(i * 2 + 1, Z + j, 1, 0);
        // }
        // F.addEdge(Z + i, T, (i + 1) / 2, 0);
        F.addEdge(i, Z + i, 1, v[i]);
        F.addEdge(Z + i, Z + i + 1, (i + 1) / 2, 0);
    }
    F.addEdge(Z + 2 * n - 1, T, n, 0);

    auto [flow, cost] = F.flow(S, T);
    cout << ans - cost << "\n";
}   

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

input:

2
3 2
1 2
1 2 2 2 1 2
3 1 4 2 2 1
3 2
2 2
1 2 2 2 1 2
3 1 4 2 2 1

output:

9
-1

result:

ok 2 number(s): "9 -1"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3672kb

input:

50
8 6
0 2 2 0 3 1
3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6
998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375
1 1
1
1 1
343766215 374461155
3 1
2
1 1 1 1 1 1
796323508 303640854 701432076 853325162 610...

output:

6033465195
343766215
2351080746
3426965219
2649098145
-1
1351561190
2539318868
1013080942
4656646546
1529666207
5409850429
2231197660
2719131728
3983627641
4712174168
-1
1046749330
6115214757
3920988203
-1
3902088946
5222240184
2566553992
5268471900
5977120748
7505501534
-1
5054275471
1467678317
883...

result:

wrong answer 1st numbers differ - expected: '-1', found: '6033465195'