結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー | ciel |
提出日時 | 2015-10-24 03:34:51 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 11 ms / 5,000 ms |
コード長 | 1,264 bytes |
コンパイル時間 | 475 ms |
コンパイル使用メモリ | 52,964 KB |
実行使用メモリ | 6,296 KB |
最終ジャッジ日時 | 2023-10-11 05:31:45 |
合計ジャッジ時間 | 1,608 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,076 KB |
testcase_01 | AC | 2 ms
5,012 KB |
testcase_02 | AC | 2 ms
5,044 KB |
testcase_03 | AC | 2 ms
4,976 KB |
testcase_04 | AC | 2 ms
5,036 KB |
testcase_05 | AC | 2 ms
5,076 KB |
testcase_06 | AC | 2 ms
5,020 KB |
testcase_07 | AC | 2 ms
5,032 KB |
testcase_08 | AC | 2 ms
5,024 KB |
testcase_09 | AC | 6 ms
5,652 KB |
testcase_10 | AC | 3 ms
5,144 KB |
testcase_11 | AC | 3 ms
5,172 KB |
testcase_12 | AC | 11 ms
6,296 KB |
testcase_13 | AC | 7 ms
5,736 KB |
testcase_14 | AC | 6 ms
5,668 KB |
testcase_15 | AC | 2 ms
5,152 KB |
testcase_16 | AC | 4 ms
5,360 KB |
testcase_17 | AC | 3 ms
5,128 KB |
ソースコード
#include <cstdio> #include <vector> #include <set> #include <stack> #include <algorithm> #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() using namespace std; typedef int Weight; typedef vector<Weight> Array; typedef vector<Array> Matrix; const int M = 20; Weight best[1<<M][M]; int prev[1<<M][M]; void buildPath(int S, int i, vector<int> &path) { if (!S) return; buildPath(S^(1<<i), ::prev[S][i], path); path.push_back(i); } Weight shortestHamiltonPath(const Matrix &w, vector<int> &path) { int n=w.size(); int N = 1 << n; REP(S,N) REP(i,n) best[S][i] = 0; REP(S,N) REP(j,n) if (!(S&(1<<j))) { int score = 0; REP(i,n) if (S&(1<<i)) score+=w[i][j]; REP(i,n) if (best[S|(1<<j)][j] <= best[S][i] + score) { best[S|(1<<j)][j] = best[S][i] + score; ::prev[S|(1<<j)][j] = i; } } int t = max_element(best[N-1], best[N-1]+n) - best[N-1]; //path.clear(); buildPath(N-1, t, path); return best[N-1][t]; } int main(){ int N,M; scanf("%d%d",&N,&M); Matrix m(N); for(int i=0;i<N;i++)m[i].resize(N); for(;M--;){ int a,b,c; scanf("%d%d%d",&a,&b,&c); m[a][b]=c; } vector<int> path; printf("%d\n",shortestHamiltonPath(m,path)); }