QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#669927#9315. Rainbow Bracket SequencefutarianWA 0ms3996kbC++143.6kb2024-10-23 20:03:352024-10-23 20:03:36

Judging History

This is the latest submission verdict.

  • [2024-10-23 20:03:36]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3996kb
  • [2024-10-23 20:03:35]
  • Submitted

answer

#include "bits/stdc++.h"
using namespace std;
#define int ll
#define ll long long
const int Len = 515 , inf = 1e9;
const ll Inf = 1e13;
ll dis[Len],fl,co;
int c[Len << 1],v[Len << 1],l[Len],n,m,head[Len],cur[Len],cnt = 1,vis[Len],flag[Len],S,T,SS,TT;
int val[Len],M;
struct node
{
	int next,to,w,ww;
	node(){next = to = w = ww = 0;}
	node(int NEXT,int TO,int W,int WW){next = NEXT , to = TO , w = W , ww = WW;}
}edge[Len << 1];
inline void add(int from,int to,int w,int ww)
{
	edge[++ cnt].to = to;
	edge[cnt].next = head[from];
	edge[cnt].w = w;
	edge[cnt].ww = ww;
	head[from] = cnt;
}
inline void adeg(int from,int to,int w,int ww)
{
	add(from , to , w , ww);
	add(to , from , 0 , -ww);
}
queue<int> Q;
inline int Dij()
{
	while(!Q.empty()) Q.pop();
	for(int i = 1 ; i <= TT ; i ++) dis[i] = Inf , cur[i] = vis[i] = flag[i] = 0;
	dis[SS] = 0 , cur[SS] = head[SS] , vis[SS] = 1 , Q.push(SS);
	while(!Q.empty())
	{
		int p = Q.front();Q.pop();
		vis[p] = 0;
		for(int e = head[p] ; e ; e = edge[e].next)
		{
			int to = edge[e].to;
			if(edge[e].w && dis[to] > dis[p] + edge[e].ww)
			{
				dis[to] = dis[p] + edge[e].ww;
				cur[to] = head[to];
				if(!vis[to]) vis[to] = 1 , Q.push(to);
			}
		}	
	} 
	if(dis[TT] == Inf) return 0;
	return 1;
}
int dfs(int u,int In)
{
	//printf("woeee%lld\n",In);
	if(u == TT) return In;
	flag[u] = 1;
	int Out = 0;
	for(int e = cur[u] ; e && In > 0 ; e = edge[e].next)
	{
		cur[u] = e;
		int to = edge[e].to;
		if(edge[e].w && dis[to] == dis[u] + edge[e].ww && !flag[to])
		{
			int res = dfs(to , min(In , edge[e].w));
			In -= res;
			Out += res;
			edge[e].w -= res;
			edge[e ^ 1].w += res;
			co += res * edge[e].ww;
		}
	}
	flag[u] = 0;
	if(!Out) return dis[u] = 0;
	return Out;
}
struct addedg
{
	int u,v,l,r,ww;
	addedg(){u = v = l = r = ww = 0;}
	addedg(int U,int V,int L,int R,int WW){u = U , v = V , l = L , r = R , ww = WW;}
}edd[Len << 2];
inline void I(int u,int v,int l,int r,int ww){edd[++ M] = addedg(u , v , l , r , ww);}
inline void BuildGraph()
{
	for(int i = 1 ; i <= M ; i ++)
	{
		int u,v,L,R,ww;u = edd[i].u , v = edd[i].v , L = edd[i].l , R = edd[i].r , ww = edd[i].ww;
		edge[cnt + 1].ww = L;
		val[v] += L , val[u] -= L;
		adeg(v , u , R - L , ww);
	}
	
	SS = T + 1 , TT = SS + 1;int sm = 0;
	for(int i = 1 ; i <= T ; i ++) 
	{
		if(val[i] < 0) adeg(SS , i , -val[i] , 0);
		else adeg(i , TT , val[i] , 0) , sm += val[i];
	}
	adeg(S , T , Inf , 0);
	//puts("doijocdjossjio");
	int as = 0;
	while(Dij()) 
	{
		as += dfs(SS , inf);
		//printf("%lld\n",as);
	}
	
	if(as != sm) 
	{
		puts("-1");
		return;
	}
	as = edge[cnt].w;
	edge[cnt].w = edge[cnt ^ 1].w = 0;
	SS = T , TT = S;
	while(Dij()) as += dfs(SS , Inf);
	printf("%lld\n",-co);
}
signed main()
{
	int ttt;scanf("%lld",&ttt);
	while(ttt --)
	{
		scanf("%lld %lld",&n,&m);
		for(int i = 1 ; i <= m ; i ++) scanf("%lld",&l[i]);
		for(int i = 1 ; i <= (n << 1) ; i ++) scanf("%lld",&c[i]);
		for(int i = 1 ; i <= (n << 1) ; i ++) scanf("%lld",&v[i]);
		S = 2 * n + m + 2 , T = 2 * n + m + 3 , SS = T + 1 , TT = SS + 1;
		for(int i = 1 ; i <= TT ; i ++) head[i] = 0;cnt = 1;
 		for(int i = 1 ; i <= m ; i ++) I(S , i , l[i] , inf , 0);
		for(int i = 1 ; i <= (n << 1) ; i ++) I(c[i] , m + i , 0 , 1 , -v[i]);
		for(int i = 1 ; i <= (n << 1) ; i ++) I(m + i , m + i + 1 , (i >> 1) + (i & 1) , inf , 0);
		I(S - 1 , T , n , n , 0);
		//puts("doijocdjossjio");
		BuildGraph();
		for(int i = 0 ; i <= cnt ; i ++) edge[i] = node(0 , 0 , 0 , 0);
		fl = co = M = 0 , cnt = 1;for(int i = 1 ; i <= TT ; i ++) head[i] = val[i] = 0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3940kb

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

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
3426965219
-1
-1
1351561190
-1
1013080942
-1
-1
-1
2231197660
-1
3983627641
-1
-1
1046749330
-1
3920988203
-1
3902088946
-1
2566553992
-1
5977120748
-1
-1
5054275471
1467678317
883992368
-1
-1
4024634503
-1
1447294256
-1
3688048676
-1
-1
6213150585
6293910825
4391400665
3381015576
-1...

result:

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