結果

問題 No.971 いたずらっ子
ユーザー keymoonkeymoon
提出日時 2020-01-17 21:38:57
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 4,680 bytes
コンパイル時間 2,970 ms
コンパイル使用メモリ 118,020 KB
実行使用メモリ 60,048 KB
最終ジャッジ日時 2024-06-25 19:04:40
合計ジャッジ時間 6,156 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 43 ms
19,200 KB
testcase_12 AC 41 ms
19,412 KB
testcase_13 AC 42 ms
19,456 KB
testcase_14 WA -
testcase_15 AC 44 ms
19,584 KB
testcase_16 AC 42 ms
19,456 KB
testcase_17 AC 41 ms
19,276 KB
testcase_18 AC 42 ms
19,304 KB
testcase_19 AC 42 ms
19,200 KB
testcase_20 AC 43 ms
19,072 KB
testcase_21 AC 41 ms
19,328 KB
testcase_22 AC 41 ms
19,464 KB
testcase_23 AC 41 ms
19,200 KB
testcase_24 AC 42 ms
19,200 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;

public static class P
{
    public static void Main()
    {
        var hw = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var h = hw[0];
        var w = hw[1];
        var map = Enumerable.Repeat(0, h).Select(_ => Console.ReadLine()).ToArray().SelectMany(x => x).ToArray();

        //skip large case
        if (10000 <= h * w) throw new Exception();

        int[] dists = new int[map.Length];

        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
            { var id = i * w + j; dists[id] = i + j; }

        List<int>[] graph = Enumerable.Repeat(0, h * w).Select(_ => new List<int>()).ToArray();
        for (int i = 0; i < h - 1; i++)
            for (int j = 0; j < w; j++)
            { var id = i * w + j; graph[id].Add(id + w); graph[id + w].Add(id); }
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w - 1; j++)
            { var id = i * w + j; graph[id].Add(id + 1); graph[id + 1].Add(id); }

        var shortest = Enumerable.Repeat(1L << 60, graph.Length).ToArray();
        PriorityQueue<Tuple<int, int>, int> pqueue = new PriorityQueue<Tuple<int, int>, int>(x => x.Item2);
        pqueue.Push(new Tuple<int, int>(0, 0));
        while (0 < pqueue.Count)
        {
            var item = pqueue.Pop();
            if (shortest[item.Item1] != 1L << 60) continue;
            shortest[item.Item1] = item.Item2;
            foreach (var elem in graph[item.Item1])
            {
                if (shortest[elem] != 1L << 60) continue;
                pqueue.Push(new Tuple<int, int>(elem, item.Item2 + 1 + (map[elem] == 'k' ? dists[elem] : 0)));
            }
        }
        Console.WriteLine(shortest.Last());
    }
}


class PriorityQueue<TValue, TKey> where TKey : IComparable<TKey>
{
    public int Count { get; private set; }
    private Func<TValue, TKey> KeySelector;
    private bool Descendance;
    private TValue[] data = new TValue[65536];
    private TKey[] keys = new TKey[65536];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public PriorityQueue(Func<TValue, TKey> keySelector, bool descendance = false) { KeySelector = keySelector; Descendance = descendance; }
    public TValue Top
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { ValidateNonEmpty(); return data[1]; }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public TValue Pop()
    {
        var top = Top;
        var item = data[Count];
        var key = keys[Count--];
        int index = 1;
        while (true)
        {
            if ((index << 1) >= Count)
            {
                if (index << 1 > Count) break;
                if (key.CompareTo(keys[index << 1]) > 0 ^ Descendance)
                { data[index] = data[index << 1]; keys[index] = keys[index << 1]; index <<= 1; }
                else break;
            }
            else
            {
                var nextIndex = keys[index << 1].CompareTo(keys[(index << 1) + 1]) <= 0 ^ Descendance ? (index << 1) : (index << 1) + 1;
                if (key.CompareTo(keys[nextIndex]) > 0 ^ Descendance)
                { data[index] = data[nextIndex]; keys[index] = keys[nextIndex]; index = nextIndex; }
                else break;
            }
        }
        data[index] = item;
        keys[index] = key;
        return top;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Push(TValue item)
    {
        var key = KeySelector(item);
        int index = ++Count;
        if (data.Length == Count) Extend(data.Length * 2);
        while ((index >> 1) != 0)
        {
            if (keys[index >> 1].CompareTo(key) > 0 ^ Descendance)
            { data[index] = data[index >> 1]; keys[index] = keys[index >> 1]; index >>= 1; }
            else break;
        }
        data[index] = item;
        keys[index] = key;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private void Extend(int newSize)
    {
        TValue[] newData = new TValue[newSize];
        TKey[] newKeys = new TKey[newSize];
        data.CopyTo(newData, 0);
        keys.CopyTo(newKeys, 0);
        data = newData;
        keys = newKeys;
    }
    private void ValidateNonEmpty() { if (Count == 0) throw new IndexOutOfRangeException(); }
}
0