QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#574336 | #9315. Rainbow Bracket Sequence | Peanut_Tang | WA | 2ms | 11996kb | C++14 | 3.9kb | 2024-09-18 21:37:17 | 2024-09-18 21:37:18 |
Judging History
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], cur[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 in[N], T, vis[N]; ll dis[N], cost;
ll dfs(int id, ll res) {
vis[id] = 1;
if(id == T) return res;
ll flow = 0;
for(int i = cur[id]; i && res; i = nxt[i]) {
cur[id] = i; // 不要漏,上面的i不要加&
ll c = std::min(res, limit[i]); int it = to[i];
if(dis[id] + cst[i] == dis[it] && c && !vis[it]) {
ll k = dfs(it, c);
if (k) {
cost += k * cst[i], flow += k, res -= k;
limit[i] -= k, limit[i ^ 1] += k;
} else {
dis[it] = -1;
}
}
}
vis[id] = 0;
return flow;
}
std::pair<ll, ll> mincost(int n, int s, int t) {
ll flow = 0; cost = 0, T = t;
while(1) {
std::queue<int> q;
std::copy(hd, hd + n + 1, cur);
std::fill(dis, dis + n + 1, INFLL);
std::fill(in, in + n + 1, 0);
q.push(s), 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;
if(!in[it]) in[it] = 1, q.push(it);
}
}
}
if(dis[t] >= INFLL) return {flow, cost};
ll x; while (std::fill(vis, vis + n + 1, 0), x = dfs(s, INFLL)) flow += x;
}
}
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> mincost(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.mincost(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.mincost(n + 1, s, t);
return {flow + res.first, cost + res.second};
}
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.mincost(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: 11996kb
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: 0ms
memory: 11952kb
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 1351561190 -1755648428 1013080942 361679250 -1 -1 -2063769636 -1575835568 -311339655 417206872 -1 1046749330 1820247461 -373979093 -1 -392878350 -1 -1728413304 973504604 1682153452 -1084433058 -1 759308175 1467678317 883992368 1044562986 -1 -270332793 -1 144...
result:
wrong answer 3rd numbers differ - expected: '2351080746', found: '-1943886550'