Submission #64787
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <bits/stdc++.h> using namespace std; using Int = long long ; const char newl = '\n' ; template < typename T1, typename T2> inline void chmin(T1 &a,T2 b){ if (a>b) a=b;} template < typename T1, typename T2> inline void chmax(T1 &a,T2 b){ if (a<b) a=b;} template < typename T> void drop( const T &x){cout<<x<<endl; exit (0);} template < typename T=Int> vector<T> read( size_t n){ vector<T> ts(n); for ( size_t i=0;i<n;i++) cin>>ts[i]; return ts; } template < typename T> vector<T> make_v( size_t a){ return vector<T>(a);} template < typename T, typename ... Ts> auto make_v( size_t a,Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); } template < typename T, typename U, typename ... V> typename enable_if<is_same<T, U>::value!=0>::type fill_v(U &u, const V... v){u=U(v...);} template < typename T, typename U, typename ... V> typename enable_if<is_same<T, U>::value==0>::type fill_v(U &u, const V... v){ for (auto &e:u) fill_v<T>(e,v...); } //INSERT ABOVE HERE const Int MAX = 13; const Int INF = 1e9; Int dp[1<<MAX]; Int es[MAX][MAX]; // i -> b Int fs[MAX][1<<MAX]; // i <- b Int ts[MAX][1<<MAX]; signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int n,m; cin>>n>>m; for (Int i=0;i<MAX;i++) for (Int j=0;j<MAX;j++) es[i][j]=INF; for (Int i=0;i<m;i++){ Int u,v,c; cin>>u>>v>>c; u--;v--; chmin(es[u][v],c); } const Int sz = 1<<n; for (Int i=0;i<n;i++){ for (Int b=0;b<sz;b++){ fs[i][b]=ts[i][b]=INF; for (Int j=0;j<n;j++){ if ((~b>>j)&1) continue ; chmin(fs[i][b],es[i][j]); chmin(ts[i][b],es[j][i]); } } } for (Int x=0;x<sz;x++) dp[x]=INF; auto nx=make_v<Int>(n,sz); fill_v<Int>(nx,INF); auto time =make_v<Int>(n,sz); fill_v<Int>( time ,-1); dp[1]=0; for (Int x=1;x<sz;x++){ auto touch=[&](Int i,Int bit){ if ( time [i][bit]<x){ nx[i][bit]=INF; time [i][bit]=x; } }; for (Int i=0;i<n;i++){ if ((x>>i)&1) continue ; touch(i,x|(1<<i)); nx[i][x|(1<<i)]=dp[x]+ts[i][x]; } for (Int z=~x&(sz-1);z>=0;z=(z-1)&~x&(sz-1)){ Int y=~z&(sz-1); for (Int i=0;i<n;i++){ if ((~y>>i)&1) continue ; touch(i,y); for (Int j=0;j<n;j++){ if ((x>>j)&1){ chmin(dp[y],nx[i][y]+es[i][j]); } else { touch(j,y|(1<<j)); chmin(nx[j][y|(1<<j)],nx[i][y]+es[i][j]); } } } if (z==0) break ; } } cout<<dp[sz-1]<<endl; return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 1460 - 高速道路網の再編 |
ユーザー名 | syoribu |
投稿日時 | 2020-11-20 12:27:32 |
言語 | C++17 |
状態 | Accepted |
得点 | 11 |
ソースコード長 | 2609 Byte |
最大実行時間 | 473 ms |
最大メモリ使用量 | 4216 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 11 / 11 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1 | AC | 30 ms | 604 KB |
1
|
in2 | AC | 21 ms | 612 KB |
1
|
in3 | AC | 25 ms | 712 KB |
1
|
in4 | AC | 28 ms | 660 KB |
1
|
in5 | AC | 19 ms | 572 KB |
1
|
in6 | AC | 31 ms | 604 KB |
1
|
in7 | AC | 28 ms | 672 KB |
1
|
in8 | AC | 19 ms | 604 KB |
1
|
in9 | AC | 20 ms | 668 KB |
1
|
in10 | AC | 26 ms | 540 KB |
1
|
in11 | AC | 29 ms | 676 KB |
1
|
in12 | AC | 60 ms | 1300 KB |
1
|
in13 | AC | 22 ms | 560 KB |
1
|
in14 | AC | 17 ms | 572 KB |
1
|
in15 | AC | 24 ms | 684 KB |
1
|
in16 | AC | 40 ms | 928 KB |
1
|
in17 | AC | 38 ms | 976 KB |
1
|
in18 | AC | 38 ms | 1024 KB |
1
|
in19 | AC | 30 ms | 1076 KB |
1
|
in20 | AC | 38 ms | 1036 KB |
1
|
in21 | AC | 55 ms | 1476 KB |
1
|
in22 | AC | 62 ms | 1500 KB |
1
|
in23 | AC | 137 ms | 2420 KB |
1
|
in24 | AC | 151 ms | 2344 KB |
1
|
in25 | AC | 27 ms | 600 KB |
1
|
in26 | AC | 22 ms | 744 KB |
1
|
in27 | AC | 21 ms | 852 KB |
1
|
in28 | AC | 30 ms | 1096 KB |
1
|
in29 | AC | 446 ms | 4216 KB |
1
|
in30 | AC | 472 ms | 3956 KB |
1
|
in31 | AC | 473 ms | 4032 KB |
1
|
in32 | AC | 466 ms | 3980 KB |
1
|
in33 | AC | 415 ms | 3928 KB |
1
|
in34 | AC | 421 ms | 4140 KB |
1
|
in35 | AC | 435 ms | 3960 KB |
1
|
in36 | AC | 452 ms | 4040 KB |
1
|