結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-25 22:18:31
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 2,113 ms / 3,000 ms
コード長 4,937 bytes
コンパイル時間 8,605 ms
コンパイル使用メモリ 157,052 KB
実行使用メモリ 452,380 KB
最終ジャッジ日時 2024-02-25 22:19:19
合計ジャッジ時間 43,348 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
33,316 KB
testcase_01 AC 84 ms
33,060 KB
testcase_02 AC 86 ms
33,316 KB
testcase_03 AC 87 ms
33,444 KB
testcase_04 AC 578 ms
115,296 KB
testcase_05 AC 2,113 ms
237,924 KB
testcase_06 AC 561 ms
109,348 KB
testcase_07 AC 508 ms
103,768 KB
testcase_08 AC 1,537 ms
201,884 KB
testcase_09 AC 306 ms
75,748 KB
testcase_10 AC 240 ms
70,640 KB
testcase_11 AC 850 ms
135,828 KB
testcase_12 AC 1,668 ms
202,336 KB
testcase_13 AC 303 ms
81,012 KB
testcase_14 AC 646 ms
125,848 KB
testcase_15 AC 124 ms
49,956 KB
testcase_16 AC 993 ms
164,012 KB
testcase_17 AC 188 ms
62,148 KB
testcase_18 AC 480 ms
89,576 KB
testcase_19 AC 569 ms
108,352 KB
testcase_20 AC 263 ms
76,012 KB
testcase_21 AC 1,185 ms
191,364 KB
testcase_22 AC 442 ms
117,428 KB
testcase_23 AC 392 ms
78,596 KB
testcase_24 AC 116 ms
49,956 KB
testcase_25 AC 130 ms
56,228 KB
testcase_26 AC 88 ms
33,700 KB
testcase_27 AC 110 ms
41,252 KB
testcase_28 AC 156 ms
61,412 KB
testcase_29 AC 134 ms
61,372 KB
testcase_30 AC 120 ms
50,596 KB
testcase_31 AC 131 ms
53,284 KB
testcase_32 AC 106 ms
42,276 KB
testcase_33 AC 146 ms
61,468 KB
testcase_34 AC 142 ms
55,972 KB
testcase_35 AC 259 ms
96,992 KB
testcase_36 AC 223 ms
96,832 KB
testcase_37 AC 96 ms
37,540 KB
testcase_38 AC 150 ms
61,712 KB
testcase_39 AC 99 ms
38,692 KB
testcase_40 AC 109 ms
45,476 KB
testcase_41 AC 93 ms
35,876 KB
testcase_42 AC 126 ms
50,212 KB
testcase_43 AC 133 ms
55,972 KB
testcase_44 AC 98 ms
37,796 KB
testcase_45 AC 100 ms
44,324 KB
testcase_46 AC 127 ms
49,572 KB
testcase_47 AC 194 ms
64,160 KB
testcase_48 AC 254 ms
95,928 KB
testcase_49 AC 266 ms
87,960 KB
testcase_50 AC 477 ms
128,952 KB
testcase_51 AC 102 ms
39,076 KB
testcase_52 AC 649 ms
170,820 KB
testcase_53 AC 236 ms
80,152 KB
testcase_54 AC 107 ms
41,892 KB
testcase_55 AC 451 ms
138,784 KB
testcase_56 AC 97 ms
38,308 KB
testcase_57 AC 418 ms
111,128 KB
testcase_58 AC 248 ms
96,376 KB
testcase_59 AC 110 ms
42,276 KB
testcase_60 AC 109 ms
46,884 KB
testcase_61 AC 167 ms
62,940 KB
testcase_62 AC 289 ms
98,492 KB
testcase_63 AC 178 ms
66,068 KB
testcase_64 AC 1,542 ms
232,232 KB
testcase_65 AC 122 ms
41,508 KB
testcase_66 AC 1,186 ms
452,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (99 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, s, t, k) = (c[0], c[1], c[2], c[3]);
        var x = NList;
        var m = NN;
        var map = NArr(m);
        var tree = new List<(int to, long len)>[n * k];
        var revtree = new List<(int to, long len)>[n * k];
        for (var i = 0; i < tree.Length; ++i)
        {
            tree[i] = new List<(int to, long len)>();
            revtree[i] = new List<(int to, long len)>();
        }
        foreach (var edge in map)
        {
            var a = edge[0] - 1;
            var b = edge[1] - 1;
            for (var i = 0; i + 1 < k; ++i)
            {
                tree[i * n + a].Add(((i + 1) * n + b, x[b] + edge[2]));
                revtree[(i + 1) * n + b].Add((i * n + a, x[b] + edge[2]));
            }
            tree[(k - 1) * n + a].Add(((k - 1) * n + b, x[b] + edge[2]));
            revtree[(k - 1) * n + b].Add(((k - 1) * n + a, x[b] + edge[2]));
        }
        var INF = long.MaxValue / 2;
        var len = Enumerable.Repeat(INF, n * k).ToArray();
        len[s - 1] = x[s - 1];
        var q = new PriorityQueue<Pair>(n, false);
        q.Enqueue(new Pair(s - 1, x[s - 1]));
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            if (len[cur.Pos] != cur.Val) continue;
            foreach (var next in tree[cur.Pos])
            {
                if (len[next.to] <= cur.Val + next.len) continue;
                len[next.to] = cur.Val + next.len;
                q.Enqueue(new Pair(next.to, len[next.to]));
            }
        }
        if (len[(k - 1) * n + t - 1] == INF)
        {
            WriteLine("Impossible");
        }
        else
        {
            WriteLine("Possible");
            var cur = (k - 1) * n + t - 1;
            var ans = new List<int>();
            while (cur != s - 1)
            {
                ans.Add(cur % n + 1);
                foreach (var next in revtree[cur])
                {
                    if (len[next.to] == len[cur] - next.len)
                    {
                        cur = next.to;
                        break;
                    }
                }
            }
            ans.Add(s);
            ans.Reverse();
            WriteLine(len[(k - 1) * n + t - 1]);
            WriteLine(ans.Count);
            WriteLine(string.Join(" ", ans));
        }
    }
    class Pair : IComparable<Pair>
    {
        public int Pos;
        public long Val;
        public Pair(int pos, long val)
        {
            Pos = pos; Val = val;
        }
        public int CompareTo(Pair b)
        {
            return Val.CompareTo(b.Val);
        }
    }
    class PriorityQueue<T> where T : IComparable<T>
    {
        public T[] List;
        public int Count;
        bool IsTopMax;
        public PriorityQueue(int count, bool isTopMax)
        {
            IsTopMax = isTopMax;
            List = new T[Math.Max(128, count)];
        }
        public void Enqueue(T value)
        {
            if (Count == List.Length)
            {
                var newlist = new T[List.Length * 2];
                for (var i = 0; i < List.Length; ++i) newlist[i] = List[i];
                List = newlist;
            }
            var pos = Count;
            List[pos] = value;
            ++Count;
            while (pos > 0)
            {
                var parent = (pos - 1) / 2;
                if (Calc(List[parent], List[pos], true)) break;
                Swap(parent, pos);
                pos = parent;
            }
        }
        public T Dequeue()
        {
            --Count;
            Swap(0, Count);
            var pos = 0;
            while (true)
            {
                var child = pos * 2 + 1;
                if (child >= Count) break;
                if (child + 1 < Count && Calc(List[child + 1], List[child], false)) ++child;
                if (Calc(List[pos], List[child], true)) break;
                Swap(pos, child);
                pos = child;
            }
            return List[Count];
        }
        bool Calc(T a, T b, bool equalVal)
        {
            var ret = a.CompareTo(b);
            if (ret == 0 && equalVal) return true;
            return IsTopMax ? ret > 0 : ret < 0;
        }
        void Swap(int a, int b)
        {
            var tmp = List[a];
            List[a] = List[b];
            List[b] = tmp;
        }
    }
}
0