QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570584 | #9315. Rainbow Bracket Sequence | suyue | WA | 2ms | 12252kb | C++14 | 5.1kb | 2024-09-17 16:36:56 | 2024-09-17 16:37:10 |
Judging History
answer
#include<stdio.h>
#include<algorithm>
#include<queue>
#define N 100010
#define M 200010
#define INF 0x3fffffffffffffffll
#define int long long
#define ll long long
using namespace std;
int read(){
int res=0,zf=1;char ch;
while((ch=getchar())<48||ch>57)if(ch=='-')zf=!zf;res=(ch^48);
while((ch=getchar())>=48&&ch<=57)res=(res<<3)+(res<<1)+(ch^48);
return zf?res:(-res);
}
ll readll(){
ll res=0;int zf=1;char ch;
while((ch=getchar())<48||ch>57)if(ch=='-')zf=!zf;res=(ch^48);
while((ch=getchar())>=48&&ch<=57)res=(res<<3)+(res<<1)+(ch^48);
return zf?res:(-res);
}
struct node{
int y,nxt;ll s,cost;
}b[M];
ll d[N],h[N];int v[N],sta[N],cur[N],lenb;
int n,m,ds,bs;
int yuan,hui,yuan2,hui2;
int spfa(){
for(int i=1;i<=ds;i++){
d[i]=INF;
v[i]=0;
}
d[yuan]=0;
queue<int>q;
q.push(yuan);
while(!q.empty()){
int x=q.front();q.pop();v[x]=0;
for(int i=sta[x];i;i=b[i].nxt)if(b[i].s&&d[b[i].y]>d[x]+b[i].cost){
d[b[i].y]=d[x]+b[i].cost;
if(!v[b[i].y]){
v[b[i].y]=1;
q.push(b[i].y);
}
}
}
return d[hui]<INF;
}
int dij(){
for(int i=1;i<=ds;i++){
d[i]=INF;v[i]=0;
}
d[yuan]=0;
priority_queue<pair<ll,int> >q;
q.push(make_pair(0LL,yuan));
while(!q.empty()){
int x=q.top().second;q.pop();
if(v[x])continue;
v[x]=1;
for(int i=sta[x];i;i=b[i].nxt){
int y=b[i].y;ll nc=b[i].cost+h[x]-h[y];
if(!b[i].s||d[y]<=d[x]+nc)continue;
d[y]=d[x]+nc;
if(!v[y])q.push(make_pair(-d[y],y));
}
}
for(int i=1;i<=ds;i++){
v[i]=0;
cur[i]=sta[i];
}
return d[hui]<INF;
}
pair<ll,ll>dinic(int x,ll flow){
if(x==hui)return make_pair(flow,0ll);
ll lastflow=flow,jscost=0;
v[x]=1;
for(int i=cur[x];i&&lastflow;i=b[i].nxt)if(!v[b[i].y]&&b[i].s&&h[b[i].y]==h[x]+b[i].cost){
pair<ll,ll>k=dinic(b[i].y,min(b[i].s,lastflow));
lastflow-=k.first;b[i].s-=k.first;b[i^1].s+=k.first;
jscost+=k.second+k.first*b[i].cost;
}
return make_pair(flow-lastflow,jscost);
}
int merge(int x,int y,ll s,ll cost){
// printf("!%lld %lld %lld %lld\n",x,y,s,cost);
if(s<=0)return 0;
b[++lenb].y=y;
b[lenb].nxt=sta[x];
sta[x]=lenb;
b[lenb].s=s;
b[lenb].cost=cost;
b[++lenb].y=x;
b[lenb].nxt=sta[y];
sta[y]=lenb;
b[lenb].s=0;
b[lenb].cost=-cost;
return 0;
}
ll insx[N],outsx[N];
int makeedge(int x,int y,ll mins,ll maxs,ll cost){
merge(x,y,maxs-mins,cost);
insx[y]+=mins;
outsx[x]+=mins;
return 0;
}
int COL[N],VAL[N];
signed main(){
int t=read();
while(t--){
n=read();m=read();
lenb=1;
yuan2=n*2+m+1;
hui2=n*2+m+2;
yuan=n*2+m+3;
hui=n*2+m+4;
ds=n*2+m+4;
for(int i=0;i<=ds;i++){
d[i]=h[i]=0;
v[i]=sta[i]=cur[i]=0;
}
for(int i=1;i<=m;i++){
int Lm=read();
makeedge(yuan2,i+n*2,Lm,n,0);
}
for(int i=1;i<=n*2;i++)COL[i]=read();
for(int i=1;i<=n*2;i++)VAL[i]=read();
for(int i=1;i<=n*2;i++){
makeedge(n*2+COL[i],i,0,1,-VAL[i]);
}
for(int i=1;i<n*2;i++){
makeedge(i,i+1,(i+1)/2,i,0);
}
makeedge(n*2,hui2,n,n,0);
ll cntins=0;
ll ppp=0;
for(int i=1;i<=ds-2;i++){
// printf("P%d %lld %lld\n",i,insx[i],outsx[i],insx[i]-outsx[i]);
ppp+=insx[i]-outsx[i];
if(insx[i]>outsx[i]){
merge(yuan,i,insx[i]-outsx[i],0);
cntins+=insx[i]-outsx[i];
}
if(outsx[i]>insx[i]){
merge(i,hui,outsx[i]-insx[i],0);
}
}
// printf("PPP%lld\n",ppp);
merge(yuan2,yuan,INF,0);
merge(hui2,hui,INF,0);
bs=lenb;
for(int i=1;i<=ds;i++)h[i]=d[i];
ll jsflow=0,jscost=0;
while(spfa()){
for(int i=1;i<=ds;i++){
cur[i]=sta[i];
h[i]=d[i];
}
pair<ll,ll>k=dinic(yuan,INF);
jsflow+=k.first;
jscost+=k.second;
// printf("!%lld %lld\n",jsflow,jscost);
}
// printf("??????%lld %lld\n",cntins,jsflow);
if(jsflow!=cntins){
puts("-1");continue;
}
for(int x=1;x<=ds-2;x++){
if(b[sta[x]].y==yuan||b[sta[x]].y==hui)sta[x]=0;
for(int i=sta[x];i;i=b[i].nxt)if(b[b[i].nxt].y==yuan||b[b[i].nxt].y==hui)b[i].nxt=0;
}
yuan=yuan2;
hui=hui2;
spfa();
for(int i=1;i<=ds-2;i++)h[i]=d[i];
while(dij()){
for(int i=1;i<=ds-2;i++)h[i]+=d[i];
pair<ll,ll>k=dinic(yuan,INF);
jsflow+=k.first;
jscost+=k.second;
}
printf("%lld\n",-jscost);
}
return 0;
}
/*
2
3 2
1 2
1 2 2 2 1 2
3 1 4 2 2 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12212kb
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: 0ms
memory: 12252kb
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 0 853325162 1248537157 -1 1230630334 0 3334103581 0 -1 -1 -1 -1 -1 -1 -1 979996637 0 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 0 0 0 -1 -1 -1 0 -1 -1 -1 -1 -1 -1 2674641740 0 -1 0 0 0 1598389115 0
result:
wrong answer 2nd numbers differ - expected: '343766215', found: '0'