QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#689453#9315. Rainbow Bracket SequencergrgtgrfWA 1ms3640kbC++234.7kb2024-10-30 17:09:072024-10-30 17:09:08

Judging History

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

  • [2024-10-30 17:09:08]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3640kb
  • [2024-10-30 17:09:07]
  • 提交

answer

#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
template<class T>
struct MinCostFlow {
    struct _Edge {
        int to;
        T cap;
        T cost;
        _Edge(int to_, T cap_, T cost_) : to(to_), cap(cap_), cost(cost_) {}
    };
    int n;
    std::vector<_Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<T> h, dis;
    std::vector<int> pre;
    bool dijkstra(int s, int t) {
        dis.assign(n, std::numeric_limits<T>::max());
        pre.assign(n, -1);
        std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> que;
        dis[s] = 0;
        que.emplace(0, s);
        while (!que.empty()) {
            T 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].to;
                T cap = e[i].cap;
                T cost = e[i].cost;
                if (cap > 0 && dis[v] > d + h[u] - h[v] + cost) {
                    dis[v] = d + h[u] - h[v] + cost;
                    pre[v] = i;
                    que.emplace(dis[v], v);
                }
            }
        }
        return dis[t] != std::numeric_limits<T>::max();
    }
    MinCostFlow() {}
    MinCostFlow(int n_) {
        init(n_);
    }
    void init(int n_) {
        n = n_;
        e.clear();
        g.assign(n, {});
    }
    void addEdge(int u, int v, T cap, T cost) {
        g[u].push_back(e.size());
        e.emplace_back(v, cap, cost);
        g[v].push_back(e.size());
        e.emplace_back(u, 0, -cost);
    }
    std::pair<T, T> flow(int s, int t) {
        T flow = 0;
        T cost = 0;
        h.assign(n, 0);
        while (dijkstra(s, t)) {
            for (int i = 0; i < n; ++i) {
                h[i] += dis[i];
            }
            T aug = std::numeric_limits<int>::max();
            for (int i = t; i != s; i = e[pre[i] ^ 1].to) {
                aug = std::min(aug, e[pre[i]].cap);
            }
            for (int i = t; i != s; i = e[pre[i] ^ 1].to) {
                e[pre[i]].cap -= aug;
                e[pre[i] ^ 1].cap += aug;
            }
            flow += aug;
            cost += aug * h[t];
        }
        return std::make_pair(flow, cost);
    }
    struct Edge {
        int from;
        int to;
        T cap;
        T cost;
        T flow;
    };
    std::vector<Edge> edges() {
        std::vector<Edge> a;
        for (int i = 0; i < e.size(); i += 2) {
            Edge x;
            x.from = e[i + 1].to;
            x.to = e[i].to;
            x.cap = e[i].cap + e[i + 1].cap;
            x.cost = e[i].cost;
            x.flow = e[i + 1].cap;
            a.push_back(x);
        }
        return a;
    }
};
const int inf = 1e9;
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);
    for(int i = 0; i < 2 * n; i++) {
        cin >> c[i];
        c[i] -= 1;
    }
    vector<int> val(2 * n);
    for(int i = 0; i < 2 * n; i++) {
        cin >> val[i];
    }

    MinCostFlow<ll> flow(m + 2 * n + 4);
    int s = m + 2 * n, t = s + 1, ns = t + 1, nt = ns + 1;
    const int N = m + 2 * n;
    vector<int> d(N + 2);
    for(int i = 0; i < 2 * n; i++) {
        flow.addEdge(c[i], i + m, 1, -val[i]);
    }


    for(int i = 0; i < 2 * n - 1; i++) {
        flow.addEdge(i + m, i + m + 1, n, 0);
        d[i + m + 1] += 1 + i / 2;
        d[i + m] -= 1 + i / 2;
    }
    d[t] += n;
    d[m + 2 * n - 1] -= n;
    flow.addEdge(s, t, inf, 0);

    for(int i = 0; i < m; i++) {
        flow.addEdge(s, i, n, 0);
        d[s] -= l[i];
        d[i] += l[i];
    }

    for(int i = 0; i < N + 2; i++) {
        if(d[i] > 0) {
            flow.addEdge(ns, i, d[i], 0);
        }
        if(d[i] < 0) {
            flow.addEdge(i, nt, -d[i], 0);
        }
    }



    auto [fl, co] = flow.flow(ns, nt);
    auto edges = flow.edges();
    // for(auto [u, v, ca, co, fl] : edges) {
    //     if(fl != 0 && u == ns) {
    //         cout << "QAQ: " << u << " " << v << " " << fl << endl;
    //     }
    //     cout << u << " " << v << " " << ca << " " << co << " " << fl <<endl;
    // }
    for(auto [u, v, ca, co, fl] : edges) {
        if(u == ns && v < m && fl < l[v]) {
            cout << -1 << "\n";
            return;
        }
    }
    cout << -co << "\n";
}
signed main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int tt = 1;
    std::cin >> tt;
    while (tt--)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
1649648670
1812906175
-1
-1
831452391
2539318868
1013080942
1097019708
-1
-1
2231197660
758225925
0
2812012416
-1
841935645
5689642167
1345740508
-1
1988904333
-1
999388067
5268471900
0
1375278074
-1
4236865447
0
883992368
839769227
-1
3576403472
-1
856364894
2713025914
299682359
495542...

result:

wrong answer 3rd numbers differ - expected: '2351080746', found: '1649648670'