結果

問題 No.114 遠い未来
ユーザー krotonkroton
提出日時 2014-12-29 01:28:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,485 ms / 5,000 ms
コード長 2,538 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 150,708 KB
実行使用メモリ 7,468 KB
最終ジャッジ日時 2023-09-03 19:41:37
合計ジャッジ時間 16,458 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
4,380 KB
testcase_01 AC 1,262 ms
4,376 KB
testcase_02 AC 174 ms
4,376 KB
testcase_03 AC 37 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 526 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 146 ms
5,088 KB
testcase_11 AC 428 ms
7,468 KB
testcase_12 AC 1,024 ms
4,380 KB
testcase_13 AC 3,485 ms
4,380 KB
testcase_14 AC 534 ms
4,380 KB
testcase_15 AC 1,751 ms
4,380 KB
testcase_16 AC 261 ms
4,380 KB
testcase_17 AC 329 ms
4,380 KB
testcase_18 AC 1,175 ms
4,380 KB
testcase_19 AC 128 ms
4,380 KB
testcase_20 AC 138 ms
4,376 KB
testcase_21 AC 10 ms
4,380 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int INF = 1 << 26;
struct Edge {
	int u, v, c;
	bool operator<(const Edge& other) const {
		return c < other.c;
	}
};

int N, M, T, ts[50];
Edge E[777];
int dist[50][50];

int root[50];
inline int find(int x){
	if(x == root[x])return x;
	return root[x] = find(root[x]);
}
inline bool conn(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y)return false;

	root[x] = y;
	return true;
}


int rest_v[50];
int use[50];
inline int min_spanning_tree(){
	for(int i=0;i<N;i++)root[i] = i;

	int res = 0;
	for(int i=0;i<M;i++){
		int u = E[i].u, v = E[i].v, c = E[i].c;
		if(!use[u] || !use[v])continue;
		if(conn(u, v)){
			res += c;
		}
	}

	for(int i=0;i<T;i++){
		if(find(ts[i]) != find(ts[0]))return INF;
	}

	return res;
}

int OPT[1<<15][50];
int min_steiner_tree(){
  for(int i=0;i<N;i++)for(int j=0;j<N;j++)dist[i][j] = INF;
  for(int i=0;i<N;i++)dist[i][i] = 0;
  for(int i=0;i<M;i++){
  	int u = E[i].u, v = E[i].v, c = E[i].c;
  	dist[u][v] = c;
  	dist[v][u] = c;
  }

  const int n = N;
  const int numT = T;
  if (numT <= 1) return 0;

  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] = dist[ts[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 = (S-1)&S; E ; E = (E-1)&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] + dist[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;
}

int main(){
	cin >> N >> M >> T;
	for(int i=0;i<M;i++){
		int u, v, c;
		cin >> u >> v >> c;
		E[i] = Edge{u - 1, v - 1, c};
	}
	for(int i=0;i<T;i++)cin >> ts[i], ts[i]--;

	int res;
	if(T <= 14){
		res = min_steiner_tree();
	} else {
		for(int i=0;i<T;i++)use[ts[i]] = true;

		int rest = 0;
		for(int i=0;i<N;i++)if(!use[i]){
			rest_v[rest++] = i;
		}

		sort(E, E + M);
		res = INF;

		for(int mask=0;mask<1<<rest;mask++){
			for(int i=0;i<rest;i++){
				if((mask >> i) & 1){
					use[rest_v[i]] = true;
				}
			}

			res = min(res, min_spanning_tree());

			for(int i=0;i<rest;i++){
				if((mask >> i) & 1){
					use[rest_v[i]] = false;
				}
			}
		}
	}

	cout << res << endl;
	return 0;
}
0