結果

問題 No.20 砂漠のオアシス
ユーザー te-shte-sh
提出日時 2020-01-22 15:29:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 88 ms / 5,000 ms
コード長 7,045 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 163,376 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2023-09-04 04:55:46
合計ジャッジ時間 3,389 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 74 ms
12,108 KB
testcase_06 AC 88 ms
12,336 KB
testcase_07 AC 86 ms
11,856 KB
testcase_08 AC 87 ms
12,416 KB
testcase_09 AC 87 ms
12,392 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 18 ms
4,504 KB
testcase_17 AC 13 ms
4,380 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 18 ms
4,480 KB
testcase_20 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/20

import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int N, V, Ox, Oy; io.getV(N, V, Ox, Oy); --Ox; --Oy;
  int[][] L; io.getM(N, N, L);

  auto a = grid(L);
  alias P = a.P;

  auto g = GraphW!int(N*N);
  foreach (y; 0..N)
    foreach (x; 0..N) {
      auto p = P(x, y);
      foreach (np; a.around4(p))
	g.addEdge(cast(int)a.p2i(p), cast(int)a.p2i(np), a[np]);
    }

  auto d1 = g.dijkstra(cast(int)a.p2i(P(0, 0))).dist;
  if (V - d1[a.p2i(P(N-1, N-1))] > 0) {
    io.put("YES");
    return;
  }

  if (Ox >= 0 && Oy >= 0) {
    auto d2 = g.dijkstra(cast(int)a.p2i(P(Ox, Oy))).dist;
    if ((V - d1[a.p2i(P(Ox, Oy))])*2 - d2[a.p2i(P(N-1, N-1))] > 0) {
      io.put("YES");
      return;
    }
  }

  io.put("NO");
}

struct Point2(T)
{
  alias P = Point2!T, Op = string;
  T x, y;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y; }
  pure T hypot2() { return x^^2+y^^2; }
}

pure T cross(T)(Point2!T p1, Point2!T p2)
{ return p1.x*p2.y - p1.y*p2.x; }

struct Point3(T)
{
  alias P = Point3!T, Op = string;
  T x, y, z;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y, z"~o~"r.z)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y; z"~o~"=r.z;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a, z"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a; z"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y+z*r.z; }
  pure T hypot2() { return x^^2+y^^2+z^^2; }
}

pure Point3!T cross(T)(Point3!T p1, Point3!T p2)
{ return Point3!T(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x); }

struct Grid(T)
{
  alias G = Grid!T, P = Point2!int, Point = P;
  size_t c, r;
  T[][] data;

  this(size_t c, size_t r) { this.c = c; this.r = r; data = new T[][](r, c); }
  this(T[][] data) { c = data[0].length; r = data.length; this.data = data; }

  pure P i2p(size_t i) { return P(cast(int)(i%c), cast(int)(i/c)); }
  pure size_t p2i(P p) { return p.x*c + p.y; }

  pure T opIndex(size_t x, size_t y) { return data[y][x]; }
  pure T opIndex(P p) { return data[p.y][p.x]; }
  G opIndexAssign(T v, size_t x, size_t y) { data[y][x] = v; return this; }
  G opIndexAssign(T v, P p) { data[p.y][p.x] = v; return this; }
  G opIndexOpAssign(string op)(T v, size_t x, size_t y)
  { mixin("data[y][x]"~op~"=v;"); return this; }
  G opIndexOpAssign(string op)(T v, P p)
  { mixin("data[p.y][p.x]"~op~"=v;"); return this; }
  G opIndexUnary(string op)(size_t x, size_t y) if (op=="++"||op=="--")
  { mixin(op~"data[y][x];"); return this; }
  G opIndexUnary(string op)(P p) if (op=="++"||op=="--")
  { mixin(op~"data[p.y][p.x];"); return this; }

