結果

問題 No.572 妖精の演奏
ユーザー tentententen
提出日時 2021-03-10 09:03:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,665 bytes
コンパイル時間 4,366 ms
コンパイル使用メモリ 73,940 KB
実行使用メモリ 53,108 KB
最終ジャッジ日時 2023-08-02 05:26:26
合計ジャッジ時間 4,623 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
47,900 KB
testcase_01 AC 46 ms
49,460 KB
testcase_02 AC 47 ms
49,308 KB
testcase_03 AC 44 ms
47,344 KB
testcase_04 AC 42 ms
49,376 KB
testcase_05 AC 51 ms
50,892 KB
testcase_06 AC 69 ms
51,940 KB
testcase_07 AC 55 ms
50,860 KB
testcase_08 AC 55 ms
50,660 KB
testcase_09 AC 71 ms
51,944 KB
testcase_10 AC 77 ms
52,788 KB
testcase_11 AC 79 ms
52,776 KB
testcase_12 AC 81 ms
52,632 KB
testcase_13 AC 80 ms
53,108 KB
testcase_14 AC 83 ms
52,740 KB
testcase_15 AC 82 ms
52,248 KB
testcase_16 AC 82 ms
52,776 KB
testcase_17 AC 83 ms
52,744 KB
testcase_18 AC 55 ms
50,660 KB
testcase_19 AC 41 ms
49,748 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