結果

問題 No.845 最長の切符
ユーザー ninjaninja
提出日時 2019-06-28 23:53:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 3,000 ms
コード長 1,061 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 145,576 KB
実行使用メモリ 11,728 KB
最終ジャッジ日時 2023-09-14 22:47:06
合計ジャッジ時間 2,727 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 11 ms
7,516 KB
testcase_16 AC 39 ms
11,544 KB
testcase_17 AC 10 ms
7,488 KB
testcase_18 AC 19 ms
9,548 KB
testcase_19 AC 5 ms
4,412 KB
testcase_20 AC 38 ms
11,684 KB
testcase_21 AC 38 ms
11,728 KB
testcase_22 AC 11 ms
7,564 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 39 ms
11,540 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 39 ms
11,520 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 38 ms
11,568 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

ll cost[20][20] = {0};
ll dp[1LL << 16][16] = {0};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cost[i][j] = -100000000;
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    cost[a][b] = max(cost[a][b], c);
    cost[b][a] = max(cost[b][a], c);
  }
  for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) dp[i][j] = -1000000;
  for (int i = 0; i < n; i++) dp[1 << i][i] = 0;
  ll ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if ((i >> k) & 1) continue;
        ll next = i | (1 << k);
        dp[next][k] = max(dp[next][k], dp[i][j] + cost[j][k]);
        ans = max(ans, dp[next][k]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0