  pure bool valid(size_t x, size_t y) { return 0 <= x && x < c && 0 <= y && y < r; }
  pure bool valid(P p) { return valid(p.x, p.y); }
  auto d4 = [P(-1, 0), P(0, -1), P(1, 0), P(0, 1)];
  pure auto around4(P p) { return d4.map!(d => d+p).filter!(np => valid(np)); }
  auto d8 = [P(-1, 0), P(-1, -1), P(0, -1), P(1, -1), P(1, 0), P(1, 1), P(0, 1), P(-1, 1)];
  pure auto around8(P p) { return d8.map!(d => d+p).filter!(np => valid(np)); }
}
Grid!T grid(T)(T[][] data) { return Grid!T(data); }

struct Graph
{
  alias Node = int;
  Node n;
  Node[][] g;
  alias g this;
  this(Node n) { this.n = n; g = new Node[][](n); }
  void addEdge(Node u, Node v) { g[u] ~= v; }
  void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; }
}

struct GraphW(W = int, W i = 10^^9)
{
  alias Node = int, Wt = W, inf = i;
  struct Edge { Node src, dst; Wt wt; alias cap = wt; }
  Node n;
  Edge[][] g;
  alias g this;
  this(Node n) { this.n = n; g = new Edge[][](n); }
  void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); }
  void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); }
}

struct GraphM(W = int, W i = 10^^9)
{
  alias Node = int, Wt = W, inf = i;
  Node n;
  Wt[][] g;
  alias g this;
  this(int n) { this.n = n; g = new Wt[][](n, n); }
  static GraphM!(W, i) init(Node n)
  {
    auto g = GraphM!(W, i)(n);
    foreach (i; 0..n) { g[i][] = inf; g[i][i] = 0; }
    return g;
  }
}

struct Dijkstra(Graph)
{
  alias Node = Graph.Node, Wt = Graph.Wt, Edge = Graph.Edge;
  Graph g;
  alias g this;
  Wt[] dist;
  Node[] prev;

  this(Graph g, Node s)
  {
    this.g = g;
    auto sent = n;

    dist = new Wt[](n);
    dist[] = g.inf;
    dist[s] = 0;

    prev = new Node[](n);
    prev[] = sent;

    auto q = heapify!("a.wt>b.wt")(Array!Edge(Edge(sent, s, 0)));
    while (!q.empty) {
      auto e = q.front; q.removeFront();
      if (prev[e.dst] != sent) continue;
      prev[e.dst] = e.src;
      foreach (f; g[e.dst]) {
        auto w = e.wt+f.wt;
        if (dist[f.dst] > w) {
          dist[f.dst] = w;
          q.insert(Edge(f.src, f.dst, w));
        }
      }
    }
  }
}
Dijkstra!Graph dijkstra(Graph, Node)(Graph g, Node s)
{ return Dijkstra!Graph(g, s); }

auto io = IO!()();
import std.stdio;
struct IO(string floatFormat = "%.10f", string delimiter = " ", alias IN = stdin, alias OUT = stdout)
{
  import std.conv, std.format, std.meta, std.traits;
  alias assignable = hasAssignableElements;

  auto getV(T...)(ref T v) { foreach (ref w; v) get(w); }
  auto getA(T)(size_t n, ref T v) if (assignable!T) { v = new T(n); foreach (ref w; v) get(w); }
  auto getC(T...)(size_t n, ref T v) if (allSatisfy!(assignable, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  auto getM(T)(size_t r, size_t c, ref T v) if (assignable!T && assignable!(ElementType!T))
  {
    v = new T(r); foreach (ref w; v) getA(c, w);
  }

  auto put(bool flush = false, T...)(T v)
  {
    foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); }
    OUT.writeln;
    static if (flush) OUT.flush();
  }
  auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); }
  auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; }

  private
  {
    dchar[] buf;
    auto sp = (new dchar[](0)).splitter;
    void nextLine() { IN.readln(buf); sp = buf.splitter; }
    auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); }

    auto putR(T)(T v)
    {
      auto w = v;
      while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); }
    }
    auto putA(T)(T v)
    {
      static if (isInputRange!T && !isSomeString!T) putR(v);
      else if (isFloatingPoint!T) OUT.write(format(floatFormat, v));
      else OUT.write(v);
    }
  }
}
0