結果
問題 | No.1 道のショートカット |
ユーザー |
![]() |
提出日時 | 2016-04-27 06:41:23 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 233 ms / 5,000 ms |
コード長 | 1,879 bytes |
コンパイル時間 | 2,299 ms |
コンパイル使用メモリ | 78,316 KB |
実行使用メモリ | 45,980 KB |
最終ジャッジ日時 | 2024-07-20 16:23:28 |
合計ジャッジ時間 | 11,700 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); int N = cin.nextInt(); int C = cin.nextInt(); int V = cin.nextInt(); ArrayList<List<Edge>> graph = new ArrayList<List<Edge>>(); for(int i = 0; i < N ; i++)graph.add(new ArrayList<Edge>()); int S[] = new int[V],T[] = new int[V] , Y[] = new int[V], M[] = new int[V]; int arr[][] = {S,T,Y,M}; for(int[] v:arr) for(int i = 0 ; i < V ; i++) v[i] = cin.nextInt(); for(int i = 0 ; i < V ; i++ ){ graph.get(S[i]-1).add(new Edge(T[i]-1,M[i],Y[i])); } System.out.println(Dijkstra(graph,0,N-1,C)); } public static class Edge implements Comparable{ int weight ; int dst; int money; Edge(int dst , int weight,int money){ this.weight = weight; this.dst = dst; this.money = money; } @Override public int compareTo(Object obj){ try{ Edge e = (Edge)obj ; if(this.weight > e.weight)return 1 ; else if(this.weight < e.weight)return -1; else return 0; }catch(ClassCastException e){ //ignore } return 0 ; } } static int Dijkstra(ArrayList<List<Edge>> graph, int src,int dst,int C){ PriorityQueue<Edge> qu = new PriorityQueue<Edge>(); int N = graph.size(); qu.add(new Edge(src,0,0)); int visited[][] = new int[N][C+1]; for(int[] v : visited)Arrays.fill(v, 1<<29); while(!qu.isEmpty()){ Edge e = qu.poll(); if(e.dst == dst) return (int)e.weight; if(e.weight > visited[e.dst][e.money])continue; visited[e.dst][e.money] = e.weight; List<Edge> nxt = graph.get(e.dst); for(Edge next :nxt){ if(next.money + e.money > C)continue; if(visited[next.dst][next.money+e.money] > next.weight + e.weight ){ qu.add(new Edge(next.dst,next.weight+e.weight,next.money+e.money)); } } } return -1; } }