QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#572629 | #9315. Rainbow Bracket Sequence | Peanut_Tang | TL | 2ms | 14108kb | C++14 | 3.5kb | 2024-09-18 15:41:30 | 2024-09-18 15:41:30 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
const int N = 1e5, M = 1e5;
const ll INF = 1e18;
int n, m, S, T, c[N];
struct costflow {
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> mincost(int n, int s, int t) {
ll flow = 0, cost = 0;
while(1) { // SPFA
std::queue<int> q;
std::fill(dis + 1, dis + n + 1, INF);
std::fill(in + 1, in + n + 1, 0);
fl[s] = INF, dis[s] = 0, q.push(s);
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], d = dis[t] + cst[i];
if(limit[i] && d < dis[it]) {
fl[it] = std::min(limit[i], fl[t]), fr[it] = i, dis[it] = d;
if(!in[it]) in[it] = 1, q.push(it);
}
}
}
if(dis[t] >= INF) return std::make_pair(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 + 1, hd + n + 1, 0);
}
};
struct bounded_costflow {
int e, u[M], v[M]; ll 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] = l, hi[e] = r, cst[e] = -c;
}
else u[++e] = _u, v[e] = _v, lo[e] = l, hi[e] = r, cst[e] = c;
}
costflow g;
std::pair<ll, ll> mincost(int n, int s, int t) {
int ss = n + 1, tt = n + 2;
static ll w[N];
std::fill(w + 1, w + n + 1, 0);
g.init(n + 2);
ll flow = 0, cost = 0, tot = 0;
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, INF, 0);
std::pair<ll, ll> res = g.mincost(n + 2, 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, s, t);
return std::make_pair(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: 14108kb
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
Time Limit Exceeded
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...