結果
| 問題 | No.114 遠い未来 |
| コンテスト | |
| ユーザー |
yaoshimax
|
| 提出日時 | 2015-03-28 19:42:15 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,523 bytes |
| 記録 | |
| コンパイル時間 | 946 ms |
| コンパイル使用メモリ | 101,216 KB |
| 実行使用メモリ | 10,140 KB |
| 最終ジャッジ日時 | 2024-06-29 01:38:00 |
| 合計ジャッジ時間 | 15,291 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 TLE * 1 -- * 16 |
ソースコード
#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;
}
yaoshimax