結果
問題 | No.114 遠い未来 |
ユーザー | msm1993 |
提出日時 | 2020-06-02 10:04:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 673 ms / 5,000 ms |
コード長 | 3,754 bytes |
コンパイル時間 | 1,990 ms |
コンパイル使用メモリ | 181,368 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-23 18:22:15 |
合計ジャッジ時間 | 6,844 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 24 ms
5,248 KB |
testcase_01 | AC | 619 ms
5,248 KB |
testcase_02 | AC | 201 ms
5,248 KB |
testcase_03 | AC | 32 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 4 ms
5,248 KB |
testcase_06 | AC | 354 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 7 ms
5,248 KB |
testcase_10 | AC | 112 ms
5,248 KB |
testcase_11 | AC | 673 ms
5,248 KB |
testcase_12 | AC | 517 ms
5,248 KB |
testcase_13 | AC | 360 ms
5,248 KB |
testcase_14 | AC | 258 ms
5,248 KB |
testcase_15 | AC | 193 ms
5,248 KB |
testcase_16 | AC | 160 ms
5,248 KB |
testcase_17 | AC | 102 ms
5,248 KB |
testcase_18 | AC | 128 ms
5,248 KB |
testcase_19 | AC | 62 ms
5,248 KB |
testcase_20 | AC | 18 ms
5,248 KB |
testcase_21 | AC | 6 ms
5,248 KB |
testcase_22 | AC | 6 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 1 ms
5,248 KB |
ソースコード
#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 << endlconst 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 };int OPT[1 << 14][40];int minimum_steiner_tree(const vector<int>& T, const VVI &g) {//prefieldconst int n = g.size();const int numT = T.size();if (numT <= 1) return 0;VVI d(g); // all-pair shortest/// FOR(i, 0, n)d[i][i] = 0;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 p = 0; p < numT; ++p) // trivial casefor (int q = 0; q < n; ++q)OPT[1 << p][q] = d[T[p]][q];for (int i = 1; i<1 << numT; i++)if (((i - 1) & i) != 0) {for (int j = 0; j<n; j++) {OPT[i][j] = INF;for (int k = (i - 1)&i; k > 0; k = (k - 1) & i) {OPT[i][j] = min(OPT[i][j], OPT[k][j] + OPT[i^k][j]);}}for (int j = 0; j<n; j++) {for (int k = 0; k<n; k++) {OPT[i][j] = min(OPT[i][j], OPT[i][k] + d[k][j]);}}}return OPT[(1 << numT) - 1][T[0]];}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(const vector<edge>& es,const VI &use,const VI& Ts ,LL cur) {//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;if (cur < min_cost)return INF;uf.unionSet(es[i].f, es[i].t);}}FOR(i, 0, SZ(Ts)) {if (!uf.same(Ts[0], Ts[i]))return INF;}return min_cost;}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]--;}LL ans;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,t,ans));FOR(i, 0, cn) {if (state & 1 << i) {now_using[candidates[i]] = 0;}}}}cout << ans << "\n";return 0;}