結果

問題 No.114 遠い未来
ユーザー 👑 kmjpkmjp
提出日時 2014-12-29 00:01:18
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,155 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 156,996 KB
実行使用メモリ 12,996 KB
最終ジャッジ日時 2023-09-03 18:55:08
合計ジャッジ時間 8,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
4,380 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
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;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

class UF {
	public:
	static const int ufmax=5052;
	int ufpar[ufmax],ufrank[ufmax],ufcnt[ufmax];
	UF() { int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; ufcnt[i]=1; } }
	int operator[](int x) {return (ufpar[x]==x)?(x):(ufpar[x] = operator[](ufpar[x]));}
	int count(int x) { return ufcnt[operator[](x)];}
	void unite(int x,int y) {
		x = operator[](x); y = operator[](y);
		if(x==y) return;
		if(ufrank[x]<ufrank[y]) ufpar[x]=y, ufcnt[y]+=ufcnt[x];
		else {ufpar[y]=x; ufcnt[x]+=ufcnt[y]; if(ufrank[x]==ufrank[y]) ufrank[x]++;}
	}
};

int N,M,T;
int A[2000],B[2000],C[2000];
vector<int> V;
int mat[40][40];

int ok[40];
int OPT[(1 << 15)][40];

int minimum_steiner_tree() {
  const int n = N;
  const int numT = V.size();
  if (numT <= 1) return 0;

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

  for (int p = 0; p < numT; ++p) // trivial case
    for (int q = 0; q < n; ++q)
      OPT[1 << p][q] = mat[V[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] + mat[p][q] );
  }
  int ans = 100000;
  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 dodo(ll mask) {
	UF uf;
	priority_queue<pair<int,int> > Q;
	int x,y;
	FOR(x,N) FOR(y,N) if(x!=y && (mask&(1LL<<x)) && (mask&(1LL<<y))) Q.push(make_pair(-mat[x][y],x*100+y));
	
	int tot=0;
	while(Q.size()) {
		pair<int,int> p=Q.top();
		Q.pop();
		x=p.second/100, y=p.second%100;
		if(uf[x]!=uf[y]) {
			tot+=-p.first;
			uf.unite(x,y);
		}
	}
	return tot;
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	UF uf;
	
	FOR(x,40) FOR(y,40) mat[x][y]=100000000;
	FOR(x,40) mat[x][x]=0;
	cin>>N>>M>>T;
	FOR(i,M) {
		cin>>A[i]>>B[i]>>C[i];
		mat[A[i]-1][B[i]-1]=mat[B[i]-1][A[i]-1]=C[i];
	}
	FOR(i,N) FOR(x,N) FOR(y,N) mat[x][y]=min(mat[x][y],mat[x][i]+mat[i][y]);
	FOR(i,T) cin>>x, V.push_back(x-1), ok[V[i]]=1;
	
	if(T<=15) {
		cout<<minimum_steiner_tree()<<endl;
	}
	else {
		vector<int> V2;
		FOR(i,N) if(ok[i]==0) V2.push_back(i);
		int mi=10000000;
		ll mask=0;
		FOR(i,N) if(ok[i]) mask|=1LL<<i;
		FOR(i,1<<V2.size()) {
			FOR(j,V2.size()) if(i&(1LL<<j)) mask |= 1LL<<V2[j];
			mi=min(mi,dodo(mask));
		}
		cout<<mi<<endl;
		
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0