結果
| 問題 | 
                            No.114 遠い未来
                             | 
                    
| コンテスト | |
| ユーザー | 
                             yaoshimax
                         | 
                    
| 提出日時 | 2015-03-28 19:17:25 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 3,323 bytes | 
| コンパイル時間 | 816 ms | 
| コンパイル使用メモリ | 94,332 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-29 01:37:15 | 
| 合計ジャッジ時間 | 28,408 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 24 TLE * 1 | 
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#define STEINER_SIZE 13
using namespace std;
int dist[35][35];
bool isImportant[35];
vector<int> important;
vector<int> optional;
int steiner_dp[1<<STEINER_SIZE][35];
int main(){
   int N,M,T;
   cin>>N>>M>>T;
   for(int i=0;i<35;i++)for(int j=0;j<35;j++) dist[i][j]=INT_MAX/2;
   for(int i=0;i<35;i++) isImportant[i]=false;
   for(int i = 0 ; i <M; i++ ){
      int a,b,c;
      cin>>a>>b>>c;
      a--;b--;
      dist[a][b]=dist[b][a]=c;
   }
   for(int i=0; i<T;i++){
      int a;
      cin>>a;
      a--;
      isImportant[a]=true;
      important.push_back(a);
   }
   for( int i=0;i<N;i++){
      if(!isImportant[i])optional.push_back(i);
   }
   sort(important.begin(),important.end());
   for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++) dist[j][k]=min(dist[j][k],dist[j][i]+dist[i][k]);
   for(int i=0;i<N;i++) dist[i][i]=0;
   if( T<=STEINER_SIZE ){
      for(int i=0;i<(1<<T);i++)for(int j=0;j<N;j++) steiner_dp[i][j]=INT_MAX/2;
      int ans = INT_MAX;
      for(int i=0;i<N;i++){
         for( int j = 0 ; j < T; j++ ){
            steiner_dp[1<<j][i]=dist[important[j]][i];
         }
      }
      for(int i = 1; i <(1<<T); i++ ){
         if( (i&(i-1)) == 0 ) continue;
         for( int p=0;p<N;p++){
            for( int j = 1; j <i ; j++ ){
               if( (i&j)==j ){
                  steiner_dp[i][p]=min(steiner_dp[i][p],steiner_dp[j][p]+steiner_dp[i-j][p]);
               }
            }
         }
         for(int p=0;p<N;p++){
            for(int q=0;q<N;q++){
               steiner_dp[i][p]=min(steiner_dp[i][p],steiner_dp[i][q]+dist[p][q]);
            }
         }
      }
      for( int i =0;i<N;i++){
         ans=min(steiner_dp[(1<<T)-1][i],ans);
      }
      cout << ans<<endl;
   }
   else{
      int ans = INT_MAX;
      for(int i=0;i<(1<<(N-T));i++){
         bool use[N];
         memset(use,false,sizeof(use));
         int size = T;
         for(int j=0;j<T;j++){
            use[important[j]]=true;
         }
         for( int j=0;j<(N-T);j++){
           if( (i&(1<<j)) ){
            use[optional[j]]=true;
            size++;
           }
         }
         int curDist[N];
         for( int j=0 ; j<N;j++){
            if( use[j] )curDist[j]=dist[important[0]][j];
            else curDist[j]=INT_MAX/2;
         }
         int score = 0;
         curDist[important[0]]=INT_MAX/2;
         for( int j=1; j<size; j++){
           int nxt=0;
           int d=curDist[nxt];
           for( int k=0;k<N;k++){
            if(curDist[k]<d){
               nxt=k;
               d=curDist[k];
            }
           }
           //cout << d << ", ";
           score+=d;
           curDist[nxt]=INT_MAX/2;
           for( int update=0;update<N;update++){
            if( curDist[update]==INT_MAX/2 ) continue;
            curDist[update]=min(curDist[update],dist[nxt][update]);
           }
         }
         //cout << score << endl;
         ans=min(score,ans);
      }
      cout << ans;
   }
   return 0;
}
            
            
            
        
            
yaoshimax