結果

問題 No.572 妖精の演奏
ユーザー tentententen
提出日時 2021-03-10 09:03:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,665 bytes
コンパイル時間 2,464 ms
コンパイル使用メモリ 77,712 KB
実行使用メモリ 39,712 KB
最終ジャッジ日時 2024-04-20 05:25:53
合計ジャッジ時間 4,908 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,292 KB
testcase_01 AC 56 ms
37,392 KB
testcase_02 AC 54 ms
36,880 KB
testcase_03 AC 54 ms
37,344 KB
testcase_04 AC 54 ms
37,320 KB
testcase_05 AC 67 ms
38,220 KB
testcase_06 AC 78 ms
38,272 KB
testcase_07 AC 65 ms
37,984 KB
testcase_08 AC 79 ms
38,544 KB
testcase_09 AC 83 ms
38,656 KB
testcase_10 AC 105 ms
39,712 KB
testcase_11 AC 99 ms
39,660 KB
testcase_12 AC 98 ms
39,552 KB
testcase_13 AC 99 ms
39,708 KB
testcase_14 AC 103 ms
39,412 KB
testcase_15 AC 101 ms
39,596 KB
testcase_16 AC 99 ms
39,456 KB
testcase_17 AC 103 ms
39,552 KB
testcase_18 AC 68 ms
37,760 KB
testcase_19 AC 50 ms
37,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        long n = Long.parseLong(br.readLine()) - 1;
        int m = Integer.parseInt(br.readLine());
        long[][][] values = new long[34][m][m];
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", m);
            for (int j = 0; j < m; j++) {
                values[0][i][j] = Integer.parseInt(line[j]);
            }
        }
        for (int i = 1; i < 34; i++) {
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < m; k++) {
                    for (int l = 0; l < m; l++) {
                        values[i][j][k] = Math.max(values[i][j][k], values[i - 1][j][l] + values[i - 1][l][k]);
                    }
                }
            }
        }
        long[][] ans = new long[m][m];
        for (int i = 33; i >= 0 && n > 0; i--) {
            if (n < (1L << i)) {
                continue;
            }
            long[][] next = new long[m][m];
            n -= (1L << i);
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < m; k++) {
                    for (int l = 0; l < m; l++) {
                        next[j][k] = Math.max(next[j][k], ans[j][l] + values[i][l][k]);
                    }
                }
            }
            ans = next;
        }
        long max = 0;
        for (long[] arr : ans) {
            for (long x : arr) {
                max = Math.max(x, max);
            }
        }
        System.out.println(max);
    }
}
0