結果

問題 No.134 走れ!サブロー君
ユーザー te-shte-sh
提出日時 2017-01-31 16:33:06
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 2,003 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 157,912 KB
実行使用メモリ 6,592 KB
最終ジャッジ日時 2023-09-03 01:11:06
合計ジャッジ時間 3,412 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 10 ms
4,392 KB
testcase_08 AC 21 ms
5,404 KB
testcase_09 AC 20 ms
6,592 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions
import std.bitmanip;  // BitArray

void main()
{
  auto rd1 = readln.split.to!(int[]), s = point(rd1[0], rd1[1]);
  auto n = readln.chomp.to!size_t;
  auto pi = new point[](n), wi = new real[](n);
  foreach (i; n.iota) {
    auto rd2 = readln.split;
    pi[i] = point(rd2[0].to!int, rd2[1].to!int);
    wi[i] = rd2[2].to!real;
  }

  auto vi = new real[](1 << n);
  foreach (i; (1 << n).iota)
    vi[i] = wi.indexed(i.bitsSet).sum;

  auto dp = new real[][](1 << n, n);
  foreach (j; n.iota) {
    dp[0][j] = real(100) / 120 * manDis(pi[j], s) + wi[j];
  }

  foreach (i; ((1 << n) - 1).iota) {
    foreach (j; n.iota) {
      foreach (k; n.iota) {
        if (j == k || i.bitTest(j)) continue;
        auto ni = i.bitSet(j);
        auto nv = dp[i][j] + (vi[ni] + 100) / 120 * manDis(pi[k], pi[j]) + wi[k];
        if (dp[ni][k].isNaN)
          dp[ni][k] = nv;
        else
          dp[ni][k] = min(dp[ni][k], nv);
      }
    }
  }

  auto f = (1 << n) - 1;
  auto mi = real.max;
  foreach (i; n.iota) {
    auto ni = f.bitReset(i);
    auto nv = dp[ni][i] + (vi[f] + 100) / 120 * manDis(pi[i], s);
    mi = min(mi, nv);
  }

  writefln("%.8f", mi);
}

auto manDis(point p1, point p2)
{
  return (p1.x - p2.x).abs + (p1.y - p2.y).abs;
}

template BitOp(T) {
  bool bitTest(T n, size_t i) { return (n & (1.to!T << i)) != 0; }
  T bitSet(T n, size_t i) { return n | (1.to!T << i); }
  T bitReset(T n, size_t i) { return n & ~(1.to!T << i); }
}

mixin BitOp!int;

struct Point(T) {
  T x, y;

  auto opBinary(string op)(Point!T rhs) {
    static if (op == "+") return Point!T(x + rhs.x, y + rhs.y);
    else static if (op == "-") return Point!T(x - rhs.x, y - rhs.y);
    else static if (op == "*") return x * rhs.x + y * rhs.y;
  }

  auto opBinary(string op)(T a) {
    static if (op == "*") return Point!T(x * a, y * a);
  }

  T hypot2() { return x ^^ 2 + y ^^ 2; }
}

alias Point!int point;
0