QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#155478#6870. Coinneko_nyaaAC ✓217ms6640kbC++232.8kb2023-09-01 17:51:592023-09-01 17:51:59

Judging History

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

  • [2023-09-01 17:51:59]
  • 评测
  • 测评结果:AC
  • 用时:217ms
  • 内存:6640kb
  • [2023-09-01 17:51:59]
  • 提交

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct Dinic {
	struct Edge {
		int to, rev;
		ll c, oc;
		ll flow() { return max(oc - c, 0LL); } // if you need flows
	};
	vi lvl, ptr, q;
	vector<vector<Edge>> adj;
	Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
	void addEdge(int a, int b, ll c, int rcap = 0) {
		adj[a].push_back({b, sz(adj[b]), c, c});
		adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap});
	}
	ll dfs(int v, int t, ll f) {
		if (v == t || !f) return f;
		for (int& i = ptr[v]; i < sz(adj[v]); i++) {
			Edge& e = adj[v][i];
			if (lvl[e.to] == lvl[v] + 1)
				if (ll p = dfs(e.to, t, min(f, e.c))) {
					e.c -= p, adj[e.to][e.rev].c += p;
					return p;
				}
		}
		return 0;
	}
	ll calc(int s, int t) {
		ll flow = 0; q[0] = s;
		rep(L,0,31) do { // 'int L=30' maybe faster for random data
			lvl = ptr = vi(sz(q));
			int qi = 0, qe = lvl[s] = 1;
			while (qi < qe && !lvl[t]) {
				int v = q[qi++];
				for (Edge e : adj[v])
					if (!lvl[e.to] && e.c >> (30 - L))
						q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
			}
			while (ll p = dfs(s, t, LLONG_MAX)) flow += p;
		} while (lvl[t]);
		return flow;
	}
	bool leftOfMinCut(int a) { return lvl[a] != 0; }
};

void solve() {
	int n, m, k; cin >> n >> m >> k;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<pair<int, int>> edges;
	vector<vector<int>> needs(n);
	for (int i = 0; i < n; i++) {
		needs[i].push_back(0);
	}
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y; x--, y--;
		edges.emplace_back(x, y);
		needs[x].push_back(i+1);
		needs[y].push_back(i+1);
	}
	for (int i = 0; i < n; i++) {
		needs[i].push_back(m+1);
	}

	vector<int> fr(n);
	for (int i = 0; i < k; i++) {
		int x; cin >> x; x--; fr[x] = 1;
	}

	int sz = 2 + 2*n + 4*m;
	Dinic dc(sz);
	map<pair<int, int>, int> id;
	int source = 0, sink = sz-1;

	int petr = 1; // source = 1, sink = sz-1
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < needs[i].size(); j++) {
			id[{i, needs[i][j]}] = petr; 
			petr++;
		}
		
		dc.addEdge(source, id[{i, needs[i][0]}], 1);
		if (fr[i]) dc.addEdge(id[{i, needs[i].back()}], sink, a[i]);
		for (int j = 1; j < needs[i].size(); j++) {
			dc.addEdge(id[{i, needs[i][j-1]}], id[{i, needs[i][j]}], a[i]);
		}
	}

	for (int i = 1; i <= m; i++) {
		auto [x, y] = edges[i-1];
		dc.addEdge(id[{x, i}], id[{y, i}], 1);
		dc.addEdge(id[{y, i}], id[{x, i}], 1);
	}

	cout << dc.calc(source, sink) << '\n';
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int t; cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 217ms
memory: 6640kb

input:

10
2621 3000 873
542 5 3 3 1 2 8 2 2 4 1 1 2 5 3 1 5 1 2 4 3 1 1 6 3 3 3 1 1 1 1 5 6 2 4 2 2 2 1 5 2 1 3 3 2 2 1 3 1 1 4 3 3 2 4 2 2 1 1 3 1 1 1 2 2 1 2 3 3 2 1 1 5 1 3 3 1 1 3 2 2 2 3 4 4 4 3 1 1 1 1 2 1 1 1 1 1 1 4 2 2 2 2 1 6 2 3 1 3 2 3 1 3 2 1 2 1 3 1 2 1 6 1 3 1 3 2 1 2 2 2 2 4 3 1 3 1 4 1 2 1...

output:

1584
1441
1691
1673
1444
1481
1573
1399
1839
1788

result:

ok 10 lines