結果

問題 No.17 2つの地点に泊まりたい
ユーザー jp_ste
提出日時 2016-03-01 07:25:57
言語 Java
(openjdk 23)
結果
AC  
実行時間 209 ms / 5,000 ms
コード長 1,572 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 76,792 KB
実行使用メモリ 45,460 KB
最終ジャッジ日時 2024-10-15 01:30:59
合計ジャッジ時間 6,972 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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