QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#567037 | #9315. Rainbow Bracket Sequence | zeemanz | WA | 1ms | 3880kb | C++20 | 3.3kb | 2024-09-16 05:35:25 | 2024-09-16 05:35:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <class T> struct MCFGraph {
static constexpr T inf = numeric_limits<T>::max() / 2;
struct Edge {
int v;
T c, w;
Edge(int v, T c, T w) : v{v}, c{c}, w{w} {}
};
int n;
vector<Edge> e;
vector<vector<int>> g;
vector<T> dis;
vector<int> vis, pre;
MCFGraph() {}
MCFGraph(int n) { init(n); }
void init(int n) {
this->n = n;
e.clear();
g.assign(n, {});
dis.resize(n);
vis.resize(n);
pre.resize(n);
}
void addEdge(int u, int v, T c, T w) {
g[u].push_back(e.size());
e.emplace_back(v, c, w);
g[v].push_back(e.size());
e.emplace_back(u, 0, -w);
}
bool spfa(int s, int t) {
dis.assign(n, inf);
vis.assign(n, 0);
pre.assign(n, -1);
queue<int> que;
dis[s] = 0;
vis[s] = 1;
que.push(s);
while (!que.empty()) {
int x = que.front();
que.pop();
vis[x] = 0;
for (int i : g[x]) {
auto [y, c, w] = e[i];
if (c > 0 && dis[y] > dis[x] + w) {
dis[y] = dis[x] + w;
pre[y] = i;
if (!vis[y]) {
vis[y] = 1;
que.push(y);
}
}
}
}
return pre[t] != -1;
}
pair<T, T> flow(int s, int t) {
T flow{}, cost{};
while (spfa(s, t)) {
T aug = inf;
for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
aug = min(aug, e[pre[i]].c);
}
for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
e[pre[i]].c -= aug;
e[pre[i] ^ 1].c += aug;
}
flow += aug;
cost += aug * dis[t];
}
return {flow, cost};
}
};
void solve() {
int n, m;
cin >> n >> m;
vector<int> l(m);
for (int i = 0; i < m; i++) {
cin >> l[i];
}
vector<int> c(2 * n), v(2 * n);
for (int i = 0; i < 2 * n; i++) {
cin >> c[i];
c[i]--;
}
for (int i = 0; i < 2 * n; i++) {
cin >> v[i];
}
vector<int> cnt(m);
for (int i = 0; i < 2 * n; i++) {
cnt[c[i]]++;
}
const int S = 4 * n + m, T = S + 1;
MCFGraph<i64> mcf(T + 1);
for (int i = 0; i < 2 * n; i++) {
mcf.addEdge(S, i, 1, 0);
mcf.addEdge(2 * n + i, 4 * n + c[i], 1, v[i]);
for (int j = i + 1; j < 2 * n; j++) {
mcf.addEdge(i, 2 * n + j, 1, 0);
}
}
for (int i = 0; i < m; i++) {
if (cnt[i] - l[i] < 0) {
cout << "-1\n";
return;
}
mcf.addEdge(4 * n + i, T, cnt[i] - l[i], 0);
}
auto [x, y] = mcf.flow(S, T);
if (x < n) {
cout << "-1\n";
} else {
cout << accumulate(v.begin(), v.end(), 0LL) - y << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(16);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3880kb
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: 1ms
memory: 3872kb
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 1649648670 1713429337 2201471991 -1 520108799 2539318868 1093935906 2190621937 -1 3658675327 2231197660 940522111 957616601 2622062646 -1 204813685 5689642167 1686082862 -1 1988904333 3752539386 1343372417 5751395565 140867946 1699018543 -1 4236865447 858682235 883992368 1044562...
result:
wrong answer 1st numbers differ - expected: '-1', found: '5220751523'