QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#572141#9315. Rainbow Bracket SequenceOIer_kzcWA 0ms1600kbC++142.5kb2024-09-18 12:30:182024-09-18 12:30:24

Judging History

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

  • [2024-09-18 12:30:24]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:1600kb
  • [2024-09-18 12:30:18]
  • 提交

answer

#include <stdio.h>
#include <string.h>

#include <vector>
#include <algorithm>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back

using namespace std;

typedef long long LL;
constexpr int N = 305, M = 1005;

int n, m;
int a[N], b[N], c[N];

int S, T;
int h[N], e[M], ne[M], w[M], v[M], idx;
int lev[N], dis[N], cur[N], q[N], hh, tt;
bool wq[N];
int maxf; LL cost;

void add(int x, int y, int c, int d) {
	ne[++idx] = h[x], h[x] = idx, e[idx] = y, w[idx] = c, v[idx] = d;
	ne[++idx] = h[y], h[y] = idx, e[idx] = x, w[idx] = 0, v[idx] = -d;
}

bool spfa() {
	memset(lev, 0, T + 1 << 2);
	memset(dis, 0x3f, T + 1 << 2);
	memcpy(cur, h, T + 1 << 2);
	lev[S] = 1, dis[S] = 0;
	*q = S, hh = 0, tt = 1;
	while (hh != tt) {
		int x = q[hh++];
		if (hh == N) hh = 0;
		wq[x] = false;
		for (int i = h[x], y; i; i = ne[i]) {
			if (w[i] && dis[y = e[i]] > dis[x] + v[i]) {
				lev[y] = lev[x] + 1;
				dis[y] = dis[x] + v[i];
				if (wq[y]) {
					continue;
				}
				wq[y] = true;
				if (dis[y] <= dis[q[hh]]) {
					if (hh == 0) hh = N;
					q[--hh] = y;
				} else {
					q[tt++] = y;
					if (tt == N) tt = 0;
				}
			}
		}
	}
	return lev[T] > 0;
}

int find(int x, int limit) {
	if (x == T) {
		return limit;
	}
	int flow = 0, t;
	for (int i = cur[x], y; i && flow < limit; i = ne[i]) {
		cur[x] = i, y = e[i];
		if (lev[y] == lev[x] + 1 && dis[y] == dis[x] + v[i] && w[i]) {
			t = find(y, min(limit - flow, w[i]));
			if (!t) lev[y] = 0;
			w[i] -= t, w[i ^ 1] += t, flow += t, cost += t * v[i];
		}
	}
	return flow;
}

void dinic() {
	while (spfa()) {
		maxf += find(S, n);
	}
}

LL solve() {
	for (int i = 1; i <= m; ++i) {
		if (a[i] < 0) {
			return -1;
		}
	}
	S = 0, T = 2 * n + m + 1;
	for (int i = 1; i <= n; ++i) {
		add(2 * i, 2 * i - 1, i - 1, 0);
	}
	add(S, 2 * n, n, 0);
	for (int i = 1; i < n; ++i) {
		add(2 * i + 1, 2 * i, n, 0);
	}
	LL sum = 0;
	for (int i = 1; i <= 2 * n; ++i) {
		add(i, c[i] + 2 * n, 1, b[i]);
		sum += b[i];
	}
	for (int i = 1; i <= m; ++i) {
		add(2 * n + i, T, a[i], 0);
	}
	dinic();
	return maxf != n ? -1 : sum - cost;
}

int main() {
	int task;
	for (scanf("%d", &task); task--; ) {
		scanf("%d%d", &n, &m);
		memset(h, 0, sizeof h), idx = 1, cost = maxf = 0;
		for (int i = 1; i <= m; ++i) {
			scanf("%d", a + i);
			a[i] = -a[i];
		}
		for (int i = 1; i <= 2 * n; ++i) {
			scanf("%d", c + i);
			++a[c[i]];
		}
		for (int i = 1; i <= 2 * n; ++i) {
			scanf("%d", b + i);
		}
		printf("%lld\n", solve());
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
-1
-1
-1
2231197660
2719131728
3983627641
4712174168
-1
1046749330
6115214757
3920988203
-1
3902088946
-1
2566553992
-1
5977120748
7505501534
-1
5054275471
1467678317
883992368
1044562986
-1
4024634503
-1
1447294256
6757607722...

result:

wrong answer 10th numbers differ - expected: '4656646546', found: '-1'