QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#575968 | #9315. Rainbow Bracket Sequence | Rilllo | WA | 4ms | 8292kb | C++23 | 3.5kb | 2024-09-19 17:42:00 | 2024-09-19 17:42:00 |
Judging History
answer
#include<bits/stdc++.h>
using i64 = long long;
const int V = 1000;
const int E = 100000;
template<class T>
struct MCF {
int s, t, n, m;
T flow, cost, dis[V];
int head[V], cur[V], pre[V];
bool vis[V];
struct edge {
int y, nxt;
T f, c;
} e[E * 2];
void init(int _s, int _t, int _n) {
m = 0;
n = _n;
s = _s;
t = _t;
for (int i = 0; i < n; i++) {
head[i] = -1;
}
}
void addedge(int x, int y, T f1, T c, T f2 = 0) {
e[m] = {y, head[x], f1, c}; head[x] = m++;
e[m] = {x, head[y], f2, -c}; head[y] = m++;
}
bool spfa() {
T inf = std::numeric_limits<T>::max() / 2;
for (int i = 0; i < n; i++) {
dis[i] = inf;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
std::queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front();
for (int i = head[x]; ~i; i = e[i].nxt) {
int y = e[i].y;
if (e[i].f && dis[y] > dis[x] + e[i].c) {
dis[y] = dis[x] + e[i].c;
pre[y] = i;
if (!vis[y]) {
vis[y] = 1;
q.push(y);
}
}
}
q.pop();
vis[x] = false;
}
// for (int i = 0; i < n; i++) {
// std::cout << dis[i] << " \n"[i == n - 1];
// }
return dis[t] != inf;
}
void augment() {
int x = t;
T f = std::numeric_limits<T>::max();
while (~pre[x]) {
// std::cout << x << " ";
f = std::min(f, e[pre[x]].f);
x = e[pre[x] ^ 1].y;
}
// std::cout << x<< "\n";
flow += f;
cost += f * dis[t];
// std::cout << "flow : " << flow << " " << "cost : " << cost << "\n";
x = t;
while (~pre[x]) {
e[pre[x]].f -= f;
e[pre[x] ^ 1].f += f;
x = e[pre[x] ^ 1].y;
}
}
std::pair<T, T> work() {
flow = 0;
cost = 0;
while (spfa()) augment();
return {flow, cost};
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> l(m), c(2 * n), v(2 * n);
for (int i = 0; i < m; i++) {
std::cin >> l[i];
}
for (int i = 0; i < 2 * n; i++) {
std::cin >> c[i];
c[i]--;
}
for (int i = 0; i < 2 * n; i++) {
std::cin >> v[i];
}
int s = 4 * n + m, t = 4 * n + m + 1;
int ss = s + 2, st = s + 3;
MCF<i64> flow;
flow.init(ss, st, st + 1);
int sum = 0;
int cs = std::accumulate(l.begin(), l.end(), 0);
if (cs > n) {
std::cout << -1 << "\n";
return ;
}
// std::cout << (int)(-1000000000) << "\n";
flow.addedge(t, st, n, 0);
flow.addedge(ss, s, n - cs, 0);
sum += n - cs;
for (int i = 0; i < m; i++) {
int id = 4 * n + i;
flow.addedge(s, id, n, 0);
flow.addedge(ss, id, l[i], 0);
sum += l[i];
}
for (int i = 0; i < 2 * n; i++) {
int id = c[i] + 4 * n;
flow.addedge(id, i, 1, -v[i]);
flow.addedge(i, st, 1, 0);
flow.addedge(ss, i + n, 1, 0);
sum++;
for (int j = i + 1; j < 2 * n; j++) {
flow.addedge(i + n, j, 1, 0);
}
flow.addedge(i + n, t, 1, 0);
}
auto[f, cost] = flow.work();
// std::cout << f << " " << -cost << "\n";
if (sum == f) {
std::cout << -cost << "\n";
} else {
std::cout << -1 << "\n";
}
// std::cout << -cost << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(10);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8292kb
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: 4ms
memory: 8292kb
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:
5220751523 343766215 2351080746 3673330360 2201471991 -1 1351561190 2539318868 1093935906 4946124799 -1 4600692243 2231197660 3107528418 4640037833 5142301623 -1 1194080633 6403585429 4247389899 -1 4230243225 4836311506 2615588023 5751395565 6003410525 7529223112 -1 5054275471 1467678317 883992368 1...
result:
wrong answer 1st numbers differ - expected: '-1', found: '5220751523'