結果

問題 No.2805 Go to School
ユーザー tobisatistobisatis
提出日時 2024-07-12 22:25:06
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 4,947 bytes
コンパイル時間 14,133 ms
コンパイル使用メモリ 172,076 KB
実行使用メモリ 236,008 KB
最終ジャッジ日時 2024-07-16 01:40:53
合計ジャッジ時間 23,537 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
30,848 KB
testcase_01 AC 64 ms
30,848 KB
testcase_02 AC 68 ms
31,360 KB
testcase_03 AC 66 ms
31,104 KB
testcase_04 AC 255 ms
76,928 KB
testcase_05 AC 306 ms
72,648 KB
testcase_06 AC 227 ms
61,548 KB
testcase_07 AC 195 ms
62,976 KB
testcase_08 AC 238 ms
69,888 KB
testcase_09 AC 238 ms
61,440 KB
testcase_10 AC 187 ms
64,384 KB
testcase_11 AC 730 ms
79,744 KB
testcase_12 AC 433 ms
74,484 KB
testcase_13 AC 588 ms
76,432 KB
testcase_14 AC 150 ms
48,640 KB
testcase_15 AC 64 ms
30,976 KB
testcase_16 AC 66 ms
30,976 KB
testcase_17 AC 65 ms
30,976 KB
testcase_18 AC 290 ms
66,688 KB
testcase_19 AC 245 ms
57,980 KB
testcase_20 AC 412 ms
73,216 KB
testcase_21 AC 664 ms
78,848 KB
testcase_22 AC 309 ms
62,820 KB
testcase_23 AC 427 ms
68,608 KB
testcase_24 AC 401 ms
68,224 KB
testcase_25 AC 229 ms
55,396 KB
testcase_26 AC 657 ms
82,816 KB
testcase_27 AC 223 ms
71,800 KB
testcase_28 AC 89 ms
45,184 KB
testcase_29 AC 108 ms
49,920 KB
testcase_30 AC 144 ms
61,824 KB
testcase_31 AC 223 ms
62,208 KB
testcase_32 AC 621 ms
91,264 KB
testcase_33 AC 673 ms
81,396 KB
testcase_34 AC 65 ms
31,232 KB
testcase_35 AC 65 ms
31,360 KB
testcase_36 AC 270 ms
56,956 KB
testcase_37 AC 277 ms
62,324 KB
testcase_38 AC 468 ms
236,008 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (115 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

namespace AtCoder;

#nullable enable

using System.Numerics;

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();

    public static (T[], (int v, int e)[]) Distances<T>(this GraphBase<T> graph, int start, T infinity) where T :
        IComparable<T>,
        IAdditiveIdentity<T, T>,
        IAdditionOperators<T, T, T>,
        IMultiplicativeIdentity<T, T>,
        IMultiplyOperators<T, T, T>
    {
        var Graph = graph.Graph;
        var n = Graph.N;
        var res = new T[n];
        var prevs = new (int, int)[n];
        for (var i = 0; i < n; i++) (res[i], prevs[i]) = (infinity, (-1, -1));
        var determined = new bool[n];
        var q = new PriorityQueue<int, T>();
        res[start] = T.AdditiveIdentity;
        q.Enqueue(start, T.AdditiveIdentity);
        while (q.Count > 0)
        {
            var v = q.Dequeue();
            if (determined[v]) continue;
            determined[v] = true;
            foreach (var (next, edge) in Graph.AdjacencyList[v])
            {
                var d = res[v] + Graph.Edges[edge].Distance;
                if (res[next].CompareTo(d) <= 0) continue;
                res[next] = d;
                prevs[next] = (v, edge);
                q.Enqueue(next, d);
            }
        }
        return (res, prevs);
    }
}

class Graph<T>
{
    public struct Edge
    {
        public int Ab { get; set; }
        public int Ad { get; set; }
        public T Distance { get; set; }
    }

    public int N { get; private init; }
    public bool Directed { get; private init; }
    public List<List<(int next, int edgeIndex)>> AdjacencyList { get; private init; }
    public List<Edge> Edges { get; private init; }

    public Graph(int verticals, IReadOnlyList<Edge> edges, bool directed)
    {
        N = verticals;
        Directed = directed;
        AdjacencyList = new List<List<(int, int)>>();
        for (var i = 0; i < N; i++) AdjacencyList.Add(new List<(int, int)>());
        Edges = new List<Edge>();
        for (var i = 0; i < edges.Count; i++)
        {
            var edge = edges[i];
            Edges.Add(edge);
            AdjacencyList[edge.Ab].Add((edge.Ad, i));
            if (!Directed) AdjacencyList[edge.Ad].Add((edge.Ab, i));
        }
    }
}

class GraphBuilder
{
    public static Graph<int> SimpleGraph(int verticals, (int ab, int ad)[] edges, bool directed)
    {
        var e = edges.Select(e => new Graph<int>.Edge { Ab = e.ab, Ad = e.ad, Distance = 1 }).ToList();
        return new(verticals, e, directed);
    }

    public static Graph<T> Graph<T>(int verticals, (int ab, int ad, T edge)[] edges, bool directed)
    {
        var e = edges.Select(e => new Graph<T>.Edge { Ab = e.ab, Ad = e.ad, Distance = e.edge }).ToList();
        return new(verticals, e, directed);
    }
}

class GraphBase<T> { public required Graph<T> Graph { get; init; } }

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var m = Int();
        var l = Int();
        var s = Int();
        var e = Int();
        var edges = m.Repeat(() => (Int() - 1, Int() - 1, Input<long>()));
        var tz = l.Repeat(() => Int() - 1);
        
        var g = GraphBuilder.Graph(n, edges, false);
        var gb = new GraphBase<long>(){ Graph = g };
        var inf = long.MaxValue / 4;
        var dz1 = gb.Distances(0, inf).Item1;
        var dzn = gb.Distances(n - 1, inf).Item1;
        var ans = inf;
        foreach (var t in tz)
        {
            var d1 = dz1[t];
            var dn = dzn[t];
            if (d1 >= s + e) continue;
            if (d1 < s) d1 = s;
            var d = d1 + 1 + dn;
            ans = Math.Min(ans, d);
        }
        if (ans == inf) ans = -1;
        return ans;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Trim().Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
}
0