結果

問題 No.17 2つの地点に泊まりたい
ユーザー chiho_miyakochiho_miyako
提出日時 2015-04-30 18:24:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 1,235 bytes
コンパイル時間 1,847 ms
コンパイル使用メモリ 78,064 KB
実行使用メモリ 58,888 KB
最終ジャッジ日時 2024-04-23 02:02:41
合計ジャッジ時間 6,697 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
53,428 KB
testcase_01 AC 103 ms
52,924 KB
testcase_02 AC 108 ms
53,008 KB
testcase_03 AC 133 ms
53,588 KB
testcase_04 AC 164 ms
54,480 KB
testcase_05 AC 199 ms
57,336 KB
testcase_06 AC 182 ms
56,536 KB
testcase_07 AC 175 ms
58,888 KB
testcase_08 AC 199 ms
57,592 KB
testcase_09 AC 192 ms
57,496 KB
testcase_10 AC 178 ms
55,072 KB
testcase_11 AC 186 ms
57,360 KB
testcase_12 AC 119 ms
54,292 KB
testcase_13 AC 116 ms
53,884 KB
testcase_14 AC 108 ms
52,988 KB
testcase_15 AC 111 ms
53,184 KB
testcase_16 AC 102 ms
52,820 KB
testcase_17 AC 127 ms
54,380 KB
testcase_18 AC 136 ms
54,508 KB
testcase_19 AC 135 ms
54,108 KB
testcase_20 AC 125 ms
53,972 KB
testcase_21 AC 121 ms
53,880 KB
testcase_22 AC 134 ms
53,840 KB
testcase_23 AC 191 ms
56,892 KB
testcase_24 AC 184 ms
57,580 KB
testcase_25 AC 122 ms
54,240 KB
testcase_26 AC 194 ms
56,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Ideone{
	public static void main (String[] args) throws java.lang.Exception{
		Scanner koko = new Scanner(System.in);
		int n = koko.nextInt();
		int[] s = new int[n];
		for(int i=0; i<n; i++){
			s[i]=koko.nextInt();
		}
		int m = koko.nextInt();
		int[] a = new int[m];
		int[] b = new int[m];
		int[] c = new int[m];
		int[][] d = new int[n][n];
		boolean[][] ju = new boolean[n][n];
		for(int i=0; i<m; i++){
			a[i]=koko.nextInt();
			b[i]=koko.nextInt();
			c[i]=koko.nextInt();
			d[a[i]][b[i]]=c[i];
			d[b[i]][a[i]]=c[i];
			ju[a[i]][b[i]]=true;
			ju[b[i]][a[i]]=true;
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				for(int k=0; k<n; k++){
					if(!ju[j][k]&&ju[j][i]&&ju[i][k]){
						d[j][k]=d[j][i]+d[i][k];
						ju[j][k]=true;
					}else if(ju[j][k]&&ju[j][i]&&ju[i][k]){
						d[j][k]=Math.min(d[j][k], d[j][i]+d[i][k]);
					}
				}
			}
		}
		int min = 1127000;
		for(int i=1; i<n-1; i++){
			for(int j=i+1; j<n-1; j++){
				if(ju[0][i]&&ju[i][j]&&ju[j][n-1]){
					min=Math.min(min, s[i]+s[j]+d[0][i]+d[i][j]+d[j][n-1]);
				}
				if(ju[0][j]&&ju[j][i]&&ju[i][n-1]){
					min=Math.min(min, s[i]+s[j]+d[0][j]+d[j][i]+d[i][n-1]);
				}
			}
		}
		System.out.println(min);
	}
}
0