結果

問題 No.845 最長の切符
ユーザー ikd
提出日時 2019-06-28 22:45:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 906 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 75,500 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-07-02 05:02:14
合計ジャッジ時間 1,996 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void chmax(int &a, int b) {
  if (a < b) a = b;
}

int main() {

  int n, m;
  cin >> n >> m;
  const int inf = 1000000000;
  vector<vector<int>> d(n, vector<int>(n, -1));
  rep(i, n) d[i][i] = 0;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    chmax(d[a][b], c);
    chmax(d[b][a], c);
  }

  vector<vector<int>> dp(1 << n, vector<int>(n, 0));
  rep(bits, 1 << n) {
    rep(v, n) {
      if (bits >> v & 1) {
        rep(w, n) {
          if (!(bits >> w & 1) and d[v][w] >= 0) {
            chmax(dp[bits ^ (1 << w)][w], dp[bits][v] + d[v][w]);
          }
        }
      }
    }
  }
  int ans = 0;
  rep(bits, 1 << n) {
    rep(v, n) {
      if (bits >> v & 1) { chmax(ans, dp[bits][v]); }
    }
  }
  cout << ans << endl;

  return 0;
}
0