QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570740 | #9315. Rainbow Bracket Sequence | dreamjoker | WA | 1ms | 3572kb | C++23 | 7.5kb | 2024-09-17 17:29:46 | 2024-09-17 17:29:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
// template<typename T>
// struct Dinic {
// int n;
// struct edge {
// int to,nxt;
// T cap,flow;
// T cost;
// };
// std::vector<edge> e;
// std::vector<int> head,now;
// std::vector<T> h,dep;
// std::vector<bool> vis;
// T inf=std::numeric_limits<T>::max()>>1;
// Dinic(int _n) : n(_n),head(n+1,-1) {}
// void add_edge(int u,int v,T cap,T cost=0) {
// e.push_back({v,head[u],cap,0,cost});
// head[u]=e.size()-1;
// e.push_back({u,head[v],0,0,-cost});
// head[v]=e.size()-1;
// }
// // Johnson algorithm
// void spfa(int s) {
// h.assign(n+1,inf);
// std::queue<int> q;
// h[s]=0;
// q.push(0);
// vis[s]=true;
// while(!q.empty()) {
// int u=q.front();
// q.pop();
// vis[u]=false;
// for(int i=head[u];~i;i=e[i].nxt) {
// int v=e[i].to;
// if(e[i].cap>e[i].flow&&h[v]>h[u]+e[i].cost) {
// h[v]=h[u]+e[i].cost;
// if(!vis[v]) {
// vis[v]=true;
// q.push(v);
// }
// }
// }
// }
// }
// bool dij(int s,int t) {
// fill(dep.begin(),dep.end(),inf);
// fill(vis.begin(),vis.end(),false);
// std::priority_queue<std::pair<T,int>,std::vector<std::pair<T,int>>,std::greater<std::pair<T,int>>> pq;
// dep[s]=0;
// pq.push({0,s});
// while(!pq.empty()) {
// int u=pq.top().second;
// pq.pop();
// if(vis[u]) continue;
// vis[u]=true;
// for(int i=head[u];~i;i=e[i].nxt) if(e[i].cap>e[i].flow){
// int v=e[i].to;
// auto newdep=dep[u]+e[i].cost+h[u]-h[v];
// if(dep[v]>newdep) {
// dep[v]=newdep;
// pq.push({dep[v],v});
// }
// }
// }
// return dep[t]!=inf;
// }
// T dfs(int u,int t,T flow) {
// if(u==t||!flow) return flow;
// T rest=flow;
// vis[u]=true;
// for(int i=now[u];~i&&rest;i=e[i].nxt) {
// now[u]=i;
// int v=e[i].to;
// if(!vis[v]&&dep[v]==dep[u]+e[i].cost+h[u]-h[v]&&e[i].cap>e[i].flow) {
// T delta=dfs(v,t,std::min(rest,e[i].cap-e[i].flow));
// if(!delta) continue;
// e[i].flow+=delta;
// e[i^1].flow-=delta;
// rest-=delta;
// }
// }
// vis[u]=false;
// return flow-rest;
// }
// // flag 表示初始图中是否可能存在负权边,如果存在的话需要先Johnson算法求出h函数
// auto MCMF(int s,int t,int flag=0) {
// h.assign(n+1,0); vis.assign(n+1,false); dep.resize(n+1);
// T flow=0,cost=0;
// if(flag) spfa(s);
// while(dij(s,t)) {
// std::fill(vis.begin(),vis.end(),false);
// now=head;
// T delta=0,tmp;
// while(tmp=dfs(s,t,inf))
// delta+=tmp;
// for(int i=0;i<=n;i++)
// h[i]+=dep[i];
// flow+=delta,cost+=h[t]*delta;
// }
// return std::make_pair(cost,flow);
// }
// void resetflow(T x=0) {
// for(auto &it:e)
// it.flow=x;
// }
// };
template<typename T>
struct Dinic {
int n;
struct edge {
int to,nxt;
T cap,flow;
T cost;
};
std::vector<edge> e;
std::vector<int> head,now;
std::vector<T> h,dep;
std::vector<bool> vis;
std::vector<int> pre;
T inf=std::numeric_limits<T>::max()>>1;
Dinic(int _n) : n(_n) {
head.resize(n+1,-1);
now.resize(n+1);
h.resize(n+1);
dep.resize(n+1);
vis.resize(n+1);
pre.resize(n+1);
}
void add_edge(int u,int v,T cap,T cost=0) {
e.push_back({v,head[u],cap,0,cost});
head[u]=e.size()-1;
e.push_back({u,head[v],0,0,-cost});
head[v]=e.size()-1;
}
// Johnson algorithm
void spfa(int s) {
fill(h.begin(),h.end(),inf);
std::queue<int> q;
h[s]=0;
q.push(0);
vis[s]=true;
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];~i;i=e[i].nxt) {
int v=e[i].to;
if(e[i].cap>e[i].flow&&h[v]>h[u]+e[i].cost) {
h[v]=h[u]+e[i].cost;
if(!vis[v]) {
vis[v]=true;
q.push(v);
}
}
}
}
}
bool dij(int s,int t) {
fill(dep.begin(),dep.end(),inf);
fill(vis.begin(),vis.end(),false);
std::priority_queue<std::pair<T,int>,std::vector<std::pair<T,int>>,std::greater<std::pair<T,int>>> pq;
dep[s]=0;
pq.push({0,s});
while(!pq.empty()) {
int u=pq.top().second;
pq.pop();
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];~i;i=e[i].nxt) if(e[i].cap>e[i].flow){
int v=e[i].to;
auto newdep=dep[u]+e[i].cost+h[u]-h[v];
if(dep[v]>newdep) {
dep[v]=newdep;
pre[v]=i; // 记录前驱边
pq.push({dep[v],v});
}
}
}
return dep[t]!=inf;
}
// flag 表示初始图中是否可能存在负权边,如果存在的话需要先Johnson算法求出h函数
auto MCMF(int s,int t,int flag=0) {
T flow=0,cost=0;
if(flag) spfa(s);
while(dij(s,t)) {
for(int i=0;i<=n;i++)
h[i]+=dep[i];
T delta=std::numeric_limits<T>::max();
for(int i=t;i!=s;i=e[pre[i]^1].to)
delta=std::min(delta,e[pre[i]].cap-e[pre[i]].flow);
for(int i=t;i!=s;i=e[pre[i]^1].to) {
e[pre[i]].flow+=delta;
e[pre[i]^1].flow-=delta;
}
flow+=delta;
cost+=delta*h[t];
}
return std::make_pair(cost,flow);
}
void resetflow(T x=0) {
for(auto &it:e)
it.flow=x;
}
};
using ll = long long ;
void sol() {
int n,m; cin>>n>>m;
vector<int> lim(m);
for(int &x:lim) cin>>x;
vector<int> c(2*n),v(2*n);
vector<int> cnt(m);
for(int &x:c) {
cin>>x;
cnt[--x]++;
}
ll sum=0;
for(int &x:v) {
cin>>x;
sum+=x;
}
int s=2*n+m,t=s+1;
Dinic<ll> dinic(t);
dinic.add_edge(s,2*n-1,n);
for(int i=2*n-1;i>0;i--)
dinic.add_edge(i,i-1,i/2);
for(int i=0;i<2*n;i++)
dinic.add_edge(i,2*n+c[i],1,v[i]);
for(int i=0;i<m;i++) {
if(cnt[i]<=lim[i]) return cout<<-1<<endl,void();
dinic.add_edge(2*n+i,t,cnt[i]-lim[i]);
}
auto [mc,mf]=dinic.MCMF(s,t);
if(mf!=n) cout<<-1<<endl;
else cout<<sum-mc<<endl;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T; cin>>T;
while(T--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
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: 3572kb
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 4656646546 -1 -1 2231197660 -1 -1 4712174168 -1 1046749330 6115214757 -1 -1 3902088946 -1 2566553992 5268471900 5977120748 7505501534 -1 5054275471 1467678317 883992368 -1 -1 4024634503 -1 1447294256 6757607722 -1 -1 -1 -1 629...
result:
wrong answer 14th numbers differ - expected: '2719131728', found: '-1'