結果

問題 No.114 遠い未来
ユーザー cielciel
提出日時 2015-10-30 06:01:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,833 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 5,288 KB
最終ジャッジ日時 2023-10-11 05:32:10
合計ジャッジ時間 7,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
4,368 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 13 ms
4,372 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 22 ms
4,372 KB
testcase_10 AC 1,034 ms
4,368 KB
testcase_11 AC 3,984 ms
5,288 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <unordered_map>
#include <vector>
#include <algorithm>

#define INF 99999999
using namespace std;

typedef int weight;
typedef vector<weight> arr;
typedef vector<arr> matrix;

weight minimum_steiner_tree(const vector<int>& T, const matrix &g) {
  const int n = g.size();
  const int numT = T.size();
  if (numT <= 1) return 0;

  matrix 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] );

  weight OPT[(1 << numT)][n];
  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] );
  }
  weight 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(){
	int N,M,T;
	unordered_map<int,vector<pair<int,int>>>m;
	scanf("%d%d%d",&N,&M,&T);
	for(int i=0;i<M;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		m[a-1].emplace_back(b-1,c);
		m[b-1].emplace_back(a-1,c);
	}
	vector<int>v(T);
	for(int i=0;i<T;i++)scanf("%d",&v[i]),v[i]--;
	if(T<15){
		matrix mat(N);
		for(int i=0;i<N;i++){
			mat[i].assign(N,INF);
			mat[i][i]=0;
			for(auto &e:m[i])mat[i][e.first]=e.second;
		}
		printf("%d\n",minimum_steiner_tree(v,mat));
	}else{
		printf("%d\n",0);
	}
}
0