結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2016-05-10 15:46:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,025 ms / 5,000 ms |
コード長 | 3,772 bytes |
コンパイル時間 | 2,870 ms |
コンパイル使用メモリ | 81,588 KB |
実行使用メモリ | 61,492 KB |
最終ジャッジ日時 | 2024-10-05 13:30:21 |
合計ジャッジ時間 | 9,325 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] lines = br.readLine().split(" "); int n = Integer.parseInt(lines[0]); int m = Integer.parseInt(lines[1]); int s = Integer.parseInt(lines[2]); int g = Integer.parseInt(lines[3]); ArrayList<Station> stations = new ArrayList<Station>(); for(int i=0;i<n;i++){ stations.add(new Station(i)); } stations.get(s).min_cost = 0; stations.get(s).min_route.add(s); for(int i=0;i<m;i++){ lines = br.readLine().split(" "); int a = Integer.parseInt(lines[0]); int b = Integer.parseInt(lines[1]); int c = Integer.parseInt(lines[2]); stations.get(a).addNext(stations.get(b),c); stations.get(b).addNext(stations.get(a),c); } PriorityQueue<Station> queue = new PriorityQueue<Station>(200,(a,b)->a.compare(b)); queue.add(stations.get(s)); while(queue.size()!=0){ Station sta = queue.poll(); for(Route route:sta.next){ Station clone = sta.clone(); clone.min_route.add(route.station.no); clone.min_cost += route.cost; if(clone.compare(route.station) < 0){ route.station.min_route = new ArrayList<Integer>(clone.min_route); route.station.min_cost = clone.min_cost; queue.add(route.station); } } } System.out.println(stations.get(g).toString()); } public static class Station{ public ArrayList<Route> next = new ArrayList<Route>(); public int no; public int min_cost = Integer.MAX_VALUE; public ArrayList<Integer> min_route = new ArrayList<Integer>(); public Station(int no){ this.no = no; } public void addNext(Station station,int cost){ next.add(new Route(station,cost)); } public int compare(Station target){ if(this.min_cost != target.min_cost){ return this.min_cost - target.min_cost; }else{ int n = Math.min(this.min_route.size(),target.min_route.size()); for(int i=0;i<n;i++){ if(this.min_route.get(i) != target.min_route.get(i)){ return this.min_route.get(i) - target.min_route.get(i); } } } return 0; } public Station clone(){ Station new_obj = new Station(this.no); new_obj.min_cost = this.min_cost; new_obj.min_route = new ArrayList<Integer>(this.min_route); new_obj.next = this.next; //ディープコピーしなくてもOK return new_obj; } public String toString(){ StringBuilder sb = new StringBuilder(); for(int route:min_route){ if(sb.length() != 0){ sb.append(" "); } sb.append(route); } return sb.toString(); } } public static class Route{ public Station station; public int cost; public Route(Station station,int cost){ this.station = station; this.cost = cost; } } }