답안 #401759

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401759 2021-05-10T19:12:02 Z Jorge213 통행료 (APIO13_toll) C++14
78 / 100
2500 ms 14740 KB
#include <bits/stdc++.h>
using namespace std;

struct Edge {
	int u, v;
	long long w;
	bool operator<(const Edge &e) const {
		return w < e.w;
	}
};

struct DSU {
	vector<int> dsu;

	DSU(int n): dsu(n) {
		for (int i = 1; i < n; i++)
			dsu[i] = i;
	}

	int root(int idx) {
		while (idx != dsu[idx]) {
			dsu[idx] = dsu[dsu[idx]];
			idx = dsu[idx];
		}
		return idx;
	}

	bool join(int a, int b) {
		if (root(a) == root(b))
			return false;
		dsu[root(a)] = root(b);
		return true;
	}
};

struct Tree {
	vector<vector<pair<int, long long>>> adjList;
	vector<long long> we, cnt;
	vector<int> d, par;

	Tree(int sz, vector<long long> &a): 
		adjList(sz), we(sz), d(sz, 0), par(sz), cnt(a) {};

	void addEdge(int u, int v, long long w) {
		adjList[u].push_back({v, w});
		adjList[v].push_back({u, w});
	}

	void dfs(int v, int p) {
		par[v] = p;
		d[v] = d[p] + 1;
		for (auto [u, w] : adjList[v]) {
			if (u == p) continue;
			dfs(u, v);
			we[u] = w;
			cnt[v] += cnt[u];
		}
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m, k;
	cin >> n >> m >> k; 

	vector<Edge> edges, nedges;
	vector<long long> a(n + 1);

	int ai, bi;
	long long ci;
	for (int i = 0; i < m; i++) {
		cin >> ai >> bi >> ci;
		edges.push_back({ai, bi, ci});
	}

	sort(edges.begin(), edges.end());

	int xi, yi;
	for (int i = 0; i < k; i++) {
		cin >> xi >> yi;
		nedges.push_back({xi, yi, 0});
	}

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}

	DSU dsu(n + 1), ndsu(n + 1);
	for (auto [u, v, w] : nedges) {
		ndsu.join(u, v);
	}

	for (auto [u, v, w] : edges) {
		if (ndsu.join(u, v))
			dsu.join(u, v);
	}

	int sz = 0;
	vector<int> comp(n + 1, 0);
	vector<long long> p = {0};

	for (int i = 1; i <= n; i++) {
		int v = dsu.root(i);
		if (!comp[v]) {
			comp[v] = ++sz;
			p.push_back(0);
		}
		comp[i] = comp[v];
		p[comp[i]] += a[i];
	}

	for (auto &[u, v, w] : nedges) {
		u = comp[u], v = comp[v];
	}

	DSU dsu2(sz + 1);
	vector<Edge> mn;
	for (auto [u, v, w] : edges) {
		if (dsu2.join(comp[u], comp[v])) {
			mn.push_back({comp[u], comp[v], w});
		}
	}

	long long ans = 0;
	for (int bm = 1; bm < (1 << k); bm++) {
		Tree g(sz + 1, p);
		DSU st(sz + 1);
		int ecnt = 0;

		for (int i = 0; i < k; i++) {
			if (!(bm & (1 << i))) continue;
			auto [u, v, w] = nedges[i];
			if (st.join(u, v)) {
				g.addEdge(u, v, LLONG_MAX);
				ecnt++;
			}
		}

		vector<Edge> unused;
		for (auto [u, v, w] : mn) {
			if (st.join(u, v)) {
				g.addEdge(u, v, 0);
				ecnt++;
			}
			else {
				unused.push_back({u, v, w});
			}
		}

		g.dfs(1, -1);
		for (auto [u, v, w] : unused) {
			if (g.d[u] < g.d[v])
				swap(u, v);

			while (g.d[u] > g.d[v]) {
				g.we[u] = min(g.we[u], w);
				u = g.par[u];
			}
		
			while (u != v) {
				g.we[u] = min(g.we[u], w);
				g.we[v] = min(g.we[v], w);
				u = g.par[u], v = g.par[v];
			}
		}

		long long can = 0;
		for (int v = 1; v <= sz; v++) {
			can += g.we[v] * g.cnt[v];
		}

		ans = max(ans, can);
	}

	cout << ans << '\n';

	return 0;
}

Compilation message

toll.cpp: In constructor 'Tree::Tree(int, std::vector<long long int>&)':
toll.cpp:39:17: warning: 'Tree::par' will be initialized after [-Wreorder]
   39 |  vector<int> d, par;
      |                 ^~~
toll.cpp:38:24: warning:   'std::vector<long long int> Tree::cnt' [-Wreorder]
   38 |  vector<long long> we, cnt;
      |                        ^~~
toll.cpp:41:2: warning:   when initialized here [-Wreorder]
   41 |  Tree(int sz, vector<long long> &a):
      |  ^~~~
toll.cpp: In member function 'void Tree::dfs(int, int)':
toll.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |   for (auto [u, w] : adjList[v]) {
      |             ^
toll.cpp: In function 'int main()':
toll.cpp:91:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |  for (auto [u, v, w] : nedges) {
      |            ^
toll.cpp:95:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   95 |  for (auto [u, v, w] : edges) {
      |            ^
toll.cpp:114:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  114 |  for (auto &[u, v, w] : nedges) {
      |             ^
toll.cpp:120:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  120 |  for (auto [u, v, w] : edges) {
      |            ^
toll.cpp:134:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  134 |    auto [u, v, w] = nedges[i];
      |         ^
toll.cpp:142:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  142 |   for (auto [u, v, w] : mn) {
      |             ^
toll.cpp:153:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  153 |   for (auto [u, v, w] : unused) {
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 2 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 2 ms 204 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 4 ms 556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 2 ms 204 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 4 ms 556 KB Output is correct
7 Correct 222 ms 14740 KB Output is correct
8 Correct 242 ms 14644 KB Output is correct
9 Correct 246 ms 14648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 2 ms 204 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 4 ms 556 KB Output is correct
7 Correct 222 ms 14740 KB Output is correct
8 Correct 242 ms 14644 KB Output is correct
9 Correct 246 ms 14648 KB Output is correct
10 Execution timed out 2570 ms 14624 KB Time limit exceeded
11 Halted 0 ms 0 KB -