結果

問題 No.1 道のショートカット
ユーザー 明智重蔵明智重蔵
提出日時 2015-09-19 06:37:02
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 374 ms / 5,000 ms
コード長 4,578 bytes
コンパイル時間 3,970 ms
コンパイル使用メモリ 110,264 KB
実行使用メモリ 24,120 KB
最終ジャッジ日時 2023-09-27 21:48:04
合計ジャッジ時間 7,621 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
23,772 KB
testcase_01 AC 59 ms
21,900 KB
testcase_02 AC 62 ms
21,720 KB
testcase_03 AC 63 ms
21,904 KB
testcase_04 AC 62 ms
21,820 KB
testcase_05 AC 58 ms
19,752 KB
testcase_06 AC 62 ms
21,628 KB
testcase_07 AC 61 ms
21,732 KB
testcase_08 AC 77 ms
21,932 KB
testcase_09 AC 70 ms
19,900 KB
testcase_10 AC 75 ms
21,952 KB
testcase_11 AC 99 ms
22,012 KB
testcase_12 AC 129 ms
21,976 KB
testcase_13 AC 126 ms
21,992 KB
testcase_14 AC 63 ms
21,696 KB
testcase_15 AC 59 ms
21,824 KB
testcase_16 AC 69 ms
21,928 KB
testcase_17 AC 61 ms
21,792 KB
testcase_18 AC 61 ms
23,728 KB
testcase_19 AC 61 ms
21,828 KB
testcase_20 AC 71 ms
21,920 KB
testcase_21 AC 69 ms
21,844 KB
testcase_22 AC 63 ms
23,788 KB
testcase_23 AC 340 ms
24,028 KB
testcase_24 AC 374 ms
21,992 KB
testcase_25 AC 82 ms
21,848 KB
testcase_26 AC 74 ms
21,976 KB
testcase_27 AC 157 ms
21,996 KB
testcase_28 AC 64 ms
23,724 KB
testcase_29 AC 80 ms
21,868 KB
testcase_30 AC 69 ms
23,932 KB
testcase_31 AC 75 ms
24,004 KB
testcase_32 AC 76 ms
23,940 KB
testcase_33 AC 70 ms
23,896 KB
testcase_34 AC 104 ms
24,120 KB
testcase_35 AC 66 ms
23,968 KB
testcase_36 AC 69 ms
21,872 KB
testcase_37 AC 69 ms
21,872 KB
testcase_38 AC 61 ms
21,752 KB
testcase_39 AC 70 ms
21,920 KB
testcase_40 AC 74 ms
21,800 KB
testcase_41 AC 72 ms
22,028 KB
testcase_42 AC 61 ms
21,800 KB
testcase_43 AC 60 ms
21,832 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.Linq;

class Program
{
    static string InputPattern = "Input4";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            WillReturn.Add("100");
            WillReturn.Add("3");
            WillReturn.Add("1 2 1");
            WillReturn.Add("2 3 3");
            WillReturn.Add("10 90 10");
            WillReturn.Add("10 10 50");
            //20
            //1→3 と行くと コスト10だが、単位時間が50掛かる。
            //1→2→3 と行くと 一文無しになるが、20単位時間なので早いのでこちらを選択する。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3");
            WillReturn.Add("100");
            WillReturn.Add("3");
            WillReturn.Add("1 2 1");
            WillReturn.Add("2 3 3");
            WillReturn.Add("1 100 10");
            WillReturn.Add("10 10 50");
            //50
            //1→2→3 と行くと時間は20と短いが、コスト101かかるため選択できない。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10");
            WillReturn.Add("10");
            WillReturn.Add("19");
            WillReturn.Add("1 1 2 4 5 1 3 4 6 4 6 4 5 7 8 2 3 4 9");
            WillReturn.Add("3 5 5 5 6 7 7 7 7 8 8 9 9 9 9 10 10 10 10");
            WillReturn.Add("8 6 8 7 6 6 9 9 7 6 9 7 7 8 7 6 6 8 6");
            WillReturn.Add("8 9 10 4 10 3 5 9 3 4 1 8 3 1 3 6 6 10 4");
            //-1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int CurrPos;
        internal int SumC;
        internal int SumM;
        //internal string Path;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int N = int.Parse(InputList[0]);
        int C = int.Parse(InputList[1]);
        int[] SArr = InputList[3].Split(' ').Select(X => int.Parse(X)).ToArray();
        int[] TArr = InputList[4].Split(' ').Select(X => int.Parse(X)).ToArray();
        int[] YArr = InputList[5].Split(' ').Select(X => int.Parse(X)).ToArray();
        int[] MArr = InputList[6].Split(' ').Select(X => int.Parse(X)).ToArray();
        int UB = SArr.GetUpperBound(0);

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrPos = 1;
        WillPush.SumC = WillPush.SumM = 0;
        //WillPush.Path = "1";
        stk.Push(WillPush);

        //SumM[SumC]なDictの配列(ノード番号を添字とする)
        var MemoDictArr = new Dictionary<int, int>[N + 1];
        for (int I = 1; I <= N; I++) {
            MemoDictArr[I] = new Dictionary<int, int>();
        }

        bool HasAnswer = false;
        //string AnswerPath = "";
        int AnswerSumM = int.MaxValue;

        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();

            //クリア処理
            if (Popped.CurrPos == N) {
                HasAnswer = true;
                if (AnswerSumM > Popped.SumM) {
                    AnswerSumM = Popped.SumM;
                    //AnswerPath = Popped.Path;
                    //Console.WriteLine("解候補{0}を発見", Popped.Path);
                }
                continue;
            }

            for (int I = 0; I <= UB; I++) {
                if (Popped.CurrPos != SArr[I]) continue;
                WillPush.CurrPos = TArr[I];
                WillPush.SumC = Popped.SumC + YArr[I];
                WillPush.SumM = Popped.SumM + MArr[I];
                //WillPush.Path = Popped.Path + string.Format(",{0}", TArr[I]);

                if (WillPush.SumC > C) continue;

                if (MemoDictArr[WillPush.CurrPos].ContainsKey(WillPush.SumC)) {
                    if (MemoDictArr[WillPush.CurrPos][WillPush.SumC]
                        < WillPush.SumM) {
                        continue;
                    }
                }
                MemoDictArr[WillPush.CurrPos][WillPush.SumC] = WillPush.SumM;

                stk.Push(WillPush);
            }
        }
        if (HasAnswer) {
            //Console.WriteLine(new string('■', 15));
            //Console.WriteLine("解を発見");
            Console.WriteLine(AnswerSumM);
            //Console.WriteLine("経路は{0}", AnswerPath);
        }
        else Console.WriteLine(-1);
    }
}
0