結果

問題 No.340 雪の足跡
ユーザー eitahoeitaho
提出日時 2016-03-23 03:06:58
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 7,744 bytes
コンパイル時間 5,078 ms
コンパイル使用メモリ 115,760 KB
実行使用メモリ 172,372 KB
最終ジャッジ日時 2024-04-09 19:16:27
合計ジャッジ時間 25,657 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,680 KB
testcase_01 AC 31 ms
25,520 KB
testcase_02 AC 30 ms
27,204 KB
testcase_03 AC 31 ms
25,596 KB
testcase_04 AC 31 ms
25,268 KB
testcase_05 AC 30 ms
25,392 KB
testcase_06 AC 31 ms
25,612 KB
testcase_07 AC 31 ms
23,476 KB
testcase_08 AC 31 ms
25,552 KB
testcase_09 AC 32 ms
25,296 KB
testcase_10 AC 30 ms
25,680 KB
testcase_11 AC 36 ms
26,188 KB
testcase_12 AC 36 ms
23,860 KB
testcase_13 AC 40 ms
27,368 KB
testcase_14 AC 98 ms
38,468 KB
testcase_15 AC 112 ms
39,156 KB
testcase_16 AC 71 ms
32,660 KB
testcase_17 AC 125 ms
39,804 KB
testcase_18 AC 121 ms
41,584 KB
testcase_19 AC 123 ms
37,880 KB
testcase_20 AC 935 ms
112,216 KB
testcase_21 AC 512 ms
54,088 KB
testcase_22 AC 201 ms
84,156 KB
testcase_23 TLE -
testcase_24 AC 807 ms
106,580 KB
testcase_25 TLE -
testcase_26 AC 105 ms
42,496 KB
testcase_27 AC 46 ms
30,016 KB
testcase_28 AC 118 ms
43,424 KB
testcase_29 TLE -
testcase_30 AC 726 ms
91,044 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly string Impossible = "Odekakedekinai..";

    public void Solve()
    {
        int W = Reader.Int(), H = Reader.Int(), N = Reader.Int();
        var table = Reader.IntTable(N * 2);
        var dij = new Dijkstra(H * W);
        long[][] row = new long[H][];
        long[][] col = new long[W][];

        for (int r = 0; r < H; r++)
            row[r] = new long[(W + 63) / 64];
        for (int c = 0; c < W; c++)
            col[c] = new long[(H + 63) / 64];

        for (int ti = 1; ti < N * 2; ti += 2)
        {
            var t = table[ti];
            for (int i = 0; i + 1 < t.Length; i++)
            {
                int r = t[i] / W, c = t[i] % W;
                int nr = t[i + 1] / W, nc = t[i + 1] % W;
                if (Math.Sign(nr - r) != 0)
                    Paint(col[c], Math.Min(r, nr), Math.Max(r, nr));
                else
                    Paint(row[r], Math.Min(c, nc), Math.Max(c, nc));
            }
        }
        for (int r = 0; r < H; r++)
            for (int c = 0; c + 1 < W; c++)
                if ((row[r][c / 64] >> c % 64 & 1) == 1 && (row[r][(c + 1) / 64] >> (c + 1) % 64 & 1) == 1)
                {
                    dij.AddEdge(r * W + c, r * W + c + 1, 1);
                    dij.AddEdge(r * W + c + 1, r * W + c, 1);
                }
        for (int c = 0; c < W; c++)
            for (int r = 0; r + 1 < H; r++)
                if ((col[c][r / 64] >> r % 64 & 1) == 1 && (col[c][(r + 1) / 64] >> (r + 1) % 64 & 1) == 1)
                {
                    dij.AddEdge(r * W + c, (r + 1) * W + c, 1);
                    dij.AddEdge((r + 1) * W + c, r * W + c, 1);
                }

        int ans = dij.MinCost(0, H, W).Last();
        Console.WriteLine(ans == Dijkstra.INF ? Impossible : ans + "");
    }

    private void Paint(long[] A, int from, int to)
    {
        if ((from >> 6) == (to >> 6))
        {
            A[from >> 6] |= (1L << to - from + 1) - 1 << from;
            return;
        }
        int a = (from + 63) >> 6;
        int b = to >> 6;
        for (int i = from & 63; i < 64; i++)
            A[from >> 6] |= 1L << i;
        for (int i = a; i < b; i++)
            A[i] = -1L;
        A[to >> 6] |= (1L << (to + 1 & 63)) - 1;
    }

    class Dijkstra
    {
        public static readonly int INF = (int)1e9;
        private readonly List<Edge>[] G;

        public Dijkstra(int V)
        {
            G = new List<Edge>[V];
            for (int i = 0; i < G.Length; i++) G[i] = new List<Edge>();
        }
        public void AddEdge(int from, int to, int cost)
        {
            G[from].Add(new Edge(to, cost));
        }
        public int[] MinCost(int start, int H, int W)
        {
            var minCost = new int[G.Length];
            for (int i = 0; i < minCost.Length; i++)
                minCost[i] = INF;
            minCost[start] = 0;
            var heap = new Heap<Vertex>();
            heap.Push(new Vertex(start, 0, 0));

            while (heap.Count > 0)
            {
                var t = heap.Pop();
                int a = t.v;
                var cost = t.cost;
                if (cost > minCost[a]) continue;

                foreach (var e in G[a])
                {
                    int b = e.to;
                    var nextCost = cost + e.cost;
                    if (nextCost < minCost[b])
                    {
                        minCost[b] = nextCost;
                        int r = a / W, c = a % W;
                        int manhattanDist = H - 1 - r + W - 1 - c;
                        heap.Push(new Vertex(b, nextCost, nextCost + manhattanDist));
                    }
                }
            }
            return minCost;
        }
    }

    public struct Vertex : IComparable<Vertex>
    {
        public int v;
        public int cost;
        public int heuristic;
        public Vertex(int v, int cost, int h) { this.v = v; this.cost = cost; heuristic = h; }
        public int CompareTo(Vertex other) { return Math.Sign(heuristic - other.heuristic); }
    }
    public struct Edge
    {
        public int to;
        public int cost;
        public Edge(int to, int cost) { this.to = to; this.cost = cost; }
    }

    class Heap<T>
    {
        private const int InitialSize = 1024;
        private readonly int sign;
        private readonly Comparer<T> comp = Comparer<T>.Default;
        private T[] A = new T[InitialSize];
        public int Count { get; private set; }
        public Heap(bool greater = false) { sign = greater ? 1 : -1; }
        public T Peek() { return A[0]; }
        public bool Empty() { return Count == 0; }
        public void Push(T val)
        {
            if (Count == A.Length)
            {
                T[] next = new T[A.Length * 2];
                Array.Copy(A, next, A.Length);
                A = next;
            }
            A[Count++] = val;
            int i = Count - 1;
            while (i > 0)
            {
                int parent = (i - 1) / 2;
                if (Math.Sign(comp.Compare(val, A[parent])) == sign)
                {
                    A[i] = A[parent];
                    i = parent;
                }
                else break;
            }
            A[i] = val;
        }
        public T Pop()
        {
            T res = A[0];
            T val = A[--Count];
            if (Count == 0) return res;
            int i = 0;
            while (i * 2 + 1 < Count)
            {
                int L = i * 2 + 1;
                int R = i * 2 + 2;
                if (R < Count && Math.Sign(comp.Compare(A[R], A[L])) == sign) L = R;
                if (Math.Sign(comp.Compare(A[L], val)) == sign) { A[i] = A[L]; i = L; }
                else break;
            }
            A[i] = val;
            return res;
        }
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0