結果

問題 No.1 道のショートカット
ユーザー 14番14番
提出日時 2016-03-30 02:15:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 3,174 bytes
コンパイル時間 2,025 ms
コンパイル使用メモリ 106,156 KB
実行使用メモリ 27,856 KB
最終ジャッジ日時 2023-09-27 21:55:24
合計ジャッジ時間 6,624 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,720 KB
testcase_01 AC 60 ms
20,668 KB
testcase_02 AC 59 ms
22,720 KB
testcase_03 AC 59 ms
22,796 KB
testcase_04 AC 58 ms
20,752 KB
testcase_05 AC 57 ms
22,736 KB
testcase_06 AC 58 ms
22,688 KB
testcase_07 AC 56 ms
20,604 KB
testcase_08 AC 70 ms
21,240 KB
testcase_09 AC 66 ms
22,864 KB
testcase_10 AC 69 ms
21,028 KB
testcase_11 AC 90 ms
25,120 KB
testcase_12 AC 101 ms
27,856 KB
testcase_13 AC 96 ms
25,912 KB
testcase_14 AC 60 ms
20,736 KB
testcase_15 AC 56 ms
20,708 KB
testcase_16 AC 65 ms
22,876 KB
testcase_17 AC 59 ms
20,808 KB
testcase_18 AC 59 ms
20,712 KB
testcase_19 AC 60 ms
20,744 KB
testcase_20 AC 69 ms
22,976 KB
testcase_21 AC 67 ms
22,964 KB
testcase_22 AC 59 ms
20,720 KB
testcase_23 AC 144 ms
27,204 KB
testcase_24 AC 140 ms
25,216 KB
testcase_25 AC 78 ms
25,052 KB
testcase_26 AC 71 ms
20,960 KB
testcase_27 AC 126 ms
27,548 KB
testcase_28 AC 60 ms
20,620 KB
testcase_29 AC 74 ms
21,128 KB
testcase_30 AC 66 ms
20,836 KB
testcase_31 AC 75 ms
22,944 KB
testcase_32 AC 77 ms
22,936 KB
testcase_33 AC 72 ms
20,924 KB
testcase_34 AC 109 ms
27,252 KB
testcase_35 AC 65 ms
23,016 KB
testcase_36 AC 73 ms
20,896 KB
testcase_37 AC 70 ms
23,016 KB
testcase_38 AC 60 ms
20,688 KB
testcase_39 AC 67 ms
18,912 KB
testcase_40 AC 66 ms
23,016 KB
testcase_41 AC 64 ms
18,840 KB
testcase_42 AC 59 ms
20,684 KB
testcase_43 AC 57 ms
20,772 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    public void Proc(){
        Reader.IsDebug = false;
        int matiCount = int.Parse(Reader.ReadLine());
        this.Money = int.Parse(Reader.ReadLine());
        int roadCount = int.Parse(Reader.ReadLine());
        int[] inpt = Reader.GetInt();
        for(int i=0; i<inpt.Length; i++) {
            Road r = new Road();
            r.From = inpt[i];
            RoadList.Add(r);
            if(!RoadDic.ContainsKey(r.From)) {
                RoadDic.Add(r.From, new List<Road>());
            }
            RoadDic[r.From].Add(r);
        }
        inpt = Reader.GetInt();
        for(int i=0; i<inpt.Length; i++) {
            RoadList[i].To = inpt[i];
        }
        inpt = Reader.GetInt();
        for(int i=0; i<inpt.Length; i++) {
            RoadList[i].Cost = inpt[i];
        }
        inpt = Reader.GetInt();
        for(int i=0; i<inpt.Length; i++) {
            RoadList[i].Time = inpt[i];
        }
        int ans = this.GetAns(1, matiCount, this.Money);
        Console.WriteLine(ans);
    }
    
    private Dictionary<int, List<Road>> RoadDic = new Dictionary<int, List<Road>>();
    private Dictionary<string, int> dic = new Dictionary<string, int>();
    private int GetAns(int fromMati, int ToMati, int remain) {
        string key = fromMati + "#" + ToMati + "#" + remain;
        if(dic.ContainsKey(key)) {
            return dic[key];
        }
        if(fromMati == ToMati) {
            return 0;
        }
        if(remain < 0) {
            return -1;
        }
        int ans = int.MaxValue;
        if(RoadDic.ContainsKey(fromMati)) {
            foreach(Road r in RoadDic[fromMati]) {
                if(r.Cost <= remain) {
                    int subAns = this.GetAns(r.To, ToMati, remain - r.Cost);
                    if(subAns >= 0) {
                        ans = Math.Min(ans, subAns + r.Time);
                    }
                }
            }
        }
        if(ans == int.MaxValue) {
            ans = -1;
        }
        dic.Add(key, ans);
        return ans;
    }
    
    private List<Road> RoadList = new List<Road>();
    public class Road {
        public int From;
        public int To;
        public int Cost;
        public int Time;
    }
    
    private int Money = 0;
    
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}

class Reader
{
    public static bool IsDebug = true;
    
    private static System.IO.StringReader sr;
    
    public static string ReadLine() {
        if(IsDebug) {
            if(sr == null) {
                sr = new System.IO.StringReader(initStr.Trim());
            }
            return sr.ReadLine();
        } else {
            return Console.ReadLine();
        }
    }
    
    public static int[] GetInt(char delimiter = ' ') {
        string[] inpt = ReadLine().Split(delimiter);
        int[] ret = new int[inpt.Length];
        for(int i=0; i<inpt.Length; i++) {
            ret[i] = int.Parse(inpt[i]);
        }
        return ret;
    }
    
    private static string initStr = @"








";
}


0