結果

問題 No.114 遠い未来
ユーザー yaoshimaxyaoshimax
提出日時 2015-03-28 19:42:15
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,523 bytes
コンパイル時間 1,607 ms
コンパイル使用メモリ 100,312 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2023-09-11 10:55:23
合計ジャッジ時間 17,533 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 297 ms
7,552 KB
testcase_01 AC 4,802 ms
4,376 KB
testcase_02 AC 113 ms
4,380 KB
testcase_03 AC 99 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 19 ms
4,380 KB
testcase_06 AC 1,683 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 33 ms
4,376 KB
testcase_10 AC 1,850 ms
4,524 KB
testcase_11 TLE -
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 <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];
vector<pair<int,int> > edge[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;
      edge[a].push_back(make_pair(-c,b));
      edge[b].push_back(make_pair(-c,a));
   }
   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++;
           }
         }
         priority_queue<pair<int,int> > pq;
         int curBest[N];
         for( int j =0;j<N;j++)curBest[j]=INT_MAX/2;
         pq.push(make_pair(0,important[0]));
         int score = 0;
         int n=0;
         while(!pq.empty()){
            pair<int,int> p=pq.top();
            pq.pop();
            int len=-p.first;
            int nxt=p.second;
            //cout << len <<" " << nxt << ", ";
            if( !use[nxt] ) continue;
            n++;
            score+=len;
            use[nxt]=false;
            for( int j=0;j<(int)edge[nxt].size();j++){
               if( use[edge[nxt][j].second]&&-edge[nxt][j].first < curBest[edge[nxt][j].second]){
                  pq.push(edge[nxt][j]);
                  curBest[edge[nxt][j].second]=-edge[nxt][j].first;
               }
            }
         }
         //cout << score << endl;
         if( n == size )ans=min(score,ans);
      }
      cout << ans;
   }
   return 0;
}
0