結果
| 問題 | No.845 最長の切符 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-08-01 23:12:47 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 360 ms / 3,000 ms | 
| コード長 | 822 bytes | 
| コンパイル時間 | 2,340 ms | 
| コンパイル使用メモリ | 203,760 KB | 
| 最終ジャッジ日時 | 2025-01-07 10:31:05 | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a - 1].emplace_back(c, b - 1);
    G[b - 1].emplace_back(c, a - 1);
  }
  vector<vector<int>> dp(1 << N, vector<int>(N));
  for (int s = 0; s < 1 << N; ++s) {
    for (int x = 0; x < N; ++x) {
      if (~s >> x & 1) continue;
      for (auto e : G[x]) {
        int w, y;
        tie(w, y) = e;
        if (s >> y & 1) continue;
        dp[s | 1 << y][y] = max(dp[s | 1 << y][y], dp[s][x] + w);
      }
    }
  }
  int ans = 0;
  for (int s = 0; s < 1 << N; ++s) {
    for (int i = 0; i < N; ++i) {
      ans = max(ans, dp[s][i]);
    }
  }
  cout << ans << endl;
  return 0;
}
            
            
            
        