結果

問題 No.572 妖精の演奏
ユーザー uafr_csuafr_cs
提出日時 2017-11-06 21:30:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 77,052 KB
実行使用メモリ 44,980 KB
最終ジャッジ日時 2024-05-03 06:25:04
合計ジャッジ時間 7,178 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
43,160 KB
testcase_01 AC 149 ms
43,524 KB
testcase_02 AC 156 ms
43,476 KB
testcase_03 AC 155 ms
43,772 KB
testcase_04 AC 149 ms
43,276 KB
testcase_05 AC 163 ms
43,816 KB
testcase_06 AC 164 ms
43,524 KB
testcase_07 AC 162 ms
43,324 KB
testcase_08 AC 166 ms
43,900 KB
testcase_09 AC 172 ms
43,736 KB
testcase_10 AC 214 ms
44,980 KB
testcase_11 AC 225 ms
44,332 KB
testcase_12 AC 223 ms
44,344 KB
testcase_13 AC 235 ms
44,784 KB
testcase_14 AC 210 ms
44,168 KB
testcase_15 AC 211 ms
44,284 KB
testcase_16 AC 211 ms
44,416 KB
testcase_17 AC 209 ms
44,348 KB
testcase_18 AC 151 ms
43,284 KB
testcase_19 AC 135 ms
43,184 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