結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
53,112 KB
testcase_01 AC 115 ms
53,860 KB
testcase_02 AC 134 ms
54,008 KB
testcase_03 AC 156 ms
54,332 KB
testcase_04 AC 160 ms
54,780 KB
testcase_05 AC 192 ms
56,972 KB
testcase_06 AC 190 ms
56,568 KB
testcase_07 AC 177 ms
56,712 KB
testcase_08 AC 208 ms
57,468 KB
testcase_09 AC 202 ms
57,560 KB
testcase_10 AC 173 ms
54,532 KB
testcase_11 AC 178 ms
56,588 KB
testcase_12 AC 115 ms
54,004 KB
testcase_13 AC 100 ms
52,724 KB
testcase_14 AC 120 ms
54,108 KB
testcase_15 AC 117 ms
53,756 KB
testcase_16 AC 118 ms
54,140 KB
testcase_17 AC 130 ms
54,520 KB
testcase_18 AC 143 ms
54,228 KB
testcase_19 AC 129 ms
54,476 KB
testcase_20 AC 134 ms
54,224 KB
testcase_21 AC 126 ms
54,004 KB
testcase_22 AC 140 ms
54,460 KB
testcase_23 AC 184 ms
56,648 KB
testcase_24 AC 186 ms
56,876 KB
testcase_25 AC 128 ms
54,200 KB
testcase_26 AC 193 ms
56,840 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