結果

問題 No.572 妖精の演奏
ユーザー tentententen
提出日時 2023-12-19 09:56:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 2,749 ms
コンパイル使用メモリ 89,248 KB
実行使用メモリ 55,792 KB
最終ジャッジ日時 2023-12-19 09:56:29
合計ジャッジ時間 5,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
54,100 KB
testcase_01 AC 63 ms
54,108 KB
testcase_02 AC 63 ms
54,124 KB
testcase_03 AC 66 ms
54,004 KB
testcase_04 AC 59 ms
53,984 KB
testcase_05 AC 70 ms
54,760 KB
testcase_06 AC 80 ms
54,868 KB
testcase_07 AC 88 ms
55,276 KB
testcase_08 AC 72 ms
54,756 KB
testcase_09 AC 91 ms
55,252 KB
testcase_10 AC 100 ms
55,760 KB
testcase_11 AC 106 ms
55,676 KB
testcase_12 AC 108 ms
55,780 KB
testcase_13 AC 108 ms
55,764 KB
testcase_14 AC 106 ms
55,776 KB
testcase_15 AC 105 ms
55,772 KB
testcase_16 AC 114 ms
55,764 KB
testcase_17 AC 109 ms
55,792 KB
testcase_18 AC 81 ms
54,880 KB
testcase_19 AC 56 ms
53,976 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;
        }
        long result = 0;
        for (long x : ans) {
            result = Math.max(result, x);
        }
        System.out.println(result);
    }
}
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