QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#567425#9315. Rainbow Bracket Sequencewym1111WA 1ms3940kbC++172.9kb2024-09-16 11:53:242024-09-16 11:53:25

Judging History

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

  • [2024-09-16 11:53:25]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3940kb
  • [2024-09-16 11:53:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define endl '\n'
const int N = 1e3 + 10;
const ll INF = 0x3f3f3f3f3f3f3f3f;

class Dinic_ssp {
private:
	struct Edge {
		int from, to;
		ll cap, flow, val;
	};
	vector<Edge> edges;
	vector<int> g[N];
	ll dis[N], cur[N], mincost;
	bool vis[N];
	
public:
	void init (int n) {
		for (int i = 1; i <= n; i ++) {
			g[i].clear();
		}
		edges.clear();
		mincost = 0;
	}
	void addEdge (int u, int v, int c, int w) {
		edges.push_back({u, v, c, 0, w});
		edges.push_back({v, u, 0, 0, -w});
		int m = edges.size();
		g[u].push_back(m - 2);
		g[v].push_back(m - 1);
	}
	bool spfa (int s, int t) {
		memset(dis, 0x3f, sizeof dis);
		memset(cur, 0, sizeof cur);
		queue<int> q;
		dis[s] = 0;
		q.push(s);
		vis[s] = 1;
		while (!q.empty()) {
			int x = q.front();
			q.pop();
			vis[x] = 0;
//			cout << x << ' ' << dis[x] << endl;
			for (auto &i: g[x]) {
				Edge &e = edges[i];
				if (e.cap > e.flow && dis[e.to] > dis[x] + e.val) {
					dis[e.to] = dis[x] + e.val;
					if (!vis[e.to]) {
						q.push(e.to);
						vis[e.to] = 1;
					}
				}
			}
		}
		return dis[t] != INF;
	}
	int dfs (int x, int t, ll flow) {
		if (x == t || flow == 0) return flow;
		ll res = 0;
		vis[x] = 1;
		for (int i = cur[x]; i < (int)g[x].size(); i ++) {
			cur[x] = i;
			Edge &e = edges[g[x][i]];
			if (dis[e.to] == dis[x] + e.val && !vis[e.to]) {
				ll d = dfs(e.to, t, min(flow - res, e.cap - e.flow));
				res += d;
				mincost += d * e.val;
				edges[g[x][i]].flow += d;
				edges[g[x][i] ^ 1].flow -= d;
				if (res == flow) {
					vis[x] = 0;
					return flow;
				}
			}
		}
		vis[x] = 0;
		return res;
	}
	pair<ll, ll> mcmf (int s, int t) {
		ll flow = 0;
		while (spfa(s, t)) {
			while (int d = dfs(s, t, INF)) {
				flow += d;
			}
		}
		return {flow, mincost};
	}
};
Dinic_ssp dinic;

int n, m;
int cnt[N], lim[N], col[N];
ll val[N], sum;

void init () {
	cin >> n >> m;
	n *= 2;
	sum = 0;
	for (int i = 1; i <= m; i ++) {
		cin >> lim[i];
		cnt[i] = 0;
	}
	for (int i = 1; i <= n; i ++) {
		cin >> col[i];
		cnt[col[i]] ++;
	}
	for (int i = 1; i <= n; i ++) {
		cin >> val[i];
		sum += val[i];
	}
}

void solve() {
	init();
	int s = 0, t = n + m + 1;
	dinic.init(t);
	
	dinic.addEdge(s, n, n, 0);
	for (int i = 1; i <= n; i ++) {
		if (i < n) dinic.addEdge(i + 1, i, i / 2, 0);
		dinic.addEdge(i, n + col[i], 1, val[i]);
	}
	for (int i = 1; i <= m; i ++) {
		dinic.addEdge(n + i, t, max(0, cnt[i] - lim[i]), 0);
	}
	
	auto [flow, cost] = dinic.mcmf(s, t);
	
//	cout << n << ' ' << flow << ' ' << cost << endl;
	
	if (flow != n / 2) {
		cout << "-1\n";
		return;
	}
	cout << sum - cost << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--){
		solve();
	}
	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: 1ms
memory: 3724kb

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
1497027962
1351561190
2539318868
1013080942
4656646546
-1
-1
2231197660
2719131728
3983627641
4712174168
3121174891
1046749330
6115214757
3920988203
3914858167
3902088946
-1
2566553992
5268471900
5977120748
7505501534
-1
5054275471
1467678317
883992368
104456298...

result:

wrong answer 6th numbers differ - expected: '-1', found: '1497027962'