結果

問題 No.17 2つの地点に泊まりたい
ユーザー tentententen
提出日時 2021-12-02 13:57:36
言語 Java19
(openjdk 21)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 2,141 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 74,704 KB
実行使用メモリ 52,568 KB
最終ジャッジ日時 2023-09-18 10:14:51
合計ジャッジ時間 5,320 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,260 KB
testcase_01 AC 43 ms
49,212 KB
testcase_02 AC 49 ms
49,344 KB
testcase_03 AC 94 ms
52,116 KB
testcase_04 AC 63 ms
50,380 KB
testcase_05 AC 100 ms
52,268 KB
testcase_06 AC 109 ms
52,200 KB
testcase_07 AC 91 ms
52,504 KB
testcase_08 AC 111 ms
52,224 KB
testcase_09 AC 111 ms
52,244 KB
testcase_10 AC 90 ms
52,516 KB
testcase_11 AC 103 ms
52,128 KB
testcase_12 AC 43 ms
49,464 KB
testcase_13 AC 43 ms
49,268 KB
testcase_14 AC 42 ms
49,312 KB
testcase_15 AC 42 ms
49,580 KB
testcase_16 AC 42 ms
49,140 KB
testcase_17 AC 56 ms
50,384 KB
testcase_18 AC 87 ms
52,140 KB
testcase_19 AC 86 ms
52,568 KB
testcase_20 AC 55 ms
50,368 KB
testcase_21 AC 47 ms
49,348 KB
testcase_22 AC 88 ms
52,188 KB
testcase_23 AC 93 ms
52,308 KB
testcase_24 AC 98 ms
52,060 KB
testcase_25 AC 46 ms
49,392 KB
testcase_26 AC 108 ms
52,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] stays = new int[n];
        for (int i = 0; i < n; i++) {
            stays[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        int[][] costs = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs[i], Integer.MAX_VALUE / 2);
            costs[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            costs[a][b] = c;
            costs[b][a] = c;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    for (int l = 0; l < n; l++) {
                        costs[j][l] = Math.min(costs[j][l], costs[j][k] + costs[k][l]);
                    }
                }
            }
        }
        long min = Long.MAX_VALUE;
        for (int i = 1; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                min = Math.min(min, 0L + stays[i] + stays[j] + costs[0][i] + costs[i][j] + costs[n - 1][j]);
                min = Math.min(min, 0L + stays[i] + stays[j] + costs[0][j] + costs[i][j] + costs[n - 1][i]);
            }
        }
        System.out.println(min);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0