結果

問題 No.1690 Power Grid
ユーザー 👑 kakel-sankakel-san
提出日時 2024-01-01 23:57:51
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 337 ms / 3,000 ms
コード長 2,520 bytes
コンパイル時間 8,766 ms
コンパイル使用メモリ 158,820 KB
実行使用メモリ 180,484 KB
最終ジャッジ日時 2024-01-01 23:58:07
合計ジャッジ時間 15,261 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
32,932 KB
testcase_01 AC 87 ms
32,932 KB
testcase_02 AC 86 ms
32,932 KB
testcase_03 AC 91 ms
33,060 KB
testcase_04 AC 88 ms
33,060 KB
testcase_05 AC 87 ms
32,932 KB
testcase_06 AC 178 ms
35,492 KB
testcase_07 AC 176 ms
35,492 KB
testcase_08 AC 173 ms
35,496 KB
testcase_09 AC 172 ms
35,496 KB
testcase_10 AC 92 ms
35,452 KB
testcase_11 AC 269 ms
35,452 KB
testcase_12 AC 84 ms
32,932 KB
testcase_13 AC 86 ms
33,316 KB
testcase_14 AC 104 ms
33,572 KB
testcase_15 AC 324 ms
35,492 KB
testcase_16 AC 189 ms
35,472 KB
testcase_17 AC 158 ms
34,340 KB
testcase_18 AC 112 ms
33,828 KB
testcase_19 AC 198 ms
35,460 KB
testcase_20 AC 203 ms
35,456 KB
testcase_21 AC 94 ms
35,464 KB
testcase_22 AC 306 ms
35,468 KB
testcase_23 AC 296 ms
35,456 KB
testcase_24 AC 333 ms
35,456 KB
testcase_25 AC 337 ms
35,460 KB
testcase_26 AC 327 ms
35,472 KB
testcase_27 AC 86 ms
180,484 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (120 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 a = NList;
        var map = NArr(m);
        var inf = long.MaxValue / 2;
        var len = new long[n, n];
        for (var i = 0; i < n; ++i) for (var j = 0; j < n; ++j) len[i, j] = inf;
        foreach (var road in map)
        {
            len[road[0] - 1, road[1] - 1] = road[2];
            len[road[1] - 1, road[0] - 1] = road[2];
        }
        for (var z = 0; z < n; ++z) for (var x = 0; x < n; ++x) for (var y = 0; y < n; ++y)
        {
            len[x, y] = Math.Min(len[x, y], len[x, z] + len[z, y]);
        }
        var bitmax = 1 << n;
        var dp = Enumerable.Repeat(inf, bitmax).ToArray();
        dp[0] = 0;
        for (var b = 0; b < bitmax; ++b)
        {
            if (b == 0)
            {
                for (var i = 0; i < n; ++i) dp[1 << i] = a[i];
            }
            else if (Popcount(b) >= k)
            {
                continue;
            }
            else
            {
                for (var i = 0; i < n; ++i)
                {
                    if ((b & (1 << i)) != 0) continue;
                    var near = inf;
                    for (var j = 0; j < n; ++j) if ((b & (1 << j)) != 0) near = Math.Min(near, len[i, j]);
                    dp[b | (1 << i)] = Math.Min(dp[b | (1 << i)], dp[b] + a[i] + near);
                }
            }
        }
        var ans = inf;
        for (var i = 0; i < bitmax; ++i) if (Popcount(i) == k) ans = Math.Min(ans, dp[i]);
        WriteLine(ans);
    }
    static int Popcount(long n)
    {
        var c = (n & 0x5555555555555555) + ((n>>1) & 0x5555555555555555);
        c = (c & 0x3333333333333333) + ((c>>2) & 0x3333333333333333);
        c = (c & 0x0f0f0f0f0f0f0f0f) + ((c>>4) & 0x0f0f0f0f0f0f0f0f);
        c = (c & 0x00ff00ff00ff00ff) + ((c>>8) & 0x00ff00ff00ff00ff);
        c = (c & 0x0000ffff0000ffff) + ((c>>16) & 0x0000ffff0000ffff);
        c = (c & 0x00000000ffffffff) + ((c>>32) & 0x00000000ffffffff);
        return (int)c;
    }
}
0