結果

問題 No.17 2つの地点に泊まりたい
ユーザー jp_stejp_ste
提出日時 2016-02-29 15:12:56
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,172 bytes
コンパイル時間 2,799 ms
コンパイル使用メモリ 80,020 KB
実行使用メモリ 62,140 KB
最終ジャッジ日時 2023-10-24 19:10:30
合計ジャッジ時間 22,954 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,400 KB
testcase_01 AC 135 ms
57,416 KB
testcase_02 AC 175 ms
57,816 KB
testcase_03 AC 367 ms
60,628 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 137 ms
57,552 KB
testcase_13 AC 133 ms
57,548 KB
testcase_14 AC 136 ms
57,428 KB
testcase_15 AC 123 ms
56,284 KB
testcase_16 AC 133 ms
57,416 KB
testcase_17 AC 191 ms
58,156 KB
testcase_18 AC 257 ms
60,544 KB
testcase_19 AC 216 ms
60,576 KB
testcase_20 AC 175 ms
57,708 KB
testcase_21 AC 140 ms
57,596 KB
testcase_22 AC 349 ms
58,448 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    
    static int N, M;
    static int stayCost[];
    static int moveCost[][];
    
	public static void main(String[] args) {
		try (Scanner scan = new Scanner(System.in)) {
		    N = scan.nextInt();
		    stayCost = new int[N];
		    for(int i=0; i<N; i++) {
		        stayCost[i] = scan.nextInt();
		    }
		    M = scan.nextInt();
		    moveCost = new int[N][N];
		    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;
		    }
		    
		    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 v1 = calcMinCost(i, 0);
		            if(v1 == Integer.MAX_VALUE) continue;
		            
		            int v2 = calcMinCost(j, i);
		            if(v2 == Integer.MAX_VALUE) continue;
		            
		            int v3 = calcMinCost(N-1, j);
		            if(v3 == Integer.MAX_VALUE) continue;
		            
		            int tmp = v1+v2+v3;
		            ans = Math.min(ans, tmp);
		        }
		    }
		    System.out.println(ans);
        }
	}
	
	static int calcMinCost(int start, int goal) {
	    int minCost[] = new int[N];
        for(int i=0; i<N; i++) {
            minCost[i] = Integer.MAX_VALUE;
        }
        minCost[start] = stayCost[start];
        boolean flag[] = new boolean[N];
        
        Queue<Integer> q = new LinkedList<>();
        q.add(start);
        
        while(true) {
            Integer now = q.poll();
            if(now == null) break;
            
            flag[now] = true;
            
            for(int i=0; i<N; i++) {
                if(i==now) continue;
                if(moveCost[now][i] > 0 && flag[i] == false) {
                    int tmp = minCost[now] + moveCost[now][i];
                    minCost[i] = Math.min(minCost[i], tmp);
                    q.add(i);
                }
            }
        }
        return minCost[goal];
	}
}
0