結果

問題 No.17 2つの地点に泊まりたい
ユーザー te-shte-sh
提出日時 2016-08-27 22:08:42
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 418 ms / 5,000 ms
コード長 1,472 bytes
コンパイル時間 769 ms
コンパイル使用メモリ 114,076 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 21:30:27
合計ジャッジ時間 4,184 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 59 ms
4,372 KB
testcase_04 AC 13 ms
4,368 KB
testcase_05 AC 153 ms
4,368 KB
testcase_06 AC 81 ms
4,368 KB
testcase_07 AC 50 ms
4,368 KB
testcase_08 AC 418 ms
4,368 KB
testcase_09 AC 289 ms
4,372 KB
testcase_10 AC 96 ms
4,372 KB
testcase_11 AC 149 ms
4,372 KB
testcase_12 AC 1 ms
4,372 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 1 ms
4,368 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 1 ms
4,372 KB
testcase_17 AC 5 ms
4,372 KB
testcase_18 AC 40 ms
4,372 KB
testcase_19 AC 22 ms
4,372 KB
testcase_20 AC 4 ms
4,372 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 48 ms
4,372 KB
testcase_23 AC 143 ms
4,372 KB
testcase_24 AC 148 ms
4,372 KB
testcase_25 AC 3 ms
4,372 KB
testcase_26 AC 271 ms
4,368 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 r1 = dijkstra(n, 0,  s1,    graph);
      auto r2 = dijkstra(n, s1, s2,    graph);
      auto r3 = dijkstra(n, s2, n - 1, graph);

      if (r1 > 0 && r2 > 0 && r3 > 0) {
        auto r = r1 + r2 + r3 + 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 -1;
}
0