結果

問題 No.845 最長の切符
ユーザー tnakao0123tnakao0123
提出日時 2019-06-29 23:15:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 3,000 ms
コード長 1,306 bytes
コンパイル時間 614 ms
コンパイル使用メモリ 81,944 KB
実行使用メモリ 7,868 KB
最終ジャッジ日時 2023-09-14 23:23:55
合計ジャッジ時間 2,084 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,676 KB
testcase_01 AC 3 ms
7,608 KB
testcase_02 AC 4 ms
7,572 KB
testcase_03 AC 4 ms
7,648 KB
testcase_04 AC 4 ms
7,536 KB
testcase_05 AC 4 ms
7,568 KB
testcase_06 AC 4 ms
7,740 KB
testcase_07 AC 4 ms
7,756 KB
testcase_08 AC 4 ms
7,744 KB
testcase_09 AC 3 ms
7,652 KB
testcase_10 AC 4 ms
7,680 KB
testcase_11 AC 4 ms
7,744 KB
testcase_12 AC 4 ms
7,624 KB
testcase_13 AC 3 ms
7,868 KB
testcase_14 AC 3 ms
7,612 KB
testcase_15 AC 13 ms
7,736 KB
testcase_16 AC 42 ms
7,532 KB
testcase_17 AC 12 ms
7,668 KB
testcase_18 AC 22 ms
7,536 KB
testcase_19 AC 8 ms
7,756 KB
testcase_20 AC 46 ms
7,568 KB
testcase_21 AC 50 ms
7,668 KB
testcase_22 AC 12 ms
7,608 KB
testcase_23 AC 6 ms
7,668 KB
testcase_24 AC 42 ms
7,612 KB
testcase_25 AC 3 ms
7,572 KB
testcase_26 AC 4 ms
7,696 KB
testcase_27 AC 4 ms
7,572 KB
testcase_28 AC 5 ms
7,540 KB
testcase_29 AC 4 ms
7,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 845.cc:  No.845 最長の切符 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 16;
const int NBITS = 1 << MAX_N;

/* typedef */

/* global variables */

int ds[MAX_N][MAX_N];
int dp[NBITS][MAX_N];

/* subroutines */

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

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    if (ds[a][b] < c) ds[a][b] = ds[b][a] = c;
  }

  int nbits = 1 << n, maxd = 0;

  memset(dp, -1, sizeof(dp));
  for (int i = 0, bi = 1; i < n; i++, bi <<= 1) dp[bi][i] = 0;

  for (int bits = 1; bits < nbits; bits++)
    for (int i = 0; i < n; i++)
      if (dp[bits][i] >= 0)
	for (int j = 0, bj = 1; j < n; j++, bj <<= 1)
	  if (! (bits & bj) && ds[i][j] > 0) {
	    int d = dp[bits][i] + ds[i][j];
	    setmax(dp[bits | bj][j], d);
	    setmax(maxd, d);
	  }

  printf("%d\n", maxd);
  return 0;
}
0