結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-03-26 23:01:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 796 ms / 3,000 ms
コード長 4,425 bytes
コンパイル時間 1,225 ms
コンパイル使用メモリ 139,376 KB
実行使用メモリ 41,204 KB
最終ジャッジ日時 2024-06-22 11:07:23
合計ジャッジ時間 12,677 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 11 ms
6,940 KB
testcase_03 AC 115 ms
6,944 KB
testcase_04 AC 10 ms
6,944 KB
testcase_05 AC 7 ms
6,940 KB
testcase_06 AC 114 ms
6,948 KB
testcase_07 AC 7 ms
6,940 KB
testcase_08 AC 104 ms
6,940 KB
testcase_09 AC 16 ms
6,940 KB
testcase_10 AC 123 ms
6,940 KB
testcase_11 AC 116 ms
11,360 KB
testcase_12 AC 462 ms
30,120 KB
testcase_13 AC 245 ms
22,160 KB
testcase_14 AC 365 ms
28,236 KB
testcase_15 AC 354 ms
24,812 KB
testcase_16 AC 495 ms
25,944 KB
testcase_17 AC 770 ms
31,044 KB
testcase_18 AC 794 ms
29,588 KB
testcase_19 AC 534 ms
30,616 KB
testcase_20 AC 780 ms
31,068 KB
testcase_21 AC 796 ms
30,844 KB
testcase_22 AC 276 ms
35,196 KB
testcase_23 AC 511 ms
41,204 KB
testcase_24 AC 233 ms
23,936 KB
testcase_25 AC 495 ms
29,056 KB
testcase_26 AC 274 ms
23,988 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