結果

問題 No.114 遠い未来
ユーザー yaoshimaxyaoshimax
提出日時 2015-03-28 19:17:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,455 ms / 5,000 ms
コード長 3,323 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 95,264 KB
実行使用メモリ 4,540 KB
最終ジャッジ日時 2023-09-11 10:54:12
合計ジャッジ時間 26,615 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 234 ms
4,376 KB
testcase_01 AC 3,078 ms
4,376 KB
testcase_02 AC 1,457 ms
4,380 KB
testcase_03 AC 147 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 16 ms
4,380 KB
testcase_06 AC 1,576 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 27 ms
4,380 KB
testcase_10 AC 1,438 ms
4,540 KB
testcase_11 AC 4,455 ms
4,380 KB
testcase_12 AC 3,334 ms
4,380 KB
testcase_13 AC 2,432 ms
4,376 KB
testcase_14 AC 1,564 ms
4,376 KB
testcase_15 AC 1,218 ms
4,376 KB
testcase_16 AC 849 ms
4,380 KB
testcase_17 AC 763 ms
4,376 KB
testcase_18 AC 902 ms
4,376 KB
testcase_19 AC 424 ms
4,376 KB
testcase_20 AC 124 ms
4,376 KB
testcase_21 AC 32 ms
4,380 KB
testcase_22 AC 30 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0