結果

問題 No.845 最長の切符
ユーザー 👑 emthrmemthrm
提出日時 2019-06-28 22:42:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 3,000 ms
コード長 1,783 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 130,708 KB
実行使用メモリ 13,912 KB
最終ジャッジ日時 2023-09-14 22:25:48
合計ジャッジ時間 2,786 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 13 ms
5,416 KB
testcase_16 AC 51 ms
13,844 KB
testcase_17 AC 12 ms
5,352 KB
testcase_18 AC 25 ms
8,168 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 59 ms
13,844 KB
testcase_21 AC 67 ms
13,788 KB
testcase_22 AC 12 ms
5,344 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 51 ms
13,736 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 19 ms
13,848 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 19 ms
13,912 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  vector<vector<int> > cost(n, vector<int>(n, INF));
  while (m--) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    cost[a][b] = min(cost[a][b], -c);
    cost[b][a] = min(cost[b][a], -c);
  }
  vector<vector<long long> > dp(1 << n, vector<long long>(n, LINF));
  REP(i, n) dp[1 << i][i] = 0;
  FOR(i, 1, (1 << n) - 1) REP(j, n) if (i >> j & 1) {
    if (dp[i][j] == LINF) continue;
    REP(k, n) if (!(i >> k & 1)) {
      if (cost[j][k] != INF) dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + cost[j][k]);
      // bitset<5> bit1(i | (1 << k)), bit2(i);
      // cerr << bit1 << ' ' << k << '|' << bit2 << ' ' << j << '|' << dp[i][j] + cost[j][k] << '\n';
    }
  }
  long long ans = 0;
  REP(i, 1 << n) REP(j, n) ans = min(ans, dp[i][j]);
  cout << -ans << '\n';
  return 0;
}
0