QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#590757 | #9315. Rainbow Bracket Sequence | Mastey_ | WA | 1ms | 3896kb | C++14 | 2.3kb | 2024-09-26 11:04:00 | 2024-09-26 11:04:00 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<queue>
#include<vector>
#include<bitset>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pil;
const int maxn=505;
const int maxm=2e3+5;
const ll inf=1e13;
inline int read()
{
int x=0,y=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') y=-1; c=getchar();}
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*y;
}
int n,m;
struct edge
{
int to,next,c,u;
ll w;
}g[maxm<<1];
int head[maxn],cnt(0);
inline void add(int a,int b,int c,ll w)
{
g[cnt].to=b; g[cnt].u=a;
g[cnt].c=c; g[cnt].w=w;
g[cnt].next=head[a];
head[a]=cnt++;
return ;
}
void Add(int a,int b,int c,ll w)
{add(a,b,c,w); add(b,a,0,-w);}
ll dis[maxn];
int vis[maxn],pre[maxn];
bool spfa(int s,int t)
{
memset(dis,-1,sizeof(dis));
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s); dis[s]=0; vis[s]=1;
while(!q.empty())
{
int u=q.front(); q.pop(); vis[u]=0;
for(int i=head[u];i!=-1;i=g[i].next)
{
int v=g[i].to;
if(!g[i].c) continue;
if(dis[v]<dis[u]+g[i].w)
{
dis[v]=dis[u]+g[i].w;
pre[v]=i;
if(!vis[v]) q.push(v),vis[v]=1;
}
}
}
return (pre[t]!=-1);
}
pil EK(int s,int t)
{
ll ans(0); int res=0;
while(spfa(s,t))
{
int mn=2e9;
for(int i=t;i!=s;i=g[pre[i]].u)
mn=min(mn,g[pre[i]].c);
for(int i=t;i!=s;i=g[pre[i]].u)
{
g[pre[i]].c-=mn;
g[pre[i]^1].c+=mn;
ans+=mn*g[pre[i]].w;
}
res+=mn;
}
return make_pair(res,ans);
}
int lim[maxn],tot(0);
int S,T,col[maxn];
ll val[maxn];
void solve()
{
tot=0;
memset(head,-1,sizeof(head)); cnt=0;
n=read(); m=read();
S=n*2+m+1; T=n*2+m+2;
for(int i=1;i<=m;i++) lim[i]=read(),tot+=lim[i];
for(int i=1;i<=2*n;i+=2) Add(S,i,1,0);
for(int i=2;i<=2*n;i++) Add(i,i-1,2e9,0);
for(int i=1;i<=2*n;i++) col[i]=read();
for(int i=1;i<=n*2;i++)
{
val[i]=read();
Add(i,2*n+col[i],1,val[i]);
}
for(int i=1;i<=m;i++) Add(2*n+i,T,lim[i],inf);
for(int i=1;i<=m;i++) Add(2*n+i,T,2e9,0);
pil tmp=EK(S,T);
if((tmp.second/inf)!=tot) printf("-1\n");
else printf("%lld\n",tmp.second%inf);
return ;
}
int main()
{
//freopen("test.in","w",stdout);
int T=read();
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3744kb
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: 3896kb
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 2351080746 3426965219 -1 -1 1351561190 2539318868 1013080942 4656646546 -1 -1 2231197660 2719131728 3983627641 4363462818 -1 1046749330 6115214757 3920988203 -1 3902088946 -1 2566553992 5268471900 5977120748 7446849937 -1 5054275471 1467678317 883992368 1044562986 -1 4024634503 -1 14472...
result:
wrong answer 16th numbers differ - expected: '4712174168', found: '4363462818'