QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#566121#9315. Rainbow Bracket Sequenceucup-team4074WA 1ms3664kbC++143.7kb2024-09-15 23:05:382024-09-15 23:05:39

Judging History

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

  • [2024-09-15 23:05:39]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3664kb
  • [2024-09-15 23:05:38]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 505, M = 50005;
const int INF = 1e9;
namespace MCMF {
    int cnt, ls[N], ans, S, T, n, cur[N], dis[N], vis[N], anss;
    struct edge {
        int nx, to;
        int c, val;
    } bian[2 * M];
    void add_edge(int x, int y, int cap, int cost) {
//        cout << x << " " << y << " " << cap << " " << cost << '\n';
        bian[++cnt].nx = ls[x];
        bian[cnt].to = y;
        bian[cnt].c = cap;
        bian[cnt].val = cost;
        ls[x] = cnt;
        bian[++cnt].nx = ls[y];
        bian[cnt].to = x;
        bian[cnt].c = 0;
        bian[cnt].val = -cost;
        ls[y] = cnt;
    }
    void init() {
        cnt = 1, ans = 0;
        memset(ls, 0, sizeof(ls));
    }
    bool SPFA() {
        queue<int> q;
        for (int i = 1; i <= n; i++)
            cur[i] = ls[i], dis[i] = INF, vis[i] = 0;
        q.push(S);
        dis[S] = 0;
        vis[S] = 1;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for (int i = ls[u]; i; i = bian[i].nx) {
                if (bian[i].c && dis[bian[i].to] > dis[u] + bian[i].val) {
                    dis[bian[i].to] = dis[u] + bian[i].val;
                    if (!vis[bian[i].to]) {
                        q.push(bian[i].to);
                        vis[bian[i].to] = 1;
                    }
                }
            }
        }
        return dis[T] != INF;
    }
    int dfs(int now, int flow) {
        if (now == T)return flow;
        int sum = 0;
        vis[now] = 1;
        for (int &i = cur[now]; i; i = bian[i].nx) {
            if (dis[bian[i].to] == dis[now] + bian[i].val && bian[i].c && !vis[bian[i].to]) {
                int sav = dfs(bian[i].to, min(flow, bian[i].c));
                if (sav) {
                    bian[i].c -= sav;
                    bian[i ^ 1].c += sav;
                    sum += sav;
                    flow -= sav;
                    anss += sav * bian[i].val;
                    if (!flow)return sum;
                } else dis[bian[i].to] = -1;
            }
        }
        return sum;
    }
    int work() {
        while (SPFA()) {
            for (int i = 1; i <= n; i++)vis[i] = 0;
            ans += dfs(S, INF);
        }
        return ans;
    }
}
int n, m, lim[N], col[N], val[N], num[N], T;
signed main() {
    cin >> T;
    while (T--) {
        memset(num, 0, sizeof(num));
        MCMF::init();
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            cin >> lim[i];
        }
        MCMF::n = m + 3 * n + 3;
        MCMF::S = 1;
        MCMF::T = m + 3 * n + 3;
        int tt = m + 3 * n + 2, sum(0);
        for (int i = 1; i <= 2 * n; i++) {
            cin >> col[i];
            num[col[i]]++;
        }
        for (int i = 1; i <= 2 * n; i++) {
            cin >> val[i];
            sum += val[i];
        }
        for (int i = 1; i <= m; i++) {
            MCMF::add_edge(MCMF::S, i + 1, num[i] - lim[i], 0);
        }

        for (int i = 1; i <= 2 * n; i++) {
            MCMF::add_edge(col[i] + 1, m + 1 + i, 1, 0);
            if (i > 1) {
                MCMF::add_edge(m + 1 + i, m + 2 * n + 1 + i / 2, 1, val[i]);
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i > 1) {
                MCMF::add_edge(m + 2 * n + 1 + i, m + 2 * n + 1 + i - 1, INF, 0);
            }
            MCMF::add_edge(m + 2 * n + 1 + i, tt, 1, 0);
        }
        MCMF::add_edge(tt, MCMF::T, n, 0);
        if (MCMF::work() == n)
            cout << sum - MCMF::anss << '\n';
        else cout << -1 << '\n';
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
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: 3664kb

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
-1486920266
145933110
236003228
-1
-4902235628
-5232564309
-5112955054
-7433977693
-1
-1
-1
-12075594493
-12380202367
-12214785906
-1
-17337205089
-21222636986
-17323025335
-23699970713
-25845602806
-27256494745
-1
-31586575172
-1
-31560744727
-32527876463
-1
-41364350306
-47305873445
-4851469268...

result:

wrong answer 2nd numbers differ - expected: '343766215', found: '-1486920266'