結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | yun_app |
提出日時 | 2016-05-10 15:46:21 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,069 ms / 5,000 ms |
コード長 | 3,772 bytes |
コンパイル時間 | 2,778 ms |
コンパイル使用メモリ | 81,332 KB |
実行使用メモリ | 50,064 KB |
最終ジャッジ日時 | 2024-04-15 15:05:15 |
合計ジャッジ時間 | 9,747 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
37,484 KB |
testcase_01 | AC | 64 ms
37,516 KB |
testcase_02 | AC | 61 ms
37,408 KB |
testcase_03 | AC | 65 ms
37,704 KB |
testcase_04 | AC | 186 ms
44,060 KB |
testcase_05 | AC | 225 ms
45,180 KB |
testcase_06 | AC | 249 ms
45,792 KB |
testcase_07 | AC | 164 ms
43,480 KB |
testcase_08 | AC | 173 ms
44,160 KB |
testcase_09 | AC | 148 ms
43,476 KB |
testcase_10 | AC | 180 ms
43,992 KB |
testcase_11 | AC | 182 ms
44,292 KB |
testcase_12 | AC | 174 ms
43,964 KB |
testcase_13 | AC | 165 ms
43,672 KB |
testcase_14 | AC | 181 ms
43,816 KB |
testcase_15 | AC | 169 ms
43,820 KB |
testcase_16 | AC | 167 ms
43,624 KB |
testcase_17 | AC | 180 ms
43,708 KB |
testcase_18 | AC | 165 ms
43,484 KB |
testcase_19 | AC | 158 ms
43,460 KB |
testcase_20 | AC | 165 ms
43,412 KB |
testcase_21 | AC | 168 ms
43,356 KB |
testcase_22 | AC | 168 ms
43,444 KB |
testcase_23 | AC | 174 ms
43,724 KB |
testcase_24 | AC | 170 ms
43,412 KB |
testcase_25 | AC | 168 ms
43,640 KB |
testcase_26 | AC | 163 ms
43,480 KB |
testcase_27 | AC | 83 ms
38,592 KB |
testcase_28 | AC | 1,069 ms
50,064 KB |
testcase_29 | AC | 88 ms
38,520 KB |
ソースコード
/* 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; } } }