結果
| 問題 | No.572 妖精の演奏 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-02-17 00:46:15 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 60 ms / 2,000 ms | 
| コード長 | 1,983 bytes | 
| コンパイル時間 | 998 ms | 
| コンパイル使用メモリ | 95,360 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-09-13 12:49:54 | 
| 合計ジャッジ時間 | 2,087 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 20 | 
ソースコード
#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;
}
            
            
            
        