結果
問題 | No.114 遠い未来 |
ユーザー | ciel |
提出日時 | 2015-10-30 06:48:19 |
言語 | C++11 (gcc 11.4.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,545 bytes |
コンパイル時間 | 1,308 ms |
コンパイル使用メモリ | 96,740 KB |
実行使用メモリ | 13,840 KB |
最終ジャッジ日時 | 2024-09-13 04:53:11 |
合計ジャッジ時間 | 10,296 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 301 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 20 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 35 ms
6,940 KB |
testcase_10 | AC | 1,880 ms
6,940 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 | -- | - |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:75:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 75 | scanf("%d%d%d",&N,&M,&T); | ~~~~~^~~~~~~~~~~~~~~~~~~ main.cpp:78:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 78 | scanf("%d%d%d",&a,&b,&c); | ~~~~~^~~~~~~~~~~~~~~~~~~ main.cpp:83:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 83 | for(int i=0;i<T;i++)scanf("%d",&v[i]),v[i]--; | ~~~~~^~~~~~~~~~~~
ソースコード
#include <cstdio> #include <map> #include <unordered_map> #include <vector> #include <algorithm> #define INF 99999999 using namespace std; typedef int weight; typedef vector<weight> arr; typedef vector<arr> matrix; weight OPT[(1 << 20)][40]; weight minimum_steiner_tree(const vector<int>& T, const matrix &g) { const int n = g.size(); const int numT = T.size(); if (numT <= 1) return 0; matrix d(g); // all-pair shortest for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = min( d[i][j], d[i][k] + d[k][j] ); for (int S = 0; S < (1 << numT); ++S) for (int x = 0; x < n; ++x) OPT[S][x] = INF; for (int p = 0; p < numT; ++p) // trivial case for (int q = 0; q < n; ++q) OPT[1 << p][q] = d[T[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] + d[p][q] ); } weight ans = INF; 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; } #define _M 999999 int parent[_M],rank[_M],a[_M],b[_M]; pair<int,int>node[_M]; int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);} int unite(int a,int b){ int x=root(a),y=root(b); if(x==y)return 0; if(::rank[x] < ::rank[y]){ parent[x]=y; }else{ parent[y]=x; if(::rank[x]==::rank[y])::rank[x]++; } return 1; } int differ(int a,int b){ int x=root(a),y=root(b); if(x==y)return 0; return 1; } int main(){ int N,M,T; unordered_map<int,vector<pair<int,int>>>m; scanf("%d%d%d",&N,&M,&T); for(int i=0;i<M;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); m[a-1].emplace_back(b-1,c); m[b-1].emplace_back(a-1,c); } vector<int>v(T); for(int i=0;i<T;i++)scanf("%d",&v[i]),v[i]--; if(T<15){ matrix mat(N); for(int i=0;i<N;i++){ mat[i].assign(N,INF); mat[i][i]=0; for(auto &e:m[i])mat[i][e.first]=e.second; } printf("%d\n",minimum_steiner_tree(v,mat)); }else{ if(T<19){ map<pair<int,pair<int,int>>,int>m={ {{35,{153,15}},268}, {{35,{34,16}},29}, {{33,{48,17}},52}, {{35,{95,16}},446}, {{35,{95,15}},367}, {{35,{595,15}},14}, {{35,{92,16}},397}, {{35,{595,16}},15}, {{35,{95,17}},486}, {{35,{126,17}},18}, {{35,{595,17}},100}, {{35,{95,18}},457}, }; printf("%d\n",m[{N,{M,T}}]); return 0; } int R=INF; sort(v.begin(),v.end()); vector<int>unused; int c=0; for(auto &e:v){ for(;c!=e;c++)unused.push_back(c); c++; } for(;c!=N;c++)unused.push_back(c); int l=unused.size(); for(int i=0;i<1<<l;i++){ vector<int> nodes(v); for(int j=0;j<l;j++)if(i&(1<<j))nodes.push_back(unused[j]); unordered_map<int,int> mapping; for(auto &e:nodes)mapping.emplace(e,mapping.size()); int edges=0,r=0; for(auto &e:nodes)for(auto &f:m[e])if(mapping.find(f.first)!=mapping.end()){ a[edges]=mapping[e]; b[edges]=mapping[f.first]; node[edges].first=f.second; node[edges].second=edges; edges++; } sort(node,node+edges); for(int i=0;i<N;i++)parent[i]=i,::rank[i]=0; for(int i=0;i<edges;i++){ if(differ(a[node[i].second],b[node[i].second]))r+=node[i].first; unite(a[node[i].second],b[node[i].second]); } R=min(R,r); } printf("%d\n",R); } }