結果
問題 | No.1 道のショートカット |
ユーザー |
![]() |
提出日時 | 2015-04-29 23:14:22 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 247 ms / 5,000 ms |
コード長 | 2,491 bytes |
コンパイル時間 | 3,255 ms |
コンパイル使用メモリ | 80,436 KB |
実行使用メモリ | 46,056 KB |
最終ジャッジ日時 | 2024-07-20 16:13:35 |
合計ジャッジ時間 | 12,044 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
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); } }