結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | autotaker1984 |
提出日時 | 2015-07-18 12:41:18 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 739 ms / 5,000 ms |
コード長 | 2,857 bytes |
コンパイル時間 | 2,576 ms |
コンパイル使用メモリ | 80,956 KB |
実行使用メモリ | 49,788 KB |
最終ジャッジ日時 | 2024-07-08 10:08:40 |
合計ジャッジ時間 | 12,973 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 155 ms
41,792 KB |
testcase_01 | AC | 155 ms
41,532 KB |
testcase_02 | AC | 155 ms
41,832 KB |
testcase_03 | AC | 162 ms
42,064 KB |
testcase_04 | AC | 328 ms
47,860 KB |
testcase_05 | AC | 397 ms
48,528 KB |
testcase_06 | AC | 486 ms
49,128 KB |
testcase_07 | AC | 298 ms
47,304 KB |
testcase_08 | AC | 310 ms
47,168 KB |
testcase_09 | AC | 284 ms
46,732 KB |
testcase_10 | AC | 321 ms
47,856 KB |
testcase_11 | AC | 320 ms
47,816 KB |
testcase_12 | AC | 320 ms
47,388 KB |
testcase_13 | AC | 300 ms
47,164 KB |
testcase_14 | AC | 336 ms
47,856 KB |
testcase_15 | AC | 320 ms
47,576 KB |
testcase_16 | AC | 289 ms
47,048 KB |
testcase_17 | AC | 307 ms
47,212 KB |
testcase_18 | AC | 299 ms
46,828 KB |
testcase_19 | AC | 287 ms
46,556 KB |
testcase_20 | AC | 286 ms
46,912 KB |
testcase_21 | AC | 310 ms
47,348 KB |
testcase_22 | AC | 319 ms
47,252 KB |
testcase_23 | AC | 312 ms
47,432 KB |
testcase_24 | AC | 303 ms
47,160 KB |
testcase_25 | AC | 304 ms
47,236 KB |
testcase_26 | AC | 299 ms
47,240 KB |
testcase_27 | AC | 255 ms
44,192 KB |
testcase_28 | AC | 739 ms
49,788 KB |
testcase_29 | AC | 250 ms
43,748 KB |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); int S = in.nextInt(); int G = in.nextInt(); Dijkstra d = new Dijkstra(N); for( int i = 0; i < M; i++ ) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); d.addEdge(a,b,c); d.addEdge(b,a,c); } int[] dist = d.run(S); ArrayList<ArrayList<Edge>> g = new ArrayList<ArrayList<Edge>>(); for( int i = 0; i < N; i++ ) { ArrayList<Edge> es = new ArrayList<Edge>(); for( Edge e : d.g.get(i) ) { if( dist[e.from] + e.weight == dist[e.to] ) { es.add(e); } } g.add(es); } int v = S; while( v != G ) { System.out.print(v + " "); ArrayList<Edge> es = g.get(v); v = N; for( Edge e : es ) { if( !reachable(g,e.to,G) ) continue; v = Math.min(v, e.to); } } System.out.println(v); } static boolean reachable(ArrayList<ArrayList<Edge>> g, int s, int t){ boolean[] vis = new boolean[g.size()]; Deque<Integer> stack = new ArrayDeque<Integer>(); stack.push(s); while(!stack.isEmpty()) { int v = stack.pop(); if( v == t ) return true; if( vis[v] ) continue; vis[v] = true; for( Edge e : g.get(v) ) { stack.push(e.to); } } return false; } } class Edge implements Comparable<Edge>{ int from; int to; int weight; public Edge(int from, int to, int weight) { this.from = from; this.to = to; this.weight = weight; } public int compareTo(Edge e) { if( this.weight < e.weight ) return -1; if( this.weight > e.weight ) return 1; if( this.from < e.from ) return -1; if( this.from > e.from ) return 1; if( this.to < e.to ) return -1; if( this.to > e.to ) return 1; return 0; } } class Dijkstra { int N; ArrayList<ArrayList<Edge>> g; public Dijkstra(int N) { this.N = N; g = new ArrayList<ArrayList<Edge>>(); for( int i = 0; i < N; i++ ) { g.add(new ArrayList<Edge>()); } } public void addEdge(Edge e) { g.get(e.from).add(e); } public void addEdge(int from, int to, int weight) { addEdge(new Edge(from, to, weight)); } public int[] run(int start) { boolean[] vis = new boolean[N]; int[] dist = new int[N]; PriorityQueue<Edge> queue = new PriorityQueue<Edge>(); queue.add(new Edge(start,0,0)); while( !queue.isEmpty() ) { Edge e = queue.poll(); if( vis[e.from] ) continue; vis[e.from] = true; dist[e.from] = e.weight; for( Edge ne : g.get(e.from) ) { if( !vis[ne.to] ) { queue.add(new Edge(ne.to,0,ne.weight + e.weight)); } } } return dist; } }