結果

問題 No.17 2つの地点に泊まりたい
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 14:50:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 219 ms / 5,000 ms
コード長 2,403 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 80,104 KB
実行使用メモリ 60,228 KB
最終ジャッジ日時 2023-08-05 03:34:04
合計ジャッジ時間 7,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,884 KB
testcase_01 AC 124 ms
55,636 KB
testcase_02 AC 137 ms
55,320 KB
testcase_03 AC 155 ms
55,784 KB
testcase_04 AC 195 ms
56,064 KB
testcase_05 AC 210 ms
58,576 KB
testcase_06 AC 203 ms
58,276 KB
testcase_07 AC 199 ms
58,276 KB
testcase_08 AC 217 ms
59,076 KB
testcase_09 AC 219 ms
58,836 KB
testcase_10 AC 191 ms
54,212 KB
testcase_11 AC 196 ms
58,060 KB
testcase_12 AC 127 ms
55,952 KB
testcase_13 AC 133 ms
55,628 KB
testcase_14 AC 133 ms
55,292 KB
testcase_15 AC 125 ms
55,496 KB
testcase_16 AC 126 ms
55,596 KB
testcase_17 AC 137 ms
53,452 KB
testcase_18 AC 156 ms
55,704 KB
testcase_19 AC 142 ms
55,584 KB
testcase_20 AC 137 ms
56,020 KB
testcase_21 AC 129 ms
55,524 KB
testcase_22 AC 164 ms
55,892 KB
testcase_23 AC 207 ms
58,400 KB
testcase_24 AC 200 ms
60,228 KB
testcase_25 AC 140 ms
55,572 KB
testcase_26 AC 210 ms
58,608 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