QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#574354#9315. Rainbow Bracket SequencePeanut_TangWA 3ms14352kbC++144.0kb2024-09-18 21:42:432024-09-18 21:42:44

Judging History

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

  • [2024-09-18 21:42:44]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14352kb
  • [2024-09-18 21:42:43]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
const int N = 1e5, M = 1e5;

int n, m, S, T, c[N];

const ll INFLL = 1e18;
struct flow {
  int cnt = 1, hd[N], nxt[M << 1], to[M << 1]; ll limit[M << 1], cst[M << 1];
  void add(int u, int v, ll w, ll c) {
    nxt[++cnt] = hd[u], hd[u] = cnt, to[cnt] = v, limit[cnt] = w, cst[cnt] = c;
    nxt[++cnt] = hd[v], hd[v] = cnt, to[cnt] = u, limit[cnt] = 0, cst[cnt] = -c;
  }
  int fr[N], in[N]; ll fl[N], dis[N];
  std::pair<ll, ll> mcmf(int n, int s, int t) {
    ll flow = 0, cost = 0;
    while(1) {
      std::queue<int> q;
      std::fill(dis, dis + n + 1, INFLL);
      q.push(s), fl[s] = INFLL, dis[s] = 0;
      while(!q.empty()) {
        int t = q.front();
        q.pop(), in[t] = 0;
        for(int i = hd[t]; i; i = nxt[i]) {
          int it = to[i]; ll d = dis[t] + cst[i];
          if(limit[i] && d < dis[it]) {
            dis[it] = d, fl[it] = std::min(fl[t], limit[i]), fr[it] = i;
            if(!in[it]) in[it] = 1, q.push(it);
          }
        }
      }
      if(dis[t] >= INFLL) return {flow, cost};
      flow += fl[t], cost += dis[t] * fl[t];
      for(int u = t; u != s; u = to[fr[u] ^ 1]) limit[fr[u]] -= fl[t], limit[fr[u] ^ 1] += fl[t];
    }
  }
  void init(int n) {
    cnt = 1; std::fill(hd, hd + n + 1, 0);
  }
};
struct bounded_flow {
  int e, u[M], v[M], lo[M], hi[M], cst[M];
  void add(int _u, int _v, ll l, ll r, ll c) {
    if(c < 0) {
      u[++e] = _u, v[e] = _v, lo[e] = r, hi[e] = r, cst[e] = c;
      u[++e] = _v, v[e] = _u, lo[e] = 0, hi[e] = r - l, cst[e] = -c;
    }
    else u[++e] = _u, v[e] = _v, lo[e] = l, hi[e] = r, cst[e] = c;
  }
  flow g;
  std::pair<ll, ll> mcmf(int n, int s, int t) {
    int ss = 0, tt = n + 1;
    static ll w[N];
    std::fill(w + 1, w + n + 1, 0);
    int flow = 0, cost = 0, tot = 0;
    g.init(n + 1);
    for(int i = 1; i <= e; i++) {
      w[u[i]] -= lo[i], w[v[i]] += lo[i];
      cost += lo[i] * cst[i];
      g.add(u[i], v[i], hi[i] - lo[i], cst[i]);
    }
    for(int i = 1; i <= n; i++)
      if(w[i] > 0) g.add(ss, i, w[i], 0), tot += w[i];
      else if(w[i] < 0) g.add(i, tt, -w[i], 0);
    g.add(t, s, INFLL, 0);
    std::pair<ll, ll> res = g.mcmf(n + 1, ss, tt);
    if (res.first != tot) return {-1, -1};
    cost += res.second;
    flow += g.limit[g.hd[s]];
    g.hd[s] = g.nxt[g.hd[s]], g.hd[t] = g.nxt[g.hd[t]];
    res = g.mcmf(n + 1, s, t);
    return {flow + res.first, cost + res.second};
  }
  std::pair<ll, ll> mcf(int n, int s, int t) {
    int ss = 0, tt = n + 1;
    static ll w[N];
    std::fill(w + 1, w + n + 1, 0);
    int flow = 0, cost = 0, tot = 0;
    g.init(n + 1);
    for(int i = 1; i <= e; i++) {
      w[u[i]] -= lo[i], w[v[i]] += lo[i];
      cost += lo[i] * cst[i];
      g.add(u[i], v[i], hi[i] - lo[i], cst[i]);
    }
    for(int i = 1; i <= n; i++)
      if(w[i] > 0) g.add(ss, i, w[i], 0), tot += w[i];
      else if(w[i] < 0) g.add(i, tt, -w[i], 0);
    g.add(t, s, INFLL, 0);
    std::pair<ll, ll> res = g.mcmf(n + 1, ss, tt);
    if (res.first != tot) return {-1, -1};
    return {res.first, res.second + cost};
  }
  void init() { e = 0; }
} f;

void Solve() {
    scanf("%d %d", &n, &m);
    n += n;
    S = n + n + m + 1;
    T = S + 1;
    f.init();
    for (int i = 1; i <= m; i++) {
        int x;
        scanf("%d", &x);
        f.add(S, i, x, n / 2, 0);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
    }
    for (int i = 1; i <= n; i++) {
        int w;
        scanf("%d", &w);
        f.add(c[i], i + m, 0, 1, -w);
        f.add(i + m, i + n + m, 0, 1, 0);
    }
    for (int i = 1; i < n; i++) {
        f.add(i + n + m, i + 1 + n + m, (i + 1) / 2, i, 0);
    }
    f.add(n + n + m, T, n / 2, n / 2, 0);

    auto res = f.mcf(n + n + m + 2, S, T);
    if (res.first == -1 && res.second == -1) {
        puts("-1");
    } else {
        printf("%lld\n", -res.second);
    }
}

int main() {
    int tt;
    scanf("%d", &tt);
    while (tt--) {
        Solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12032kb

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: 3ms
memory: 14352kb

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
-1943886550
-868002077
-1
-1
-2943406106
-1755648428
1013080942
-3933288046
-1
-1
-2063769636
-1575835568
-311339655
-3877760424
-1
-3248217966
-2474719835
-373979093
-1
-392878350
-1
-1728413304
-3321462692
-2612813844
-5379400354
-1
-3535659121
1467678317
883992368
-3250404310
-1
-456...

result:

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