結果

問題 No.17 2つの地点に泊まりたい
ユーザー jp_stejp_ste
提出日時 2016-03-01 07:25:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 215 ms / 5,000 ms
コード長 1,572 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 77,328 KB
実行使用メモリ 46,228 KB
最終ジャッジ日時 2024-04-23 02:11:15
合計ジャッジ時間 7,073 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
41,176 KB
testcase_01 AC 131 ms
41,764 KB
testcase_02 AC 115 ms
41,520 KB
testcase_03 AC 148 ms
42,000 KB
testcase_04 AC 166 ms
42,280 KB
testcase_05 AC 190 ms
44,348 KB
testcase_06 AC 191 ms
43,408 KB
testcase_07 AC 183 ms
43,352 KB
testcase_08 AC 215 ms
46,228 KB
testcase_09 AC 206 ms
45,812 KB
testcase_10 AC 180 ms
43,136 KB
testcase_11 AC 193 ms
43,568 KB
testcase_12 AC 119 ms
41,628 KB
testcase_13 AC 106 ms
41,472 KB
testcase_14 AC 110 ms
41,756 KB
testcase_15 AC 120 ms
41,212 KB
testcase_16 AC 115 ms
41,392 KB
testcase_17 AC 129 ms
41,404 KB
testcase_18 AC 170 ms
41,992 KB
testcase_19 AC 144 ms
41,772 KB
testcase_20 AC 138 ms
41,384 KB
testcase_21 AC 110 ms
41,092 KB
testcase_22 AC 159 ms
42,120 KB
testcase_23 AC 200 ms
43,952 KB
testcase_24 AC 186 ms
43,100 KB
testcase_25 AC 129 ms
41,584 KB
testcase_26 AC 189 ms
44,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int N = scan.nextInt();
            int stayCost[] = new int[N];
            for(int i=0; i<N; i++) {
                stayCost[i] = scan.nextInt();
            }
            int M = scan.nextInt();
            int moveCost[][] = new int[N][N];
            for(int i=0; i<N; i++) {
                for(int j=0; j<N; j++) {
                    moveCost[i][j] = 1000000;
                }
            }
            
            for(int i=0; i<M; i++) {
                int from = scan.nextInt();
                int to = scan.nextInt();
                int cost = scan.nextInt();
                moveCost[from][to] = cost;
                moveCost[to][from] = cost;
            }
            
            for(int k = 0; k < N; ++k) {
                for(int i = 0; i < N; ++i) {
                    for(int j = 0; j < N; ++j) {
                        moveCost[i][j] = Math.min(moveCost[i][j], moveCost[i][k] + moveCost[k][j]);
                    }
                }
            }
            
            int ans = Integer.MAX_VALUE;
            for(int i=1; i<N-1; i++) {
                for(int j=1; j<N-1; j++) {
                    if(i==j) continue;
                    int tmp = moveCost[i][0] + moveCost[j][i] + moveCost[N-1][j] + stayCost[i] + stayCost[j];
                    ans = Math.min(ans, tmp);
                }
            }
            
            System.out.println(ans);
        }
    }
}
0