結果

問題 No.17 2つの地点に泊まりたい
ユーザー te-shte-sh
提出日時 2016-08-27 22:05:52
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,394 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 113,876 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 21:30:22
合計ジャッジ時間 4,934 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 58 ms
4,372 KB
testcase_04 AC 12 ms
4,368 KB
testcase_05 AC 149 ms
4,368 KB
testcase_06 AC 79 ms
4,368 KB
testcase_07 AC 49 ms
4,372 KB
testcase_08 AC 420 ms
4,368 KB
testcase_09 AC 290 ms
4,368 KB
testcase_10 AC 90 ms
4,372 KB
testcase_11 AC 144 ms
4,368 KB
testcase_12 AC 1 ms
4,372 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 139 ms
4,368 KB
testcase_24 AC 143 ms
4,368 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 270 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

alias Tuple!(int, "v", int, "c") side;

void main()
{
  auto n = readln.chomp.to!int;
  auto sti = iota(n).map!(i => readln.chomp.to!int).array;

  auto graph = new side[][n];

  auto m = readln.chomp.to!int;
  foreach (i; 0..m) {
    auto rd = readln.split.map!(to!int);
    graph[rd[0]] ~= side(rd[1], rd[2]);
    graph[rd[1]] ~= side(rd[0], rd[2]);
  }

  auto min = int.max;
  foreach (s1; 1..(n - 1)) {
    foreach (s2; 1..(n - 1)) {
      if (s1 == s2)
        continue;

      auto r =
        dijkstra(n, 0,  s1,    graph) +
        dijkstra(n, s1, s2,    graph) +
        dijkstra(n, s2, n - 1, graph) +
        sti[s1] + sti[s2];
      if (r < min)
        min = r;
    }
  }

  writeln(min);
}

int dijkstra(int n, int s, int e, side[][] graph)
{
  auto memo = new int[n];
  auto visited = new bool[n];
  visited[s] = true;

  auto pq = Array!side().heapify!("a.c > b.c");
  foreach (si; graph[s])
    pq.insert(si);

  while (!pq.empty) {
    auto si = pq.front;
    pq.removeFront;
    if (visited[si.v])
      continue;

    if (si.v == e)
      return si.c;

    memo[si.v] = si.c;
    visited[si.v] = true;

    foreach (c; graph[si.v]) {
      if (!visited[c.v]) {
        pq.insert(side(c.v, si.c + c.c));
      }
    }
  }

  return memo[e];
}
0