結果
| 問題 | 
                            No.1 道のショートカット
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-05-18 16:32:39 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 3,321 bytes | 
| コンパイル時間 | 1,968 ms | 
| コンパイル使用メモリ | 79,468 KB | 
| 実行使用メモリ | 553,104 KB | 
| 最終ジャッジ日時 | 2024-07-08 05:25:46 | 
| 合計ジャッジ時間 | 9,914 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 4 MLE * 1 -- * 35 | 
ソースコード
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);
		// たどり着く経路から、手持ち資金で選択できる経路を抽出する
		List<List<Integer>> filteredByMoneyRoutes = filterByMoney(compRoutes, money, costs);
		if(filteredByMoneyRoutes.isEmpty()) {
			return -1;
		}
		// 経路のうち最短時間を計算する
		return calcurateMinTime(filteredByMoneyRoutes, times);
	}
	private void searchRoute(int location, int goal, List<Integer> cityS, List<Integer> cityT, List<Integer> routes, List<List<Integer>> compRoutes) {
		for (int i = 0; i < cityS.size(); i++) {
			int start = cityS.get(i);
			if (start != location) {	// 道の入り口が現在地と異なるのでスキップ
				continue;
			}
			List<Integer> newRoutes = new ArrayList<Integer>(routes);
			newRoutes.add(i);
			if (goal == cityT.get(i)) {	// 目的地にたどり着いたのでcompに入れて再探索
				compRoutes.add(newRoutes);
				continue;
			}else {	// 道の出口が目的地ではないため、再帰して探索
				searchRoute(cityT.get(i), goal, cityS, cityT, newRoutes, compRoutes);
			}
		}
	}
	private List<List<Integer>> filterByMoney(List<List<Integer>> compRoutes, int money, List<Integer> costs) {
		List<List<Integer>> ret = new ArrayList<List<Integer>>();
		for (List<Integer> route : compRoutes) {
			int totalCost = sumAmountWithRoute(route, costs);
			if (totalCost <= money) {
				ret.add(route);
			}
		}
		return ret;
	}
	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();
	}
}