QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570273#9315. Rainbow Bracket Sequencesanbi52WA 1ms3592kbC++173.8kb2024-09-17 15:04:332024-09-17 15:04:38

Judging History

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

  • [2024-09-17 15:04:38]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3592kb
  • [2024-09-17 15:04:33]
  • 提交

answer

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

typedef long long ll;

const ll INF = 1e14;
        
template <class T, class C>
struct MCMF{
    int n, m, s, t;
    struct Edge{
        int from, to;
        T cap, flow; 
        C cost;
    };
    vector<vector<int>> node;
    vector<Edge> edges;
    vector<int> inq;
    vector<int> p;
    vector<C> d;
    vector<T> a;
    vector<C> h;
    MCMF(){}
    MCMF(int n) : node(n), p(n), inq(n), d(n), a(n), h(n){
        this -> n = n;
    }
    void add_edge(int from, int to, T cap, C cst){
        edges.push_back(Edge{from, to, cap, 0, cst});
        edges.push_back(Edge{to, from, 0, 0, -cst});
        m = edges.size();
        node[from].push_back(m - 2);
        node[to].push_back(m - 1);
    }
    void SPFA(){
        h.assign(n, INF);
        h[s] = 0;
        queue<int> qe;
        vector<char> inque(n);
        qe.push(s);
        while(!qe.empty()){
            int u = qe.front();
            qe.pop();
            for(int i = 0; i < node[u].size(); i++){
                Edge &e = edges[node[u][i]];
                if(e.cap > e.flow && h[e.to] > h[u] + e.cost){
                    if(!inque[e.to])qe.push(e.to);
                    inque[e.to] = 1;
                    h[e.to] = h[u] + e.cost;
                }
            }
        }
    }
    bool Dijkstra(T &flow, C &cost){
        d.assign(n, INF);
        d[s] = 0;
        a[s] = INF;
        set<pair<C, int>> qe;
        qe.emplace(d[s], s);

        while(!qe.empty()){
            auto [dis, u] = *qe.begin();
            qe.erase(*qe.begin());

            for(int i = 0; i < node[u].size(); i++){
                Edge &e = edges[node[u][i]];
                if(e.cap > e.flow && d[e.to] > d[u] + e.cost + h[u] - h[e.to]){
                    qe.erase({d[e.to], e.to});
                    d[e.to] = d[u] + e.cost + h[u] - h[e.to];
                    p[e.to] = node[u][i];
                    qe.insert({d[e.to], e.to});
                    a[e.to] = min(a[u], e.cap - e.flow);
                }
            }
        }

        if(d[t] >= INF)return false;
        flow += a[t];
        cost += (d[t] - h[s] + h[t]) * a[t];
        int u = t;
        while(u != s){
            edges[p[u]].flow += a[t];
            edges[p[u] ^ 1].flow -= a[t];
            u = edges[p[u]].from;
        }
        for(int i = 0; i < n; i++){
            h[i] += d[i];
        }
        return true;
    }
    pair<T, C> work(int s, int t){
        this -> s = s, this -> t = t;
        SPFA();
        T flow = 0; 
        C cost = 0;
        while(Dijkstra(flow, cost));
        return {flow, cost};
    }
};

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

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

    vector<int> col(n);
    for(int i = 0; i < n; i++){
        cin >> col[i];
        col[i]--;
        cnt[col[i]]++;
    }

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

    MCMF<ll, ll> gp(n + m + 2);
    int S = n + m, T = n + m + 1;
    for(int i = 0; i < n; i++){
        if(i)gp.add_edge(i, i - 1, INF, 0);
        gp.add_edge(i, T, 1, -val[i]);
    }

    for(int i = 0; i < m; i++){
        if(num[i] > cnt[i]){
            cout << -1 << "\n";
            return;
        }
        gp.add_edge(S, n + i, cnt[i] - num[i], 0);
        for(int j = 1; j < n; j++){
            if(col[j] == i)gp.add_edge(n + i, j - 1, 1, 0);
        }
    }

    auto [flow, cost] = gp.work(S, T);
    if(flow != n / 2){
        cout << -1 << "\n";
    } else{
        cout << -cost << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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:

5904524413
343766215
-1
-1
2682083449
-1
-1
2539318868
1013080942
-1
-1
-1
2231197660
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
6469784940
-1
-1
-1
-1
-1
883992368
1232523916
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1309966981
610708479
-1
-1
1373323696

result:

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