結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー mkawa2mkawa2
提出日時 2022-11-01 18:32:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,435 ms / 3,000 ms
コード長 3,240 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 140,700 KB
最終ジャッジ日時 2023-09-23 09:12:03
合計ジャッジ時間 31,399 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,256 KB
testcase_01 AC 76 ms
71,528 KB
testcase_02 AC 264 ms
81,740 KB
testcase_03 AC 312 ms
80,808 KB
testcase_04 AC 241 ms
81,176 KB
testcase_05 AC 177 ms
80,116 KB
testcase_06 AC 316 ms
80,876 KB
testcase_07 AC 228 ms
80,712 KB
testcase_08 AC 282 ms
80,324 KB
testcase_09 AC 277 ms
82,504 KB
testcase_10 AC 334 ms
81,252 KB
testcase_11 AC 320 ms
81,588 KB
testcase_12 AC 1,475 ms
128,032 KB
testcase_13 AC 526 ms
102,356 KB
testcase_14 AC 1,027 ms
119,256 KB
testcase_15 AC 832 ms
108,632 KB
testcase_16 AC 1,278 ms
117,336 KB
testcase_17 AC 2,365 ms
136,884 KB
testcase_18 AC 2,270 ms
138,184 KB
testcase_19 AC 1,723 ms
127,760 KB
testcase_20 AC 2,435 ms
137,352 KB
testcase_21 AC 2,394 ms
140,012 KB
testcase_22 AC 532 ms
116,940 KB
testcase_23 AC 1,615 ms
140,700 KB
testcase_24 AC 401 ms
103,856 KB
testcase_25 AC 1,464 ms
137,420 KB
testcase_26 AC 601 ms
111,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

class LCA:
    def __init__(self, parents, depth):
        self.n = len(parents)
        self.parents = parents
        self.depth = depth
        self.max_level = max(self.depth).bit_length()
        self.ancestor = [self.parents]+[[-1]*self.n for _ in range(self.max_level)]
        row0 = self.ancestor[0]
        for lv in range(self.max_level):
            row1 = self.ancestor[lv+1]
            for u in range(self.n):
                if row0[u] == -1: continue
                row1[u] = row0[row0[u]]
            row0 = row1

    def anc(self, u, v):
        diff = self.depth[u]-self.depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        lv = 0
        while diff:
            if diff & 1: u = self.ancestor[lv][u]
            lv, diff = lv+1, diff >> 1
        if u == v: return u
        for lv in range(self.depth[u].bit_length()-1, -1, -1):
            anclv = self.ancestor[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self.parents[u]

n, k = LI()
to = [[] for _ in range(n+k)]
for _ in range(n-1):
    u, v, c = LI()
    u, v = u-1, v-1
    to[u].append((v, c))
    to[v].append((u, c))

parent, depth, vis, dist = [-1]*n, [0]*n, [0]*n, [0]*n

def dfs(root):
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        while i < len(to[u]) and vis[to[u][i][0]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            v, c = to[u][i]
            vis[v] = 1
            stack.append(v)
            dist[v] = dist[u]+c
            parent[v] = u
            depth[v] = depth[u]+1

dfs(0)
lca = LCA(parent, depth)

pp = []
for s in range(k):
    s += n
    m, p = LI()
    pp.append(p)
    for u in LI1():
        to[u].append((s, p))
        to[s].append((u, 0))
# pDB(pp)

from heapq import *

def dijkstra(to, root=0):
    n = len(to)
    dist = [inf]*n
    dist[root] = 0
    hp = [(0, root)]
    while hp:
        d, u = heappop(hp)
        if d > dist[u]: continue
        for v, c in to[u]:
            nd = d+c
            if dist[v] <= nd: continue
            dist[v] = nd
            heappush(hp, (nd, v))

    return dist

dd = []
for s in range(n, n+k):
    dd.append(dijkstra(to, s))
# p2D(dd)

for _ in range(II()):
    u, v = LI1()
    a = lca.anc(u, v)
    ans = dist[u]+dist[v]-dist[a]*2
    # pDB(u, v, a, ans)
    for s in range(k):
        cur = dd[s][u]+dd[s][v]+pp[s]
        # pDB(s, cur, ans)
        if cur < ans: ans = cur
    print(ans)
0