QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578059 | #9315. Rainbow Bracket Sequence | Yoralen | WA | 1ms | 8048kb | C++14 | 2.0kb | 2024-09-20 16:16:32 | 2024-09-20 16:16:33 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
const int N=100005,inf=1e9;
int cnt=1,h[N],d[N],vis[N],fr[N],Ot[N],In[N];
int cost,cst,q[N],s,t,S,T,flow,n,m;
int val[N],lim[N],tot,c[N];
struct Edge{int to,next,v,c,f;}w[N<<1];
void Add(int u,int v,int L,int R,int val){
cst+=L*val;In[v]+=L,Ot[u]+=L;
w[++cnt].to=v,w[cnt].f=u,w[cnt].next=h[u],h[u]=cnt,w[cnt].v=val,w[cnt].c=R-L;
w[++cnt].to=u,w[cnt].f=v,w[cnt].next=h[v],h[v]=cnt,w[cnt].v=-val,w[cnt].c=0;
}
void Addv(int u,int v,int c,int val){
w[++cnt].to=v,w[cnt].f=u,w[cnt].next=h[u],h[u]=cnt,w[cnt].v=val,w[cnt].c=c;
w[++cnt].to=u,w[cnt].f=v,w[cnt].next=h[v],h[v]=cnt,w[cnt].v=-val,w[cnt].c=0;
}
bool spfa(){
int l=1,r=1,i;q[1]=S;
for(i=1;i<=tot;i++)d[i]=inf;d[S]=0;vis[S]=1;
while(l<=r){
int u=q[l];l++;vis[u]=0;
for(i=h[u];i;i=w[i].next){
if(!w[i].c)continue;
int e=w[i].to;
if(d[e]>d[u]+w[i].v){
d[e]=d[u]+w[i].v;fr[e]=i;
if(!vis[e])q[++r]=e,vis[e]=1;
}
}
}return d[T]!=inf;
}
void deal(){
int u=T,res=inf;
while(u^S)res=min(res,w[fr[u]].c),u=w[fr[u]].f;
flow+=res,cost+=res*d[T];u=T;
while(u^S)w[fr[u]].c-=res,w[fr[u]^1].c+=res,u=w[fr[u]].f;
}
void work(){cost=0;while(spfa()){deal();};}
int main(){
int Te,i;
scanf("%d",&Te);
while(Te--){
scanf("%d%d",&n,&m);n<<=1;
s=m+n+1,t=m+n+2;int limit=t;
S=t+1,T=S+1;tot=T;
for(i=1;i<=T;i++)h[i]=In[i]=Ot[i]=fr[i]=0;
cnt=1;cst=flow=cost=0;
for(i=1;i<=m;i++)scanf("%d",&lim[i]),Add(s,i,lim[i],n/2,0);
for(i=1;i<=n;i++)scanf("%d",&c[i]);
for(i=1;i<=n;i++){
scanf("%d",&val[i]);
Add(c[i],i+m,0,1,-val[i]);
if(i!=n)Add(i+m,i+m+1,(i+1)/2,n/2,0);
}
Add(n+m,t,n/2,n/2,0);
Addv(t,s,inf,0);
int fs=0,ft=0;
for(i=1;i<=limit;i++){
if(In[i]>=Ot[i])Addv(S,i,In[i]-Ot[i],0),fs+=In[i]-Ot[i];
else Addv(i,T,Ot[i]-In[i],0),ft+=Ot[i]-In[i];
}
// printf("%d %d\n",fs,ft);
work();//printf("!%d\n",flow);
if(flow!=fs)puts("-1");
else printf("%d\n",cst-cost);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7964kb
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: 8048kb
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 -1943886550 -868002077 -1 -1 1351561190 -1755648428 1013080942 361679250 -1 -1 -2063769636 -1575835568 -311339655 417206872 -1 1046749330 1820247461 -373979093 -1 -392878350 -1 -1728413304 973504604 1682153452 -1084433058 -1 759308175 1467678317 883992368 1044562986 -1 -270332793 -1 144...
result:
wrong answer 3rd numbers differ - expected: '2351080746', found: '-1943886550'