QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#577958 | #9315. Rainbow Bracket Sequence | DBsoleil | WA | 0ms | 3896kb | C++14 | 3.3kb | 2024-09-20 15:41:15 | 2024-09-20 15:41:16 |
Judging History
answer
#define pb push_back
#define fi first
#define se second
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N=505,M=2005;
const int INF=0x3f3f3f3f;
int T,n,m;
int l[N],c[N],v[N],ss[N];
void input()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d",&l[i]);
for(int i=1;i<=n*2;i++) scanf("%d",&c[i]);
for(int i=1;i<=n*2;i++) scanf("%d",&v[i]);
}
bool spjudge()
{
for(int i=1,tot=0,cnt;i<=m;i++)
{
cnt=0,tot+=l[i];
for(int j=1;j<=n*2;j++) if(c[j]==i) cnt++;
if(cnt%2||tot>n) return 0;
}
return 1;
}
struct hlflow
{
struct edge{int u,v,c,w;}p[M];
int n,m,S,T,tot,ans;
int head[N],nxt[M],deg[N];
int dis[N],flow[N],lst[N];
void init(int nw)
{
n=nw,m=1,S=++n,T=++n,ans=tot=0;
for(int i=1;i<=n;i++) head[i]=0,deg[i]=0;
}
void ade(int u,int v,int c,int w)
{
if(c<=0) return;
//cerr<<u<<' '<<v<<' '<<'('<<c<<','<<w<<')'<<endl;
p[++m]={u,v,c,w},nxt[m]=head[u],head[u]=m;
p[++m]={v,u,0,-w},nxt[m]=head[v],head[v]=m;
}
void addedge(int u,int v,int l,int h,int w) {deg[u]-=l,deg[v]+=l,ans+=l*w,ade(u,v,h-l,w);}
void build()
{
for(int i=1;i<=n-2;i++)
{
if(deg[i]>0) ade(S,i,deg[i],0);
if(deg[i]<0) ade(i,T,-deg[i],0);
}
}
bool dijkstra(int S,int T)
{
priority_queue<pii> pq;
for(int i=1;i<=n;i++) dis[i]=-INF,flow[i]=0,lst[i]=-1;
dis[S]=0,flow[S]=INF,pq.push({0,S});
//cerr<<' '<<S<<endl;
while(!pq.empty())
{
auto [d,u]=pq.top(); pq.pop();
if(d!=dis[u]) continue;
//cerr<<' '<<u<<' '<<flow[u]<<' '<<dis[u]<<endl;
for(int i=head[u];i;i=nxt[i]) if(p[i].c>0)
{
auto [u,v,c,w]=p[i];
if(dis[v]<d+w)
{
dis[v]=d+w,flow[v]=min(c,flow[u]),lst[v]=i;
pq.push(pii{dis[v],v});
}
}
}
//cerr<<' '<<flow[T]<<' '<<dis[T]<<endl;
return flow[T]>0&&dis[T]>0;
}
void doflow(int S,int T)
{
tot+=flow[T],ans+=flow[T]*dis[T];
for(int x=T;lst[x]>0;x=p[lst[x]].u)
{
int a=lst[x],b=a^1;
p[a].c-=flow[T],p[b].c+=flow[T];
}
}
int solve()
{
while(dijkstra(S,T)) doflow(S,T);
for(int i=head[S];i;i=nxt[i]) if(p[i].c>0) return -1;
for(int i=head[T];i;i=nxt[i]) if(p[i^1].c>0) return -1;
return ans;
}
}flow;
void build()
{
flow.init(n*2+m+2);
int S=n*2+m+1,T=n*2+m+2;
for(int i=1;i<=m;i++) flow.addedge(S,n*2+i,l[i],INF,0);
for(int i=1;i<=n*2;i++)
{
//cerr<<i<<' '<<n*2+c[i]<<' '<<i<<endl;
flow.addedge(n*2+c[i],i,0,1,v[i]);
if(i<n*2) flow.addedge(i,i+1,i/2,n,0);
}
flow.addedge(n*2,T,n,n,0);
flow.addedge(T,S,0,INF,0);
flow.build();
//cerr<<' '<<flow.n<<endl;
}
void solve()
{
input();
if(!spjudge()) {printf("-1\n"); return;}
build();
int ans=flow.solve();
printf("%d\n",ans);
}
int main()
{
scanf("%d",&T);
while(T--) solve();
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3896kb
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'