結果

問題 No.572 妖精の演奏
ユーザー tentententen
提出日時 2023-04-07 11:48:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 2,542 bytes
コンパイル時間 2,733 ms
コンパイル使用メモリ 88,232 KB
実行使用メモリ 39,740 KB
最終ジャッジ日時 2024-04-10 16:12:17
合計ジャッジ時間 5,256 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
37,668 KB
testcase_01 AC 59 ms
37,452 KB
testcase_02 AC 61 ms
37,108 KB
testcase_03 AC 60 ms
37,504 KB
testcase_04 AC 55 ms
37,508 KB
testcase_05 AC 68 ms
38,176 KB
testcase_06 AC 85 ms
38,568 KB
testcase_07 AC 69 ms
37,920 KB
testcase_08 AC 70 ms
37,880 KB
testcase_09 AC 89 ms
38,724 KB
testcase_10 AC 92 ms
39,516 KB
testcase_11 AC 92 ms
39,516 KB
testcase_12 AC 93 ms
39,552 KB
testcase_13 AC 94 ms
39,740 KB
testcase_14 AC 92 ms
39,740 KB
testcase_15 AC 92 ms
39,364 KB
testcase_16 AC 93 ms
39,536 KB
testcase_17 AC 97 ms
39,656 KB
testcase_18 AC 77 ms
37,988 KB
testcase_19 AC 52 ms
37,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long n = sc.nextLong() - 1;
        int m = sc.nextInt();
        long[][][] matrix = new long[40][m][m];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                matrix[0][i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i < 40; i++) {
            for (int a = 0; a < m; a++) {
                for (int b = 0; b < m; b++) {
                    for (int c = 0; c < m; c++) {
                        matrix[i][b][c] = Math.max(matrix[i][b][c], matrix[i - 1][b][a] + matrix[i - 1][a][c]);
                    }
                }
            }
        }
        long[] ans = new long[m];
        for (int i = 39; i >= 0; i--) {
            if (n < (1L << i)) {
                continue;
            }
            n -= (1L << i);
            long[] next = new long[m];
            for (int a = 0; a < m; a++) {
                for (int b = 0; b < m; b++) {
                    next[b] = Math.max(next[b], ans[a] + matrix[i][a][b]);
                }
            }
            ans = next;
        }
        Arrays.sort(ans);
        System.out.println(ans[m - 1]);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0