結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | yun_app |
提出日時 | 2016-05-10 15:46:21 |
言語 | Java21 (openjdk 21) |
結果 |
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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
50,772 KB |
testcase_01 | AC | 62 ms
50,648 KB |
testcase_02 | AC | 62 ms
50,728 KB |
testcase_03 | AC | 64 ms
50,720 KB |
testcase_04 | AC | 174 ms
55,244 KB |
testcase_05 | AC | 207 ms
56,100 KB |
testcase_06 | AC | 238 ms
56,264 KB |
testcase_07 | AC | 160 ms
55,236 KB |
testcase_08 | AC | 163 ms
55,008 KB |
testcase_09 | AC | 150 ms
55,132 KB |
testcase_10 | AC | 167 ms
55,004 KB |
testcase_11 | AC | 170 ms
55,416 KB |
testcase_12 | AC | 164 ms
55,088 KB |
testcase_13 | AC | 153 ms
54,844 KB |
testcase_14 | AC | 178 ms
55,192 KB |
testcase_15 | AC | 160 ms
54,836 KB |
testcase_16 | AC | 157 ms
54,844 KB |
testcase_17 | AC | 168 ms
54,988 KB |
testcase_18 | AC | 146 ms
54,844 KB |
testcase_19 | AC | 153 ms
55,288 KB |
testcase_20 | AC | 144 ms
55,504 KB |
testcase_21 | AC | 158 ms
54,852 KB |
testcase_22 | AC | 159 ms
55,252 KB |
testcase_23 | AC | 152 ms
55,140 KB |
testcase_24 | AC | 162 ms
55,328 KB |
testcase_25 | AC | 148 ms
55,156 KB |
testcase_26 | AC | 156 ms
54,960 KB |
testcase_27 | AC | 82 ms
51,280 KB |
testcase_28 | AC | 1,025 ms
61,492 KB |
testcase_29 | AC | 86 ms
50,624 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; } } }