結果

問題 No.17 2つの地点に泊まりたい
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 14:50:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 208 ms / 5,000 ms
コード長 2,403 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 78,368 KB
実行使用メモリ 45,520 KB
最終ジャッジ日時 2024-10-15 01:21:06
合計ジャッジ時間 6,960 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
40,864 KB
testcase_01 AC 105 ms
40,076 KB
testcase_02 AC 119 ms
40,012 KB
testcase_03 AC 142 ms
41,456 KB
testcase_04 AC 169 ms
42,124 KB
testcase_05 AC 196 ms
44,176 KB
testcase_06 AC 188 ms
43,272 KB
testcase_07 AC 182 ms
42,988 KB
testcase_08 AC 207 ms
45,520 KB
testcase_09 AC 208 ms
45,408 KB
testcase_10 AC 180 ms
42,684 KB
testcase_11 AC 192 ms
42,992 KB
testcase_12 AC 117 ms
41,076 KB
testcase_13 AC 107 ms
39,820 KB
testcase_14 AC 113 ms
40,036 KB
testcase_15 AC 117 ms
40,940 KB
testcase_16 AC 116 ms
40,260 KB
testcase_17 AC 136 ms
41,164 KB
testcase_18 AC 155 ms
41,664 KB
testcase_19 AC 129 ms
41,284 KB
testcase_20 AC 127 ms
40,920 KB
testcase_21 AC 126 ms
40,216 KB
testcase_22 AC 143 ms
41,640 KB
testcase_23 AC 195 ms
43,648 KB
testcase_24 AC 194 ms
43,364 KB
testcase_25 AC 126 ms
41,172 KB
testcase_26 AC 184 ms
44,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static final int INF = Integer.MAX_VALUE / 2 - 1;
	
	public static class Walk implements Comparable<Walk> {
		int cost, current, stay1, stay2;

		public Walk(int cost, int current, int stay1, int stay2) {
			super();
			this.cost = cost;
			this.current = current;
			this.stay1 = stay1;
			this.stay2 = stay2;
		}

		@Override
		public int compareTo(Walk o) {
			return Integer.compare(this.cost, o.cost);
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		int[] stay_costs = new int[N];
		for(int i = 0; i < N; i++){
			stay_costs[i] = sc.nextInt();
		}
		
		final int M = sc.nextInt();
		int[][] adj = new int[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				adj[i][j] = INF;
			}
		}
		for(int i = 0; i < M; i++){
			final int a = sc.nextInt();
			final int b = sc.nextInt();
			final int c = sc.nextInt();

			adj[a][b] = adj[b][a] = c;
		}
		
		boolean[][][] visited = new boolean[N][N][N];
		PriorityQueue<Walk> queue = new PriorityQueue<Walk>();
		queue.add(new Walk(0, 0, 0, 0));

		while(!queue.isEmpty()){
			final Walk walk = queue.poll();
			
			if(visited[walk.current][walk.stay1][walk.stay2]){
				continue;
			}else{
				visited[walk.current][walk.stay1][walk.stay2] = true;
			}
			
			//System.out.println(walk.current + " " + walk.cost + " " + walk.stay1 + " " + walk.stay2);
			if(walk.current == N - 1 && walk.stay1 != 0 && walk.stay2 != 0 && walk.stay1 != walk.stay2){
				System.out.println(walk.cost);
				break;
			}
					
			if(walk.current != 0 && walk.current != N - 1){
				if(walk.stay1 == 0){
					queue.add(new Walk(walk.cost + stay_costs[walk.current], walk.current, walk.current, walk.stay2));
				}else if(walk.stay1 != walk.current && walk.stay2 == 0){
					queue.add(new Walk(walk.cost + stay_costs[walk.current], walk.current, walk.stay1, walk.current));
				}
			}
			
			for(int next = 0; next < N; next++){
				if(adj[walk.current][next] >= INF){
					continue;
				}else if(visited[next][walk.stay1][walk.stay2]){
					continue;
				}
				
				queue.add(new Walk(walk.cost + adj[walk.current][next], next, walk.stay1, walk.stay2));
			}
		}
		
	}
	
}
0