結果

問題 No.114 遠い未来
ユーザー Yang33Yang33
提出日時 2018-04-25 02:26:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,467 bytes
コンパイル時間 3,009 ms
コンパイル使用メモリ 180,840 KB
実行使用メモリ 7,620 KB
最終ジャッジ日時 2023-09-10 05:19:07
合計ジャッジ時間 14,539 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 278 ms
4,380 KB
testcase_01 AC 1,344 ms
4,384 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 19 ms
4,380 KB
testcase_06 AC 520 ms
4,388 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 32 ms
4,380 KB
testcase_10 AC 1,646 ms
4,736 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/24  Problem: yukicoder 114  / Link: http://yukicoder.me/problems/no/114  ----- */
/* ------問題------

簡単に言うと、無向でコスト付きのグラフが与えられる。
グラフの中の点のいくつかは"重要"な点だと考えられている。
そのいくつかの重要な頂点を"全て"連結にするような部分木で木の辺のコストの和が最小となるようなものを考えたい。
その時のコストの和を出力するという問題。

-----問題ここまで----- */
/* -----解説等-----

場合分け解法をね

----解説ここまで---- */

int OPT[1 << 14][40];

int minimum_steiner_tree(const vector<int>& T, const VVI &g) {//prefield
	const int n = g.size();
	const int numT = T.size();
	if (numT <= 1) return 0;
	VVI d(g); // all-pair shortest
	for (int k = 0; k < n; ++k)
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

	for (int S = 0; S < (1 << numT); ++S)
		for (int x = 0; x < n; ++x)
			OPT[S][x] = INF;

	for (int p = 0; p < numT; ++p) // trivial case
		for (int q = 0; q < n; ++q)
			OPT[1 << p][q] = d[T[p]][q];

	for (int S = 1; S < (1 << numT); ++S) { // DP step
		if (!(S & (S - 1))) continue;
		for (int p = 0; p < n; ++p)
			for (int E = 0; E < S; ++E)
				if ((E | S) == S)
					OPT[S][p] = min(OPT[S][p], OPT[E][p] + OPT[S - E][p]);
		for (int p = 0; p < n; ++p)
			for (int q = 0; q < n; ++q)
				OPT[S][p] = min(OPT[S][p], OPT[S][q] + d[p][q]);
	}

	int ans = INF;
	for (int S = 0; S < (1 << numT); ++S)
		for (int q = 0; q < n; ++q)
			ans = min(ans, OPT[S][q] + OPT[((1 << numT) - 1) - S][q]);

	return ans;
}


struct UnionFind {
	vector<int> data;
	UnionFind(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

struct edge {
	int f, t, c;
	edge() {}
	edge(int x, int y, int z) :f(x), t(y), c(z) {}
	bool operator < (const edge &e) const { return c < e.c; };
};


LL kruskal(vector<edge>& es, VI &use) {
	//sort(es.begin(), es.end());
	UnionFind uf(SZ(use));
	LL min_cost = 0;
	FOR(i, 0, SZ(es)) {
		if (use[es[i].f] && use[es[i].t])
			if (!uf.same(es[i].f, es[i].t)) {
				min_cost += es[i].c;
				uf.unionSet(es[i].f, es[i].t);
			}
	}
	FOR(i, 0, SZ(use)) {
		if (!uf.same(use[0], use[i]))return INF;
	}

	return min_cost;
}




LL ans = 0LL;

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

	int N, M, T; cin >> N >> M >> T;
	VVI G(N, VI(N, INF));
	FOR(i, 0, N)G[i][i] = 0;
	vector<edge>edges(M);
	FOR(i, 0, M) {
		int a, b, c;
		cin >> a >> b >> c;
		a--, b--;
		G[a][b] = G[b][a] = c;
		edges[i] = edge(a, b, c);
	}

	VI t(T);
	FOR(i, 0, T) {
		cin >> t[i];
		t[i]--;
	}
	if (T < 14) {
		ans = minimum_steiner_tree(t, G);
	}
	else { // Tに含まれないものを全探索
		VI now_using(N, 0);
		FOR(i, 0, T)now_using[t[i]] = 1;
		int cn = N - T;
		VI candidates;
		FOR(i, 0, N) {
			if (!now_using[i])candidates.push_back(i);
		}

		SORT(edges);
		ans = INF;
		FOR(state, 0, 1 << cn) {
			FOR(i, 0, cn) {
				if (state & 1 << i) {
					now_using[candidates[i]] = 1;
				}
			}
			ans = min(ans, kruskal(edges, now_using));
			FOR(i, 0, cn) {
				if (state & 1 << i) {
					now_using[candidates[i]] = 0;
				}
			}

		}

	}

	cout << ans << "\n";

	return 0;
}
0