QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#576907#9315. Rainbow Bracket SequenceGlastrierWA 1ms3876kbC++202.3kb2024-09-19 23:17:342024-09-19 23:17:34

Judging History

你现在查看的是最新测评结果

  • [2024-09-19 23:17:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3876kb
  • [2024-09-19 23:17:34]
  • 提交

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,limit[i],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: 3800kb

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: 3876kb

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
-1
-1
-1
-1
-1
2539318868
1013080942
-1
-1
-1
2231197660
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
5268471900
-1
-1
-1
-1
-1
883992368
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1309966981
-1
-1
-1
1373323696

result:

wrong answer 3rd numbers differ - expected: '2351080746', found: '-1'