結果

問題 No.1283 Extra Fee
ユーザー kakel-san
提出日時 2025-02-08 20:27:37
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 650 ms / 2,000 ms
コード長 2,025 bytes
コンパイル時間 12,998 ms
コンパイル使用メモリ 172,012 KB
実行使用メモリ 218,292 KB
最終ジャッジ日時 2025-02-08 20:28:05
合計ジャッジ時間 24,443 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (92 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.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) = (c[0], c[1]);
        var map = NArr(m);
        var cost = new int[n, n];
        for (var i = 0; i < m; ++i) cost[map[i][0] - 1, map[i][1] - 1] = map[i][2];

        var INF = long.MaxValue / 2;
        var len = new long[n, n, 2];
        for (var i = 0; i < n; ++i) for (var j = 0; j < n; ++j)
        {
            len[i, j, 0] = INF;
            len[i, j, 1] = INF;
        }
        len[0, 0, 0] = 0;
        var q = new PriorityQueue<(int x, int y, int d, long len), long>();
        q.Enqueue((0, 0, 0, 0), 0);
        var mx = new int[] { -1, 1, 0, 0 };
        var my = new int[] { 0, 0, -1, 1 };
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            if (len[cur.x, cur.y, cur.d] != cur.len) continue;
            for (var i = 0; i < 4; ++i)
            {
                var nx = cur.x + mx[i];
                var ny = cur.y + my[i];
                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
                if (len[nx, ny, cur.d] > cur.len + 1 + cost[nx, ny])
                {
                    len[nx, ny, cur.d] = cur.len + 1 + cost[nx, ny];
                    q.Enqueue((nx, ny, cur.d, len[nx, ny, cur.d]), len[nx, ny, cur.d]);
                }
                if (cur.d == 0 && len[nx, ny, 1] > cur.len + 1)
                {
                    len[nx, ny, 1] = cur.len + 1;
                    q.Enqueue((nx, ny, 1, len[nx, ny, 1]), len[nx, ny, 1]);
                }
            }
        }
        WriteLine(len[n - 1, n - 1, 1]);
    }
}
0