QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#566206 | #9315. Rainbow Bracket Sequence | ucup-team4074 | WA | 1ms | 3656kb | C++14 | 3.9kb | 2024-09-15 23:21:48 | 2024-09-15 23:21:52 |
Judging History
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 = anss = 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];
}
int flag(1);
for (int i = 1; i <= m; i++) {
if (num[i] - lim[i] >= 0)
MCMF::add_edge(MCMF::S, i + 1, num[i] - lim[i], 0);
else flag = 0;
}
if (!flag) {
cout << -1 << '\n';
continue;
}
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;
}
/*
1
1 1
1
1 1
343766215 374461155
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3656kb
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: 3596kb
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 2351080746 3426965219 -1 -1 1351561190 2539318868 1013080942 -1 -1 -1 2231197660 2719131728 3983627641 -1 -1 1046749330 6115214757 3920988203 -1 3902088946 -1 2566553992 -1 5977120748 7505501534 -1 5054275471 1467678317 883992368 1044562986 -1 4024634503 -1 1447294256 6757607722 3688048...
result:
wrong answer 10th numbers differ - expected: '4656646546', found: '-1'