QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#573762 | #9315. Rainbow Bracket Sequence | Rosmontispes | RE | 0ms | 3576kb | C++20 | 3.5kb | 2024-09-18 19:54:39 | 2024-09-18 19:54:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int inf = 0x3f3f3f3f;
struct MCFGraph {
struct Edge {
int v, c, f;
Edge(int v, int c, int f) : v(v), c(c), f(f) {}
};
const int n;
std::vector<Edge> e;
std::vector<std::vector<int>> g;
std::vector<i64> h, dis;
std::vector<int> pre;
bool dijkstra(int s, int t) {
dis.assign(n, std::numeric_limits<i64>::max());
pre.assign(n, -1);
priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> que;
dis[s] = 0;
que.emplace(0, s);
while (!que.empty()) {
i64 d = que.top().first;
int u = que.top().second;
que.pop();
if (dis[u] < d) continue;
for (int i : g[u]) {
int v = e[i].v;
int c = e[i].c;
int f = e[i].f;
if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
dis[v] = d + h[u] - h[v] + f;
pre[v] = i;
que.emplace(dis[v], v);
}
}
}
return dis[t] != numeric_limits<i64>::max();
}
MCFGraph(int n) : n(n), g(n) {}
void addEdge(int u, int v, int c, int f) {
g[u].push_back(e.size());
e.emplace_back(v, c, f);
g[v].push_back(e.size());
e.emplace_back(u, 0, -f);
}
pair<int, i64> flow(int s, int t) {
int flow = 0;
i64 cost = 0;
h.assign(n, 0);
while (dijkstra(s, t)) {
for (int i = 0; i < n; ++i) h[i] += dis[i];
int aug = numeric_limits<int>::max();
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 += i64(aug) * h[t];
}
return make_pair(flow, cost);
}
};
void solve()
{
int n,m;
cin>>n>>m;
vector<int>lim(m),clr(n),val(n);
for(int i = 0;i < m;i ++)
cin>>lim[i];
for(int i = 0;i < 2 * n;i ++)
cin>>clr[i];
for(int i = 0;i < 2 * n;i ++)
cin>>val[i];
//0~m - 1:颜色
//m~m + 2 * n - 1:点
int tot = 0;
int S = m + 2 * n,T = m + 2 * n + 1;
int SS = m + 2 * n + 2,TT = m + 2 * n + 3;
MCFGraph F(m + 2 * n + 4);
vector<int>out(m + 2 * n + 2);
for(int i = 0;i < m;i ++)
{
F.addEdge(S,i,inf,0);
out[S] += lim[i];
out[i] -= lim[i];
}
for(int i = 0;i < 2 * n;i ++){
F.addEdge(clr[i] - 1,m + i,1,-val[i]);
if(i < 2 * n - 1){
F.addEdge(m + i,m + i + 1,n - (i / 2 + 1),0);
out[m + i] += i / 2 + 1;
out[m + i + 1] -= i / 2 + 1;
}
}
F.addEdge(m + 2 * n - 1,T,0,0);
out[m + 2 * n - 1] += n;
out[T] -= n;
F.addEdge(T,S,inf,0);
for(int i = 0;i < m + 2 * n + 2;i ++){
if(out[i] > 0){
tot += out[i];
F.addEdge(i,TT,out[i],0);
}
else if(out[i] < 0){
F.addEdge(SS,i,-out[i],0);
}
}
auto [flow,cost] = F.flow(SS,TT);
// cout<<flow<<" "<<cost<<"\n";
if(flow < tot)
cout<<"-1\n";
else
cout<<-cost<<"\n";
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
cin>>T;
while(T --)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
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
Runtime Error
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...