結果

問題 No.1 道のショートカット
ユーザー uafr_csuafr_cs
提出日時 2015-04-29 23:14:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 218 ms / 5,000 ms
コード長 2,491 bytes
コンパイル時間 3,629 ms
コンパイル使用メモリ 76,440 KB
実行使用メモリ 59,748 KB
最終ジャッジ日時 2023-09-27 21:43:32
合計ジャッジ時間 10,910 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
55,796 KB
testcase_01 AC 116 ms
55,976 KB
testcase_02 AC 118 ms
56,304 KB
testcase_03 AC 122 ms
56,136 KB
testcase_04 AC 117 ms
55,888 KB
testcase_05 AC 114 ms
55,792 KB
testcase_06 AC 119 ms
56,508 KB
testcase_07 AC 125 ms
55,792 KB
testcase_08 AC 205 ms
59,384 KB
testcase_09 AC 190 ms
59,040 KB
testcase_10 AC 204 ms
57,392 KB
testcase_11 AC 207 ms
59,148 KB
testcase_12 AC 208 ms
59,364 KB
testcase_13 AC 218 ms
59,624 KB
testcase_14 AC 133 ms
56,308 KB
testcase_15 AC 117 ms
56,024 KB
testcase_16 AC 178 ms
56,332 KB
testcase_17 AC 118 ms
55,956 KB
testcase_18 AC 149 ms
55,988 KB
testcase_19 AC 155 ms
56,032 KB
testcase_20 AC 172 ms
56,056 KB
testcase_21 AC 170 ms
56,424 KB
testcase_22 AC 137 ms
55,732 KB
testcase_23 AC 209 ms
59,540 KB
testcase_24 AC 202 ms
59,748 KB
testcase_25 AC 168 ms
56,156 KB
testcase_26 AC 168 ms
56,216 KB
testcase_27 AC 200 ms
59,636 KB
testcase_28 AC 119 ms
55,868 KB
testcase_29 AC 206 ms
59,168 KB
testcase_30 AC 184 ms
58,344 KB
testcase_31 AC 193 ms
58,988 KB
testcase_32 AC 193 ms
59,688 KB
testcase_33 AC 186 ms
59,296 KB
testcase_34 AC 199 ms
59,028 KB
testcase_35 AC 192 ms
59,604 KB
testcase_36 AC 196 ms
59,088 KB
testcase_37 AC 190 ms
59,616 KB
testcase_38 AC 140 ms
55,924 KB
testcase_39 AC 162 ms
56,372 KB
testcase_40 AC 179 ms
58,284 KB
testcase_41 AC 168 ms
56,104 KB
testcase_42 AC 113 ms
55,668 KB
testcase_43 AC 116 ms
55,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main {
	
	public static class State implements Comparable<State> {
		int node, cost, time;

		public State(int node, int cost, int time) {
			super();
			this.node = node;
			this.cost = cost;
			this.time = time;
		}

		public int compareTo(State o) {
			return Integer.compare(this.time, o.time);
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int C = sc.nextInt();
		final int V = sc.nextInt();
		
		int[] S = new int[V];
		for(int i = 0; i < V; i++){
			S[i] = sc.nextInt() - 1;
		}
		
		int[] T = new int[V];
		for(int i = 0; i < V; i++){
			T[i] = sc.nextInt() - 1;
		}
		
		int[] Y = new int[V];
		for(int i = 0; i < V; i++){
			Y[i] = sc.nextInt();
		}
		
		int[] M = new int[V];
		for(int i = 0; i < V; i++){
			M[i] = sc.nextInt();
		}
		
		ArrayList<HashMap<Integer, HashMap<Integer, Integer>>> adj = new ArrayList<HashMap<Integer, HashMap<Integer, Integer>>>();
		for(int i = 0; i < N; i++){
			adj.add(new HashMap<Integer, HashMap<Integer, Integer>>());
		}
		
		for(int i = 0; i < V; i++){
			if(!adj.get(S[i]).containsKey(T[i])){
				adj.get(S[i]).put(T[i], new HashMap<Integer, Integer>());
			}
			
			adj.get(S[i]).get(T[i]).put(Y[i], Math.min(M[i], adj.get(S[i]).get(T[i]).containsKey(Y[i]) ? adj.get(S[i]).get(T[i]).get(Y[i]) : Integer.MAX_VALUE));
		}
		
		boolean[][] visited = new boolean[N][C + 1];
		PriorityQueue<State> queue = new PriorityQueue<State>();
		queue.add(new State(0, 0, 0));
		
		while(!queue.isEmpty()){
			final State state = queue.poll();
			
			if(visited[state.node][state.cost]){
				continue;
			}else{
				visited[state.node][state.cost] = true;
			}
			
			if(state.node == N - 1){
				System.out.println(state.time);
				return;
			}
			
			for(Entry<Integer, HashMap<Integer, Integer>> entrys : adj.get(state.node).entrySet()){
				final int next = entrys.getKey();
				
				for(Entry<Integer, Integer> entry : entrys.getValue().entrySet()){
					final int next_cost = state.cost + entry.getKey();
					final int next_time = state.time + entry.getValue();
				
					if(next_cost > C){
						continue;
					}else if(visited[next][next_cost]){
						continue;
					}
				
					queue.add(new State(next, next_cost, next_time));
				}
			}
		}
		
		System.out.println(-1);
	}
	
}
0