結果

問題 No.845 最長の切符
ユーザー simansiman
提出日時 2022-06-24 11:16:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 3,000 ms
コード長 1,205 bytes
コンパイル時間 1,002 ms
コンパイル使用メモリ 110,320 KB
実行使用メモリ 7,988 KB
最終ジャッジ日時 2024-04-25 15:59:09
合計ジャッジ時間 3,106 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 16 ms
6,944 KB
testcase_16 AC 333 ms
7,864 KB
testcase_17 AC 80 ms
6,940 KB
testcase_18 AC 54 ms
6,940 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 79 ms
7,988 KB
testcase_21 AC 49 ms
7,900 KB
testcase_22 AC 69 ms
6,944 KB
testcase_23 AC 16 ms
6,940 KB
testcase_24 AC 272 ms
7,848 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 14 ms
7,896 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 15 ms
7,940 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Edge {
  int to;
  int cost;

  Edge(int to = -1, int cost = -1) {
    this->to = to;
    this->cost = cost;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<Edge> E[N + 1];

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    --a;
    --b;
    E[a].push_back(Edge(b, c));
    E[b].push_back(Edge(a, c));
  }

  int L = pow(2, N);
  int dp[L][N];
  memset(dp, 0, sizeof(dp));
  int ans = 0;

  for (int c = 1; c < N; ++c) {
    for (int mask = 0; mask < L; ++mask) {
      if (__builtin_popcount(mask) != c) continue;

      for (int i = 0; i < N; ++i) {
        if ((mask >> i & 1) == 0) continue;

        for (Edge &e : E[i]) {
          if (mask >> e.to & 1) continue;
          int nmask = mask | (1 << e.to);
          int ncost = dp[mask][i] + e.cost;;

          dp[nmask][e.to] = max(dp[nmask][e.to], ncost);
          ans = max(ans, ncost);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0