QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#575095 | #9315. Rainbow Bracket Sequence | star | WA | 2ms | 3972kb | C++20 | 2.5kb | 2024-09-19 10:29:31 | 2024-09-19 10:29:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N=5e3+50,M=1e6+5,inf=0x7f7f7f7f7f7f7f7f;
int S,T;
struct edge{int v,c,cost,ne;}e[M];
int h[N],idx=1; //从2,3开始配对
int d[N],cur[N],vis[N];
int mincost=0;
int a[N],c[N],v[N];
void add(int a,int b,int c,int cost){
e[++idx]={b,c,cost,h[a]};
h[a]=idx;
}
void adde(int a,int b,int c,int cost){
add(a,b,c,cost);
add(b,a,0,-cost);
}
bool spfa(){
memset(d,inf,sizeof d);
memset(vis,0,sizeof vis);
queue<int>q;
d[S]=0;
vis[S]=1;
q.push(S);
while(q.size()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(e[i].c&&d[v]>d[u]+e[i].cost){
d[v]=d[u]+e[i].cost;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}
if(d[T]!=inf)return 1;
return 0;
}
int dfs(int u, int mf){ //多路增广
if(u==T||mf==0) return mf;
int sum=0;
vis[u]=1;
for(int i=cur[u];i;i=e[i].ne){
cur[u]=i; //当前弧优化
int v=e[i].v;
if(d[v]==d[u]+e[i].cost && e[i].c&&!vis[v]){
int f=dfs(v,min(mf,e[i].c));
if(f){
e[i].c-=f;
e[i^1].c+=f; //更新残留网
sum+=f; //累加u的流出流量
mincost+=e[i].cost*f;
mf-=f; //减少u的剩余流量
}
}
if(mf==0)break;//余量优化
}
if(sum==0) d[u]=0; //残枝优化
vis[u]=0;
return sum;
}
int dinic(){ //累加可行流
int flow=0;
while(spfa()){
memcpy(cur, h, sizeof h);
flow+=dfs(S,inf);
}
return flow;
}
void init(){
idx=1;
mincost=0;
memset(h,0,sizeof h);
memset(cur,0,sizeof cur);
}
void slove(){
int n,m;
cin>>n>>m;
init();
S=0,T=m+n*2+1;
int sum=0;
for(int i=1;i<=m;i++){
cin>>a[i];
adde(S,i,a[i]*2,0);
sum+=a[i];
}
for(int i=1;i<=n*2;i++){
cin>>c[i];
adde(m+i,T,1,0);
}
for(int i=1;i<=n*2;i++){
cin>>v[i];
adde(c[i],i+m,2,-v[i]);
}
for(int i=1;i<=n*2;i++){
for(int j=i+1;j<=n*2;j++){
adde(m+i,m+j,1,0);
}
}
if(sum>n){
cout<<-1<<'\n';
return;
}
int ans=dinic();
ans/=2;
/*
if(ans!=sum){
cout<<-1<<'\n';
return;
}*/
cout<<-mincost/2<<'\n';
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
int t;
cin>>t;
while(t--){
slove();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3972kb
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: 2ms
memory: 3864kb
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:
4839751835 359113685 1649648670 1843181641 2145509671 1230630334 831452391 2539318868 1053508424 1241758834 360191962 3583012638 2231197660 758225925 0 3021518502 1773576034 915601296 5749070141 1345740508 2539009666 1988904333 2390661163 999388067 5509933732 0 1375278074 5215200712 4236865447 0 883...
result:
wrong answer 1st numbers differ - expected: '-1', found: '4839751835'