結果

問題 No.845 最長の切符
ユーザー yuppe19 😺
提出日時 2019-07-04 15:20:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 49 ms / 3,000 ms
コード長 928 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 72,344 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2024-09-19 03:56:51
合計ジャッジ時間 1,915 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  int N, M; scanf("%d%d", &N, &M);
  vector<vector<int>> G(N, vector<int>(N, 0));
  for(int i=0; i<M; ++i) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    --a; --b;
    G[a][b] = max(G[a][b], c);
    G[b][a] = max(G[b][a], c);
  }
  // dp[現在地][訪問済み] := 最長距離
  vector<vector<int>> dp(N, vector<int>(1<<N, -1));
  for(int s=0; s<N; ++s) { dp[s][1<<s] = 0; }
  int res = 0;
  for(int mask=0; mask<1<<N; ++mask) {
    for(int u=0; u<N; ++u) {
      if(!(mask >> u & 1)) { continue; }
      if(dp[u][mask] == -1) { continue; }
      for(int v=0; v<N; ++v) {
        if(mask >> v & 1) { continue; }
        if(G[u][v] == 0) { continue; }
        int nmask = mask | (1<<v);
        dp[v][nmask] = max(dp[v][nmask], dp[u][mask] + G[u][v]);
        res = max(res, dp[v][nmask]);
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
0