結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-03-26 23:01:33
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,168 ms / 3,000 ms
コード長 4,425 bytes
コンパイル時間 1,875 ms
コンパイル使用メモリ 127,844 KB
実行使用メモリ 44,584 KB
最終ジャッジ日時 2023-09-04 12:27:13
合計ジャッジ時間 17,635 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 13 ms
4,384 KB
testcase_03 AC 136 ms
4,380 KB
testcase_04 AC 13 ms
4,384 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 134 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 120 ms
4,376 KB
testcase_09 AC 21 ms
4,728 KB
testcase_10 AC 144 ms
4,448 KB
testcase_11 AC 129 ms
6,208 KB
testcase_12 AC 614 ms
29,700 KB
testcase_13 AC 303 ms
21,976 KB
testcase_14 AC 463 ms
28,176 KB
testcase_15 AC 465 ms
24,220 KB
testcase_16 AC 681 ms
26,236 KB
testcase_17 AC 1,113 ms
31,396 KB
testcase_18 AC 1,090 ms
31,148 KB
testcase_19 AC 748 ms
28,852 KB
testcase_20 AC 1,168 ms
30,888 KB
testcase_21 AC 1,165 ms
30,912 KB
testcase_22 AC 383 ms
35,828 KB
testcase_23 AC 752 ms
44,584 KB
testcase_24 AC 292 ms
22,764 KB
testcase_25 AC 689 ms
33,068 KB
testcase_26 AC 330 ms
25,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum E = 17;
enum INF = 10L^^18;

int N, K;
int[] A, B;
long[] C;
int[] M;
long[] P;
int[][] X;

int[][] G;
int[][] par;
int[] dep;
long[] dist;
void dfs(int u, int p, int d, long c) {
  par[0][u] = p;
  dep[u] = d;
  dist[u] = c;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u, d + 1, c + C[i]);
    }
  }
}

int lca(int u, int v) {
  foreach_reverse (e; 0 .. E) {
    if (dep[u] - (1 << e) >= dep[v]) {
      u = par[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      v = par[e][v];
    }
  }
  foreach_reverse (e; 0 .. E) {
    if (par[e][u] != par[e][v]) {
      u = par[e][u];
      v = par[e][v];
    }
  }
  if (u != v) {
    u = par[0][u];
    v = par[0][v];
  }
  return u;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      C = new long[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
        C[i] = readLong();
      }
      M = new int[K];
      P = new long[K];
      X = new int[][K];
      foreach (k; 0 .. K) {
        M[k] = readInt();
        P[k] = readLong();
        X[k] = new int[M[k]];
        foreach (m; 0 .. M[k]) {
          X[k][m] = readInt() - 1;
        }
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      par = new int[][](E, N);
      dep = new int[N];
      dist = new long[N];
      dfs(0, -1, 0, 0);
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          par[e + 1][u] = (par[e][u] == -1) ? -1 : par[e][par[e][u]];
        }
      }
      
      auto dss = new long[][](K, N);
      foreach (k; 0 .. K) {
        alias Entry = Tuple!(long, "c", int, "u");
        BinaryHeap!(Array!Entry, "a.c > b.c") que;
        dss[k][] = INF;
        foreach (u; X[k]) {
          dss[k][u] = 0;
          que.insert(Entry(0, u));
        }
        for (; !que.empty; ) {
          const c = que.front.c;
          const u = que.front.u;
          que.removeFront;
          if (dss[k][u] == c) {
            foreach (i; G[u]) {
              const cc = c + C[i];
              const v = A[i] ^ B[i] ^ u;
              if (chmin(dss[k][v], cc)) {
                que.insert(Entry(cc, v));
              }
            }
          }
        }
      }
      auto dd = new long[][](K, K);
      foreach (k; 0 .. K) {
        foreach (l; 0 .. K) {
          dd[k][l] = INF;
          foreach (u; X[l]) {
            chmin(dd[k][l], dss[k][u]);
          }
        }
      }
      foreach (m; 0 .. K) foreach (k; 0 .. K) foreach (l; 0 .. K) {
        chmin(dd[k][l], dd[k][m] + P[m] + dd[m][l]);
      }
      
      const Q = readInt();
      foreach (q; 0 .. Q) {
        const u = readInt() - 1;
        const v = readInt() - 1;
        long ans = INF;
        // 0
        chmin(ans, dist[u] + dist[v] - 2 * dist[lca(u, v)]);
        // 1
        foreach (k; 0 .. K) {
          chmin(ans, dss[k][u] + P[k] + dss[k][v]);
        }
        // 2
        foreach (k; 0 .. K) foreach (l; 0 .. K) {
          chmin(ans, dss[k][u] + P[k] + dd[k][l] + P[l] + dss[l][v]);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0