QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#688181 | #9315. Rainbow Bracket Sequence | Carucao | WA | 1ms | 3672kb | C++20 | 2.3kb | 2024-10-30 00:09:31 | 2024-10-30 00:09:31 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=410;
const int inf=1e12;
int n,m,l[N],col[N],v[N],dis[N],num,head[N],pre[N],s,t,preve[N],h[N];
bool p[N];
struct node
{
int x,dis;
friend bool operator<(node a,node b)
{
return a.dis>b.dis;
}
};
priority_queue<node> q;
struct adc
{
int nex,to,cost,z;
}c[N<<4];
void ljb(int x,int y,int cost,int z)
{
c[++num].nex=head[x];
c[num].to=y;
c[num].cost=cost;
c[num].z=z;
head[x]=num;
}
void add(int x,int y,int cost,int z)
{
ljb(x,y,cost,z);
ljb(y,x,-cost,0);
}
bool dijkstra()
{
for(int i=1;i<=t;++i) pre[i]=0,h[i]+=dis[i],dis[i]=inf,p[i]=0;
dis[s]=0;
q.push({s,0});
while(!q.empty())
{
auto u=q.top();
q.pop();
int x=u.x;
if(p[x]) continue;
p[x]=1;
for(int i=head[x];i;i=c[i].nex)
{
if(c[i].z>0)
{
int y=c[i].to;
double cost=c[i].cost;
if(dis[x]+cost+h[x]-h[y]<dis[y])
{
dis[y]=dis[x]+cost+h[x]-h[y];
pre[y]=x;
preve[y]=i;
q.push({y,dis[y]});
}
}
}
}
return dis[t]!=inf;
}
int mincost()
{
int cost=0;
while(dijkstra())
{
int v=t,flow=inf;
while(pre[v])
{
int u=pre[v],i=preve[v];
flow=min(flow,c[i].z);
v=u;
}
v=t;
while(pre[v])
{
int u=pre[v],i=preve[v];
c[i].z-=flow;
c[i^1].z+=flow;
v=u;
}
cost+=dis[t]*flow;
//cout<<cost<<endl;
}
return cost;
}
void work()
{
cin>>n>>m;
num=1;
for(int i=1;i<=2*n+m+2;++i) head[i]=0;
int res=0;
for(int i=1;i<2*n;++i)
{
add(i,i+1,-inf,(i+1)/2);
add(i,i+1,0,n);
res+=(i+1)/2*(-inf);
}
s=2*n+m+1,t=2*n+m+2;
add(2*n,t,0,n);
int sum=0;
for(int i=1;i<=m;++i)
{
cin>>l[i];
sum+=l[i];
add(s,2*n+i,-inf,l[i]);
add(s,2*n+i,0,n);
res+=l[i]*(-inf);
}
for(int i=1;i<=2*n;++i) cin>>col[i];
for(int i=1;i<=2*n;++i)
{
cin>>v[i];
add(2*n+col[i],i,-v[i],1);
}
if(sum>n)
{
cout<<-1<<endl;
return;
}
int h=mincost();
int ans=res-h;
//cout<<res<<" "<<-h<<endl;
if(ans<0) cout<<-1<<endl;
else cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T=1;
cin>>T;
for(;T;--T)
work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3672kb
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:
-1 -1
result:
wrong answer 1st numbers differ - expected: '9', found: '-1'