QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570819#9315. Rainbow Bracket SequencezeemanzWA 1ms3852kbC++203.3kb2024-09-17 18:05:082024-09-17 18:05:11

Judging History

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

  • [2024-09-17 18:05:11]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3852kb
  • [2024-09-17 18:05:08]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

template <class T> struct MCFGraph {
    static constexpr T inf = numeric_limits<T>::max() / 2;
    struct Edge {
        int v;
        T c, w;
        Edge(int v, T c, T w) : v{v}, c{c}, w{w} {}
    };
    int n;
    vector<Edge> e;
    vector<vector<int>> g;
    vector<T> dis;
    vector<int> vis, pre;
    MCFGraph() {}
    MCFGraph(int n) { init(n); }
    void init(int n) {
        this->n = n;
        e.clear();
        g.assign(n, {});
        dis.resize(n);
        vis.resize(n);
        pre.resize(n);
    }
    void addEdge(int u, int v, T c, T w) {
        g[u].push_back(e.size());
        e.emplace_back(v, c, w);
        g[v].push_back(e.size());
        e.emplace_back(u, 0, -w);
    }
    bool spfa(int s, int t) {
        dis.assign(n, inf);
        vis.assign(n, 0);
        pre.assign(n, -1);
        queue<int> que;
        dis[s] = 0;
        vis[s] = 1;
        que.push(s);
        while (!que.empty()) {
            int x = que.front();
            que.pop();
            vis[x] = 0;
            for (int i : g[x]) {
                auto [y, c, w] = e[i];
                if (c > 0 && dis[y] > dis[x] + w) {
                    dis[y] = dis[x] + w;
                    pre[y] = i;
                    if (!vis[y]) {
                        vis[y] = 1;
                        que.push(y);
                    }
                }
            }
        }
        return pre[t] != -1;
    }
    pair<T, T> flow(int s, int t) {
        T flow{}, cost{};
        while (spfa(s, t)) {
            T aug = inf;
            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 += aug * dis[t];
        }
        return {flow, cost};
    }
};

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> l(m);
    for (int i = 0; i < m; i++) {
        cin >> l[i];
    }

    vector<int> c(2 * n), v(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        cin >> c[i];
        c[i]--;
    }
    for (int i = 0; i < 2 * n; i++) {
        cin >> v[i];
    }

    vector<int> cnt(m);
    for (int i = 0; i < 2 * n; i++) {
        cnt[c[i]]++;
    }

    const int S = 3 * n + m, T = S + 1;
    MCFGraph<i64> mcf(T + 1);
    for (int i = 0; i < m; i++) {
        if (cnt[i] < l[i]) {
            cout << "-1\n";
            return;
        }
        mcf.addEdge(S, i, cnt[i] - l[i], 0);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 2 * i + 1; j <= n + i; j++) {
            mcf.addEdge(c[j], m + j, 1, 0);
            mcf.addEdge(m + j, m + 2 * n + i, 1, v[j]);
        }
        mcf.addEdge(m + 2 * n + i, T, 1, 0);
    }

    auto [x, y] = mcf.flow(S, T);
    if (x != n) {
        cout << "-1\n";
    } else {
        cout << accumulate(v.begin(), v.end(), 0LL) - y << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(16);

    int t = 1;
    cin >> t;

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

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3852kb

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: 3596kb

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:

-1
343766215
2351080746
4172047967
-1
-1
1351561190
2667742610
1013080942
4773704882
-1
-1
2231197660
2777995236
4360754300
4844097443
-1
1046749330
6655522033
5120267407
-1
4541488905
-1
2566553992
5453213598
6729655037
7934132672
-1
5054275471
1467678317
883992368
1100762323
-1
4081996785
-1
14472...

result:

wrong answer 4th numbers differ - expected: '3426965219', found: '4172047967'