結果
問題 | No.845 最長の切符 |
ユーザー |
|
提出日時 | 2019-06-28 21:56:14 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 334 ms / 3,000 ms |
コード長 | 1,633 bytes |
コンパイル時間 | 1,192 ms |
コンパイル使用メモリ | 123,728 KB |
実行使用メモリ | 9,856 KB |
最終ジャッジ日時 | 2024-07-02 04:41:11 |
合計ジャッジ時間 | 3,141 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; class Edge { public: int to, cost; Edge(int to, int cost){ this->to = to; this->cost = cost; } }; int main() { int n, m; cin >> n >> m; vector<vector<Edge> > edges(n); for(int i=0; i<m; ++i){ int a, b, c; cin >> a >> b >> c; -- a; -- b; edges[a].push_back(Edge(b, c)); edges[b].push_back(Edge(a, c)); } vector<vector<int> > dp(1<<n, vector<int>(n, -1)); for(int i=0; i<n; ++i) dp[1<<i][i] = 0; for(int i=0; i<(1<<n); ++i){ bitset<32> bs(i); for(int j=0; j<n; ++j){ if(dp[i][j] == -1) continue; for(const Edge& e : edges[j]){ if(bs[e.to]) continue; bitset<32> bs2 = bs; bs2[e.to] = true; dp[bs2.to_ulong()][e.to] = max(dp[bs2.to_ulong()][e.to], dp[i][j] + e.cost); } } } int ans = 0; for(int i=1; i<(1<<n); ++i){ for(int j=0; j<n; ++j){ ans = max(ans, dp[i][j]); } } cout << ans << endl; return 0; }