QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#575295 | #9315. Rainbow Bracket Sequence | Lezy233 | WA | 1ms | 3872kb | C++20 | 3.3kb | 2024-09-19 11:55:06 | 2024-09-19 11:55:07 |
Judging History
answer
#include <bits/stdc++.h>
using std::cin,std::cout,std::vector,std::array;
#define endl "\n"
#define ALL(x) x.begin(),x.end()
using ll = long long;
using PII = std::pair<int,int>;
using T3I = std::tuple<int,int,int>;
template <typename T>
class MCF_Graph {
private:
using PTI = std::pair<T, int>;
struct _Edge {
int to;
T cap, cost;
_Edge(int _to, T _cap, T _cost): to(_to), cap(_cap), cost(_cost) {}
};
const int n;
std::vector<_Edge> edges;
std::vector<std::vector<int>> adj;
std::vector<T> h, dis;
std::vector<int> pre;
bool dijkstra(int s, int t) {
dis.assign(n, std::numeric_limits<T>::max());
pre.assign(n, -1);
std::priority_queue<PTI, std::vector<PTI>, std::greater<PTI>> pq;
dis[s] = 0;
pq.emplace(0, s);
while(!pq.empty()) {
auto [uDis,u] = pq.top(); pq.pop();
if(dis[u]<uDis) continue;
for(auto &i:adj[u]) {
auto &[v,cp,ct] = edges[i];
if(cp>0 && dis[v]>uDis+h[u]-h[v]+ct) {
dis[v] = uDis+h[u]-h[v]+ct;
pre[v] = i;
pq.emplace(dis[v], v);
}
}
}
return dis[t]!=std::numeric_limits<T>::max();
}
public:
MCF_Graph(size_t _n): n(_n), adj(_n) {}
void addEdge(int u, int v, T cost=0, T cap=std::numeric_limits<T>::max()) {
adj[u].emplace_back(edges.size());
edges.emplace_back(v, cap, cost);
adj[v].emplace_back(edges.size());
edges.emplace_back(u, 0, -cost);
}
// first: maxFlow
// second: minCost
std::pair<T, T> getFlow(int s, int t) {
T maxFlow = 0, minCost = 0;
h.assign(n, 0);
while(dijkstra(s, t)) {
for(int i=0; i<n; ++i) h[i] += dis[i];
T aug = std::numeric_limits<T>::max();
for(int i=t; i!=s; i=edges[pre[i]^1].to) aug = std::min(aug, edges[pre[i]].cap);
for(int i=t; i!=s; i=edges[pre[i]^1].to) {
edges[pre[i]].cap -= aug;
edges[pre[i]^1].cap += aug;
}
maxFlow += aug;
minCost += aug*h[t];
}
return {maxFlow, minCost};
}
};
void solve()
{
int n,m; cin>>n>>m;
vector<int> l(m+1), c(n*2+1), v(n*2+1), cnt(m+1);
for(int i=1; i<=m; ++i) cin>>l[i];
for(int i=1; i<=n*2; ++i) {
cin>>c[i];
++cnt[c[i]];
}
for(int i=1; i<=n*2; ++i) cin>>v[i];
if(std::accumulate(ALL(l), 0)>n){ cout<<-1<<endl; return; }
ll sum_val = std::accumulate(ALL(v), 0LL);
int S = n*2+m+1, T = S+1;
MCF_Graph<ll> MF(T+1);
MF.addEdge(S, n*2, 0, n);
for(int i=n*2-1; i; --i)
MF.addEdge(i+1, i, 0, i/2);
for(int i=n*2; i; --i)
MF.addEdge(i, n*2+c[i], v[i], 1);
for(int i=1; i<=m; ++i)
MF.addEdge(n*2+i, T, 0, cnt[i]-l[i]);
auto [maxFlow, minCost] = MF.getFlow(S, T);
if(maxFlow<n) cout<<-1<<endl;
else cout<<sum_val-minCost<<endl;
}
int main()
{
std::cin.tie(nullptr)->sync_with_stdio(false);
#ifndef ONLINE_JUDGE
std::ifstream in("C++/in.txt");
std::cin.rdbuf(in.rdbuf());
#endif
int T = 1;
std::cin>>T;
while(T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3872kb
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: 3676kb
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 1497027962 1351561190 2539318868 1013080942 4656646546 -1 -1 2231197660 2719131728 3983627641 4712174168 3121174891 1046749330 6115214757 3920988203 3914858167 3902088946 -1 2566553992 5268471900 5977120748 7505501534 -1 5054275471 1467678317 883992368 104456298...
result:
wrong answer 6th numbers differ - expected: '-1', found: '1497027962'