結果

問題 No.1 道のショートカット
ユーザー maruyuki95maruyuki95
提出日時 2020-05-18 16:54:00
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,992 bytes
コンパイル時間 2,875 ms
コンパイル使用メモリ 80,728 KB
実行使用メモリ 617,028 KB
最終ジャッジ日時 2023-09-22 13:46:33
合計ジャッジ時間 13,663 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,840 KB
testcase_01 AC 123 ms
55,780 KB
testcase_02 AC 129 ms
55,928 KB
testcase_03 AC 131 ms
55,812 KB
testcase_04 AC 123 ms
55,976 KB
testcase_05 AC 123 ms
55,580 KB
testcase_06 AC 132 ms
56,120 KB
testcase_07 AC 139 ms
57,780 KB
testcase_08 AC 478 ms
62,328 KB
testcase_09 AC 247 ms
59,760 KB
testcase_10 AC 522 ms
63,412 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {

		// 目的地にたどり着く経路を検索
		List<Integer> routes = new ArrayList<Integer>();
		List<List<Integer>> compRoutes = new ArrayList<List<Integer>>();
		searchRoute(1, cityNum, cityS, cityT, routes, compRoutes, money, costs);

		if(compRoutes.isEmpty()) {
			return -1;
		}

		// 経路のうち最短時間を計算する
		return calcurateMinTime(compRoutes, times);
	}

	private void searchRoute(int location, int goal, List<Integer> cityS, List<Integer> cityT, List<Integer> routes, List<List<Integer>> compRoutes, int money, List<Integer> costs) {
		for (int i = 0; i < cityS.size(); i++) {
			int start = cityS.get(i);
			if (start != location) {	// 道の入り口が現在地と異なるのでスキップ
				continue;
			}

			Integer cost = costs.get(i);
			if (money < cost) {	// 資金が足りないのでスキップ
				continue;
			}

			List<Integer> newRoutes = new ArrayList<Integer>(routes);
			newRoutes.add(i);
			if (goal == cityT.get(i)) {	// 目的地にたどり着いたのでcompに入れて再探索
				compRoutes.add(newRoutes);
				continue;
			}else {	// 道の出口が目的地ではないため、再帰して探索
				int newMoney = money - cost;
				searchRoute(cityT.get(i), goal, cityS, cityT, newRoutes, compRoutes, newMoney, costs);
			}
		}
	}

	private int sumAmountWithRoute(List<Integer> route, List<Integer> values) {
		int ret = 0;
		for (Integer roadNum : route) {
			ret += values.get(roadNum);
		}
		return ret;
	}

	private int calcurateMinTime(List<List<Integer>> filteredByMoneyRoutes, List<Integer> times) {
		if (filteredByMoneyRoutes == null || filteredByMoneyRoutes.isEmpty()) {
			throw new IllegalArgumentException("No routes.");
		}

		Integer minTime = null;
		for (List<Integer> route : filteredByMoneyRoutes) {
			int totalTime = sumAmountWithRoute(route, times);
			if (minTime == null || totalTime < minTime.intValue()) {
				minTime = totalTime;
			}
		}

		return minTime.intValue();
	}


}
0