結果

問題 No.572 妖精の演奏
ユーザー uafr_csuafr_cs
提出日時 2017-11-06 21:30:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 73,924 KB
実行使用メモリ 59,096 KB
最終ジャッジ日時 2023-08-15 19:40:05
合計ジャッジ時間 6,550 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
55,788 KB
testcase_01 AC 141 ms
56,028 KB
testcase_02 AC 142 ms
55,652 KB
testcase_03 AC 141 ms
55,808 KB
testcase_04 AC 133 ms
55,632 KB
testcase_05 AC 156 ms
56,184 KB
testcase_06 AC 155 ms
55,924 KB
testcase_07 AC 151 ms
55,664 KB
testcase_08 AC 153 ms
56,200 KB
testcase_09 AC 154 ms
55,640 KB
testcase_10 AC 205 ms
56,788 KB
testcase_11 AC 208 ms
56,400 KB
testcase_12 AC 214 ms
56,700 KB
testcase_13 AC 222 ms
59,096 KB
testcase_14 AC 192 ms
56,648 KB
testcase_15 AC 195 ms
56,268 KB
testcase_16 AC 195 ms
56,572 KB
testcase_17 AC 203 ms
56,624 KB
testcase_18 AC 145 ms
55,828 KB
testcase_19 AC 130 ms
55,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static long[][] mat_mul(int m, long[][] mat, long p){
		if(p == 0){
			return new long[m][m];
		}else if(p == 1){
			return mat;
		}else if(p % 2 == 1){
			return mat_max(m, mat, mat_mul(m, mat, p - 1));
		}else{
			long[][] ret = mat_mul(m, mat, p / 2);
			return mat_max(m, ret, ret);
		}
	}
	
	public static long[][] mat_max(int m, long[][] mat1, long[][] mat2){
		long[][] ret = new long[m][m];
		
		for(int from = 0; from < m; from++){
			for(int to = 0; to < m; to++){
				for(int i = 0; i < m; i++){
					ret[from][to] = Math.max(ret[from][to], mat1[from][i] + mat2[i][to]);
				}
			}
		}
		
		return ret;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		final int m = sc.nextInt();
		
		long[][] mat = new long[m][m];
		for(int i = 0; i < m; i++){
			for(int j = 0; j < m; j++){
				mat[i][j] = sc.nextLong();
			}
		}
		
		long[][] ret = mat_mul(m, mat, n - 1);
		
		long max = 0;
		for(int i = 0; i < m; i++){
			for(int j = 0; j < m; j++){
				//System.out.print(ret[i][j] + " ");
				max = Math.max(max, ret[i][j]);
			}
			//System.out.println();
		}
		
		System.out.println(max);
	}
}
0