結果

問題 No.572 妖精の演奏
ユーザー 0w10w1
提出日時 2017-11-21 15:28:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,224 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 201,120 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 11:47:37
合計ジャッジ時間 3,249 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 33 ms
4,376 KB
testcase_11 AC 33 ms
4,376 KB
testcase_12 AC 33 ms
4,380 KB
testcase_13 AC 33 ms
4,376 KB
testcase_14 AC 33 ms
4,380 KB
testcase_15 AC 33 ms
4,376 KB
testcase_16 AC 33 ms
4,380 KB
testcase_17 AC 33 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAXN = int(5e9);
const int MAXM = 30;

long long N;
int M;
int A[MAXM][MAXM];
long long dp[40][MAXM][MAXM];

long long dp2[MAXM];

signed main() {
  ios::sync_with_stdio(false);
  cin >> N;
  cin >> M;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < M; ++j) {
      cin >> A[i][j];
    }
  }
  memset(dp, 0xc0, sizeof(dp));
  for (int i = 0; i < M; ++i) {
    dp[0][i][i] = 0;
  }
  for (int i = 0; i + 1 < 40; ++i) {
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        for (int l = 0; l < M; ++l) {
          for (int m = 0; m < M; ++m) {
            dp[i + 1][j][m] = max(dp[i + 1][j][m], dp[i][j][k] + A[k][l] + dp[i][l][m]);
          }
        }
      }
    }
  }
  --N;
  for (int i = 0; i < 40; ++i) {
    if (~N >> i & 1LL) continue;
    static long long tmp[MAXM];
    memset(tmp, 0xc0, sizeof(tmp));
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k < M; ++k) {
        for (int l = 0; l < M; ++l) {
          tmp[l] = max(tmp[l], dp2[j] + A[j][k] + dp[i][k][l]);
        }
      }
    }
    for (int j = 0; j < M; ++j) {
      dp2[j] = tmp[j];
    }
  }
  cout << *max_element(dp2, dp2 + M) << endl;
  return 0;
}
0