結果

問題 No.1812 Uribo Road
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-22 00:19:57
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 335 ms / 5,000 ms
コード長 3,752 bytes
コンパイル時間 9,312 ms
コンパイル使用メモリ 159,072 KB
実行使用メモリ 190,272 KB
最終ジャッジ日時 2023-12-22 00:20:14
合計ジャッジ時間 13,773 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
33,188 KB
testcase_01 AC 89 ms
33,188 KB
testcase_02 AC 91 ms
33,700 KB
testcase_03 AC 103 ms
34,084 KB
testcase_04 AC 86 ms
33,188 KB
testcase_05 AC 90 ms
33,188 KB
testcase_06 AC 90 ms
33,188 KB
testcase_07 AC 92 ms
33,700 KB
testcase_08 AC 96 ms
33,956 KB
testcase_09 AC 100 ms
34,340 KB
testcase_10 AC 96 ms
33,828 KB
testcase_11 AC 97 ms
34,084 KB
testcase_12 AC 199 ms
39,460 KB
testcase_13 AC 135 ms
39,332 KB
testcase_14 AC 133 ms
39,332 KB
testcase_15 AC 169 ms
44,068 KB
testcase_16 AC 163 ms
37,924 KB
testcase_17 AC 321 ms
47,396 KB
testcase_18 AC 133 ms
41,764 KB
testcase_19 AC 335 ms
48,096 KB
testcase_20 AC 332 ms
48,096 KB
testcase_21 AC 305 ms
48,200 KB
testcase_22 AC 213 ms
46,372 KB
testcase_23 AC 96 ms
33,956 KB
testcase_24 AC 90 ms
33,316 KB
testcase_25 AC 123 ms
39,972 KB
testcase_26 AC 95 ms
34,212 KB
testcase_27 AC 125 ms
40,228 KB
testcase_28 AC 154 ms
41,636 KB
testcase_29 AC 92 ms
33,188 KB
testcase_30 AC 98 ms
35,108 KB
testcase_31 AC 215 ms
44,032 KB
testcase_32 AC 92 ms
33,828 KB
testcase_33 AC 205 ms
190,272 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (100 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m, k) = (c[0], c[1], c[2]);
        var r = NList;
        var map = NArr(m);
        Array.Sort(r);

        var tree = new List<(int to, int len)>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int len)>();
        for (var i = 0; i < map.Length; ++i)
        {
            tree[map[i][0] - 1].Add((map[i][1] - 1, map[i][2]));
            tree[map[i][1] - 1].Add((map[i][0] - 1, map[i][2]));
        }

        var re = new int[k * 2 + 2];
        re[1] = n - 1;
        for (var i = 0; i < k; ++i)
        {
            re[i * 2 + 2] = map[r[i] - 1][0] - 1;
            re[i * 2 + 3] = map[r[i] - 1][1] - 1;
        }
        var mx = new int[k * 2 + 2, k * 2 + 2];
        for (var i = 0; i < re.Length; ++i)
        {
            var len = GetLen(re[i], tree);
            for (var j = 0; j < re.Length; ++j)
            {
                if (j < 2) mx[i, j] = len[re[j]];
                else mx[i, j] = len[re[j ^ 1]] + map[r[j / 2 - 1] - 1][2];
            }
        }
        var bitmax = 1 << k;
        var dp = new int[bitmax, k * 2];
        for (var i = 0; i < bitmax; ++i) for (var j = 0; j < k * 2; ++j) dp[i, j] = INF;
        dp[0, 0] = 0;
        for (var b = 0; b < bitmax; ++b)
        {
            if (b == 0)
            {
                for (var next = 0; next < k * 2; ++next) dp[1 << next / 2, next] = mx[0, next + 2];
            }
            else
            {
                for (var prev = 0; prev < k * 2; ++prev) for (var next = 0; next < k * 2; ++next)
                {
                    if ((b & (1 << prev / 2)) == 0) continue;
                    if ((b & (1 << next / 2)) != 0) continue;
                    dp[b | (1 << next / 2), next] = Math.Min(dp[b | (1 << next / 2), next], dp[b, prev] + mx[prev + 2, next + 2]);
                }
            }
        }
        var ans = INF;
        for (var i = 0; i < k * 2; ++i) ans = Math.Min(ans, dp[bitmax - 1, i] + mx[i + 2, 1]);
        WriteLine(ans);
    }
    static void DebugTable<T>(T[,] table)
    {
        Console.WriteLine("{");
        for (var i = 0; i < table.GetLength(0); ++i)
        {
            var s = i == 0 ? "{" : " ";
            for (var j = 0; j < table.GetLength(1); ++j)
            {
                s += j > 0 ? ", " : "";
                s += table[i, j].ToString();
            }
            s += i == table.GetLength(0) - 1 ? "}" : "";
            Console.WriteLine(s);
        }
        Console.WriteLine("}");
    }
    static int INF = int.MaxValue / 2;
    static int[] GetLen(int start, List<(int to, int len)>[] tree)
    {
        var ans = Enumerable.Repeat(INF, tree.Length).ToArray();
        ans[start] = 0;
        var q = new PriorityQueue<(int to, int len), int>();
        q.Enqueue((start, 0), 0);
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            if (ans[cur.to] != cur.len) continue;
            foreach (var next in tree[cur.to])
            {
                if (ans[next.to] <= cur.len + next.len) continue;
                ans[next.to] = cur.len + next.len;
                q.Enqueue((next.to, ans[next.to]), ans[next.to]);
            }
        }
        return ans;
    }
}
0