結果
問題 | No.1 道のショートカット |
ユーザー | maruyuki95 |
提出日時 | 2020-05-18 17:42:02 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 230 ms / 5,000 ms |
コード長 | 2,439 bytes |
コンパイル時間 | 3,974 ms |
コンパイル使用メモリ | 73,916 KB |
実行使用メモリ | 60,408 KB |
最終ジャッジ日時 | 2023-09-27 22:28:55 |
合計ジャッジ時間 | 10,816 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 120 ms
55,364 KB |
testcase_01 | AC | 116 ms
55,708 KB |
testcase_02 | AC | 115 ms
55,668 KB |
testcase_03 | AC | 118 ms
56,692 KB |
testcase_04 | AC | 116 ms
55,744 KB |
testcase_05 | AC | 115 ms
55,848 KB |
testcase_06 | AC | 123 ms
55,900 KB |
testcase_07 | AC | 131 ms
55,760 KB |
testcase_08 | AC | 228 ms
60,408 KB |
testcase_09 | AC | 192 ms
58,880 KB |
testcase_10 | AC | 218 ms
59,220 KB |
testcase_11 | AC | 206 ms
59,020 KB |
testcase_12 | AC | 230 ms
59,752 KB |
testcase_13 | AC | 222 ms
59,888 KB |
testcase_14 | AC | 167 ms
55,976 KB |
testcase_15 | AC | 117 ms
55,876 KB |
testcase_16 | AC | 189 ms
53,936 KB |
testcase_17 | AC | 114 ms
55,956 KB |
testcase_18 | AC | 163 ms
56,000 KB |
testcase_19 | AC | 162 ms
55,876 KB |
testcase_20 | AC | 176 ms
56,060 KB |
testcase_21 | AC | 156 ms
56,212 KB |
testcase_22 | AC | 138 ms
56,032 KB |
testcase_23 | AC | 223 ms
59,592 KB |
testcase_24 | AC | 208 ms
59,036 KB |
testcase_25 | AC | 180 ms
56,556 KB |
testcase_26 | AC | 176 ms
56,016 KB |
testcase_27 | AC | 213 ms
59,900 KB |
testcase_28 | AC | 120 ms
56,408 KB |
testcase_29 | AC | 216 ms
59,584 KB |
testcase_30 | AC | 180 ms
56,556 KB |
testcase_31 | AC | 188 ms
58,704 KB |
testcase_32 | AC | 199 ms
58,488 KB |
testcase_33 | AC | 186 ms
58,396 KB |
testcase_34 | AC | 213 ms
59,260 KB |
testcase_35 | AC | 197 ms
59,284 KB |
testcase_36 | AC | 202 ms
59,616 KB |
testcase_37 | AC | 201 ms
58,944 KB |
testcase_38 | AC | 136 ms
56,024 KB |
testcase_39 | AC | 166 ms
55,884 KB |
testcase_40 | AC | 180 ms
58,468 KB |
testcase_41 | AC | 164 ms
56,064 KB |
testcase_42 | AC | 118 ms
55,984 KB |
testcase_43 | AC | 112 ms
55,700 KB |
ソースコード
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cityNum = sc.nextInt(); int money = sc.nextInt(); int roadNum = sc.nextInt(); List<Integer> cityS = new ArrayList<Integer>(); for (int i = 0; i < roadNum; i++) { cityS.add(sc.nextInt()); } List<Integer> cityT = new ArrayList<Integer>(); for (int i = 0; i < roadNum; i++) { cityT.add(sc.nextInt()); } List<Integer> costs = new ArrayList<Integer>(); for (int i = 0; i < roadNum; i++) { costs.add(sc.nextInt()); } List<Integer> times = new ArrayList<Integer>(); for (int i = 0; i < roadNum; i++) { times.add(sc.nextInt()); } int ret = new Main().execute(cityNum, money, roadNum, cityS, cityT, costs, times); System.out.println(ret); } private int execute(int cityNum, int money, int roadNum, List<Integer> cityS, List<Integer> cityT, List<Integer> costs, List<Integer> times) { searchRoute(1, cityNum, cityS, cityT, money, costs, 0, times); if(minTime == null) { return -1; } return minTime.intValue(); } private Integer minTime = null; private void searchRoute(int location, int goal, List<Integer> cityS, List<Integer> cityT, int money, List<Integer> costs, int totalTime, List<Integer> times) { List<Integer> roadNums = filterRoadNumsWithLocation(cityS, location); // 道の入り口が現在地と同じ道のみに絞る for (Integer roadNum : roadNums) { Integer cost = costs.get(roadNum); if (money < cost) { // 資金が足りないのでスキップ continue; } int afterTime = totalTime + times.get(roadNum); if (minTime != null && minTime.intValue() <= afterTime) { // 既に最短時間よりも時間がかかっているためスキップ continue; } Integer afterLocation = cityT.get(roadNum); if (goal == afterLocation) { minTime = afterTime; continue; }else { // 道の出口が目的地ではないため、再帰して探索 int afterMoney = money - cost; searchRoute(afterLocation, goal, cityS, cityT, afterMoney, costs, afterTime, times); } } } private List<Integer> filterRoadNumsWithLocation(List<Integer> cityS, int location) { List<Integer> ret = new ArrayList<Integer>(); for (int i = 0; i < cityS.size(); i++) { if (cityS.get(i) == location) { ret.add(i); } } return ret; } }