結果

問題 No.572 妖精の演奏
ユーザー 夜
提出日時 2021-02-17 00:46:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 1,233 ms
コンパイル使用メモリ 92,864 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 14:06:42
合計ジャッジ時間 1,974 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long dp[40][40][40] = {};
long long ans[40][40][40] = {};
long long a[40][40];
int main() {
	long long n, m;
	cin >> n >> m;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			dp[1][i][j] = a[i][j];
		}
	}
	for (int i = 2; i <= 35; i++) {
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < m; k++) {
				for (int l = 0; l < m; l++) {
					for (int m1 = 0; m1 < m; m1++) {
						if (dp[i][j][m1] < dp[i - 1][j][k] + a[k][l] + dp[i - 1][l][m1]) {
							dp[i][j][m1] = dp[i - 1][j][k] + a[k][l] + dp[i - 1][l][m1];
						}
					}
				}
			}
		}
	}
	long long o = 1;
	int co = 1;
	bool bo = false;
	for (long long i = 1; i < 35; i++) {
		if (n & (o << i)) {
			if (!bo) {
				for (int j = 0; j < m; j++) {
					for (int k = 0; k < m; k++) {
						ans[co][j][k] = dp[i][j][k];
					}
				}
			}
			else {
				for (int j = 0; j < m; j++) {
					for (int k = 0; k < m; k++) {
						for (int l = 0; l < m; l++) {
							for (int m1 = 0; m1 < m; m1++) {
								if (ans[co][j][m1] < ans[co - 1][j][k] + a[k][l] + dp[i][l][m1]) {
									ans[co][j][m1] = ans[co - 1][j][k] + a[k][l] + dp[i][l][m1];
								}
							}
						}
					}
				}
			}
			bo = true;
			co++;
		}
	}
	long long ans1 = 0;
	if (n % 2 == 1) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (ans1 < ans[co - 1][i][j] + a[j][k]) {
						ans1 = ans[co - 1][i][j] + a[j][k];
					}
				}
			}
		}
	}
	else {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (ans1 < ans[co - 1][i][j]) {
					ans1 = ans[co - 1][i][j];
				}
			}
		}
	}
	cout << ans1 << endl;
}
0