結果

問題 No.17 2つの地点に泊まりたい
ユーザー GoryudyumaGoryudyuma
提出日時 2017-02-12 22:59:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 230 ms / 5,000 ms
コード長 2,023 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 77,700 KB
実行使用メモリ 45,732 KB
最終ジャッジ日時 2024-04-23 02:18:06
合計ジャッジ時間 7,778 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
41,396 KB
testcase_01 AC 135 ms
41,412 KB
testcase_02 AC 143 ms
41,708 KB
testcase_03 AC 171 ms
42,304 KB
testcase_04 AC 189 ms
42,620 KB
testcase_05 AC 218 ms
43,896 KB
testcase_06 AC 205 ms
43,496 KB
testcase_07 AC 192 ms
43,384 KB
testcase_08 AC 228 ms
45,732 KB
testcase_09 AC 230 ms
45,700 KB
testcase_10 AC 202 ms
42,616 KB
testcase_11 AC 212 ms
43,176 KB
testcase_12 AC 139 ms
41,912 KB
testcase_13 AC 134 ms
41,464 KB
testcase_14 AC 122 ms
40,368 KB
testcase_15 AC 124 ms
40,160 KB
testcase_16 AC 134 ms
41,668 KB
testcase_17 AC 149 ms
41,808 KB
testcase_18 AC 152 ms
41,508 KB
testcase_19 AC 150 ms
41,984 KB
testcase_20 AC 143 ms
41,500 KB
testcase_21 AC 143 ms
41,172 KB
testcase_22 AC 164 ms
41,812 KB
testcase_23 AC 204 ms
43,860 KB
testcase_24 AC 211 ms
43,684 KB
testcase_25 AC 144 ms
41,628 KB
testcase_26 AC 217 ms
44,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
        public static void main(String args[]) {
                try (Scanner sc = new Scanner(System.in)) {
                        int N = sc.nextInt();
                        int[] cost = new int[N];
                        for (int i = 0; i < N; i++) {
                                cost[i] = sc.nextInt();
                        }

                        int M = sc.nextInt();
                        int[][] bridge = new int[N][N];
                        for (int i = 0; i < N; i++) {
                                for (int j = 0; j < N; j++) {
                                        bridge[i][j] = 1 << 25;
                                }
                                bridge[i][i] = 0;
                        }
                        for (int i = 0; i < M; i++) {
                                int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
                                bridge[a][b] = bridge[b][a] = c;
                        }
                        for (int k = 0; k < N; k++) {
                                for (int i = 0; i < N; i++) {
                                        for (int j = i + 1; j < N; j++) {
                                                bridge[i][j] = Math.min(bridge[i][j], bridge[i][k] + bridge[k][j]);
                                                bridge[j][i] = bridge[i][j];
                                        }
                                }
                        }
                        int ans = 1 << 26;
                        for (int i = 1; i < N - 1; i++) {
                                for (int j = 1; j < N - 1; j++) {
                                        if (i != j) {
                                                ans = Math.min(ans, bridge[0][i] + cost[i] + bridge[i][j] + cost[j] + bridge[j][N-1]);
                                        }
                                }
                        }
                        System.out.println(ans);
                }
        }
}
0