QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#576929 | #9315. Rainbow Bracket Sequence | Glastrier | WA | 1ms | 3824kb | C++20 | 2.3kb | 2024-09-19 23:26:20 | 2024-09-19 23:26:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
const int maxm=2e5+5;
#define ll long long
const ll inf=1e12;
struct node{
int to;
int nxt;
ll v;
ll c;
}e[maxm];
int n,m,s,t,cnt=1,head[maxn],pre[maxn];
ll cost[maxn],flow[maxn];
bool vis[maxn];
void add(int x,int y,ll w,ll c)
{
e[++cnt].to=y;
e[cnt].v=w;
e[cnt].nxt=head[x];
e[cnt].c=c;
head[x]=cnt;
e[++cnt].to=x;
e[cnt].v=0;
e[cnt].nxt=head[y];
e[cnt].c=-c;
head[y]=cnt;
}
bool spfa()
{
vis[s]=true;
for(int i=1;i<=t;i++)
{
cost[i]=flow[i]=inf;
pre[i]=-1;
vis[i]=0;
}
queue<int>q;
cost[s]=0;
q.push(s);
pre[s]=0;
vis[s]=true;
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false;
for(int i=head[x];i;i=e[i].nxt)
{
if(e[i].v>0&&cost[e[i].to]>cost[x]+e[i].c)
{
cost[e[i].to]=cost[x]+e[i].c;
flow[e[i].to]=min(flow[x],e[i].v);
pre[e[i].to]=i;
if(!vis[e[i].to])
{
q.push(e[i].to);
vis[e[i].to]=true;
}
}
}
}
/*printf("SPFA Result\n");
printf("------------------------\n");
for(int i=1;i<=n;i++)
{
printf("%d %d\n",i,pre[i]);
}
printf("-----------------------\n");*/
return pre[t]!=-1;
}
int limit[maxn],co[maxn],po[maxn];
void pr()
{
for(int i=1;i<=t;i++) head[i]=0;
}
void solve()
{
scanf("%d%d",&n,&m);
pr();
cnt=1;
int sum=0;
for(int i=1;i<=m;i++)
{
scanf("%d",&limit[i]);
sum+=limit[i];
}
for(int i=1;i<=2*n;i++)
{
scanf("%d",&co[i]);
}
for(int i=1;i<=2*n;i++)
{
scanf("%d",&po[i]);
}
if(sum>n)
{
printf("-1\n");
return;
}
s=2*n+m+1,t=2*n+m+2;
add(s,1,n,0);
int now=1;
for(int i=1;i<=2*n;i+=2)
{
add(i,i+1,n-now,0);
now++;
if(i+1!=2*n) add(i+1,i+2,inf,0);
}
for(int i=1;i<=2*n;i++)
{
add(i,2*n+co[i],1,-po[i]);
}
for(int i=1;i<=m;i++)
{
add(2*n+i,t,inf,0);
}
ll mf=0,mc=0;
while(spfa())
{
mf+=flow[t];
mc+=flow[t]*cost[t];
for(int i=t;pre[i];i=e[pre[i]^1].to)
{
e[pre[i]].v-=flow[t];
e[pre[i]^1].v+=flow[t];
}
}
printf("%lld\n",mf==n?mc*-1:-1);
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
solve();
}
/*int u,v,w,c;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&w,&c);
add(u,v,w,c);
}*/
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3796kb
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: 3824kb
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:
5813317795 343766215 2351080746 3426965219 3079410450 1497027962 1351561190 2539318868 1013080942 5372255482 1311169903 5938245406 2231197660 2719131728 3983627641 5756953475 3121174891 1046749330 6115214757 4997298213 3914858167 3902088946 4270179075 2566553992 6469784940 5977120748 7505501534 5951...
result:
wrong answer 1st numbers differ - expected: '-1', found: '5813317795'