結果

問題 No.17 2つの地点に泊まりたい
ユーザー te-shte-sh
提出日時 2017-01-16 14:38:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,126 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 88,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 00:37:50
合計ジャッジ時間 1,977 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto si = n.iota.map!(_ => readln.chomp.to!int).array;
  auto m = readln.chomp.to!size_t;
  auto cij = new int[][](n, n);
  foreach (_; m.iota) {
    auto rd = readln.split, a = rd[0].to!size_t, b = rd[1].to!size_t, c = rd[2].to!int;
    cij[a][b] = c;
    cij[b][a] = c;
  }

  auto dij = cij.warshalFloyd;

  auto r = int.max;
  foreach (i; 1..n-1)
    foreach (j; 1..n-1)
      if (i != j) {
        auto c1 = dij[0][i];
        auto c2 = dij[i][j];
        auto c3 = dij[j][n-1];
        if (c1 && c2 && c3)
          r = min(r, c1 + c2 + c3 + si[i] + si[j]);
      }

  writeln(r);
}

T[][] warshalFloyd(T)(T[][] a)
{
  auto n = a.length;

  auto b = new T[][](n);
  foreach (i; 0..n) b[i] = a[i].dup;

  foreach (k; 0..n)
    foreach (i; 0..n)
      foreach (j; 0..n) 
        if ((i == k || b[i][k]) && (k == j || b[k][j])) {
          auto s = b[i][k] + b[k][j];
          if (i == j || b[i][j])
            b[i][j] = min(b[i][j], s);
          else
            b[i][j] = s;
        }

  return b;
}
0