結果

問題 No.17 2つの地点に泊まりたい
ユーザー 明智重蔵明智重蔵
提出日時 2022-07-24 21:30:21
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 5,179 bytes
コンパイル時間 1,764 ms
コンパイル使用メモリ 68,356 KB
実行使用メモリ 24,036 KB
最終ジャッジ日時 2023-09-20 21:59:03
合計ジャッジ時間 4,316 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
21,920 KB
testcase_01 AC 68 ms
23,968 KB
testcase_02 AC 67 ms
23,876 KB
testcase_03 AC 68 ms
21,916 KB
testcase_04 AC 69 ms
21,788 KB
testcase_05 AC 71 ms
21,840 KB
testcase_06 AC 71 ms
21,900 KB
testcase_07 AC 70 ms
23,812 KB
testcase_08 AC 71 ms
21,796 KB
testcase_09 AC 71 ms
19,860 KB
testcase_10 AC 70 ms
23,912 KB
testcase_11 AC 70 ms
21,884 KB
testcase_12 AC 68 ms
21,904 KB
testcase_13 AC 68 ms
21,820 KB
testcase_14 AC 67 ms
21,888 KB
testcase_15 AC 68 ms
21,872 KB
testcase_16 AC 67 ms
21,828 KB
testcase_17 AC 68 ms
21,912 KB
testcase_18 AC 68 ms
19,940 KB
testcase_19 AC 68 ms
24,036 KB
testcase_20 AC 68 ms
21,848 KB
testcase_21 AC 67 ms
21,788 KB
testcase_22 AC 69 ms
23,888 KB
testcase_23 AC 70 ms
21,976 KB
testcase_24 AC 70 ms
19,976 KB
testcase_25 AC 68 ms
22,016 KB
testcase_26 AC 72 ms
23,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

// https://yukicoder.me/problems/no/17
class Program
{
    static string InputPattern = "InputX";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("4");
            WillReturn.Add("0");
            WillReturn.Add("2");
            WillReturn.Add("100");
            WillReturn.Add("0");
            WillReturn.Add("5");
            WillReturn.Add("0 1 1");
            WillReturn.Add("0 2 1");
            WillReturn.Add("1 2 1");
            WillReturn.Add("1 3 1");
            WillReturn.Add("2 3 2");
            //105
            //地点は全部で4個。
            //地点1と地点2にはかならず滞在しなければならない。
            //0→2→1→3 の順番で移動し、
            //地点1と地点2に滞在したとき最小コストになる。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("0");
            WillReturn.Add("3");
            WillReturn.Add("3");
            WillReturn.Add("0");
            WillReturn.Add("3");
            WillReturn.Add("0 1 1");
            WillReturn.Add("1 3 2");
            WillReturn.Add("2 3 4");
            //17
            //地点1と地点2にはかならず滞在しなければならない。
            //よって、0→1→3→2→3 のような移動が必要。
            //同じ地点を2度通っても良い。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("16");
            WillReturn.Add("0");
            WillReturn.Add("15");
            WillReturn.Add("6");
            WillReturn.Add("23");
            WillReturn.Add("8");
            WillReturn.Add("193");
            WillReturn.Add("14");
            WillReturn.Add("13");
            WillReturn.Add("53");
            WillReturn.Add("16");
            WillReturn.Add("85");
            WillReturn.Add("12");
            WillReturn.Add("15");
            WillReturn.Add("5");
            WillReturn.Add("14");
            WillReturn.Add("0");
            WillReturn.Add("15");
            WillReturn.Add("0 1 17");
            WillReturn.Add("14 15 18");
            WillReturn.Add("7 8 87");
            WillReturn.Add("4 5 137");
            WillReturn.Add("8 9 17");
            WillReturn.Add("11 12 33");
            WillReturn.Add("5 6 177");
            WillReturn.Add("9 10 47");
            WillReturn.Add("10 11 27");
            WillReturn.Add("1 2 77");
            WillReturn.Add("6 7 114");
            WillReturn.Add("12 13 15");
            WillReturn.Add("2 3 127");
            WillReturn.Add("13 14 11");
            WillReturn.Add("3 4 85");
            //1000
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();

        long N = long.Parse(InputList[0]);
        long UB = N - 1;

        long[,] KyoriArr = new long[UB + 1, UB + 1];

        const long INFTY = long.MaxValue;

        // 初期化処理
        for (long I = 0; I <= UB; I++) {
            for (long J = 0; J <= UB; J++) {
                KyoriArr[I, J] = (I == J) ? 0 : INFTY;
            }
        }

        var SList = new List<long>();
        foreach (string EachStr in InputList.Skip(1).Take((int)N)) {
            SList.Add(int.Parse(EachStr));
        }
        long[] SArr = SList.ToArray();

        foreach (string EachStr in InputList.Skip(1 + (int)N + 1)) {
            SplitAct(EachStr);
            KyoriArr[wkArr[0], wkArr[1]] = wkArr[2];
            KyoriArr[wkArr[1], wkArr[0]] = wkArr[2];
        }

        // ワーシャルフロイド法
        for (long K = 0; K <= UB; K++) {
            for (long I = 0; I <= UB; I++) {
                if (KyoriArr[I, K] == INFTY) continue;
                for (long J = 0; J <= UB; J++) {
                    if (KyoriArr[K, J] == INFTY) continue;
                    long CurrKouho = KyoriArr[I, K] + KyoriArr[K, J];
                    if (KyoriArr[I, J] > CurrKouho) {
                        KyoriArr[I, J] = CurrKouho;
                    }
                }
            }
        }

        // 2つの滞在地点を全探索
        var AnswerKouhoList = new List<long>();
        for (long I = 1; I <= N - 2; I++) {
            for (long J = 1; J <= N - 2; J++) {
                if (I == J) continue;
                long CurrKouho = 0;
                CurrKouho += SArr[I];
                CurrKouho += SArr[J];

                if (KyoriArr[0, I] != INFTY && KyoriArr[I, J] != INFTY && KyoriArr[J, N - 1] != INFTY) {
                    AnswerKouhoList.Add(CurrKouho + KyoriArr[0, I] + KyoriArr[I, J] + KyoriArr[J, N - 1]);
                }
            }
        }
        Console.WriteLine(AnswerKouhoList.Min());
    }
}
0