結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー mkawa2mkawa2
提出日時 2022-11-01 18:32:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,023 ms / 3,000 ms
コード長 3,240 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 81,776 KB
実行使用メモリ 137,964 KB
最終ジャッジ日時 2024-07-16 08:56:11
合計ジャッジ時間 22,868 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,376 KB
testcase_01 AC 36 ms
55,372 KB
testcase_02 AC 185 ms
79,720 KB
testcase_03 AC 249 ms
78,848 KB
testcase_04 AC 187 ms
79,392 KB
testcase_05 AC 127 ms
78,084 KB
testcase_06 AC 250 ms
78,888 KB
testcase_07 AC 172 ms
78,972 KB
testcase_08 AC 227 ms
78,960 KB
testcase_09 AC 211 ms
80,728 KB
testcase_10 AC 274 ms
79,904 KB
testcase_11 AC 268 ms
80,208 KB
testcase_12 AC 1,146 ms
127,936 KB
testcase_13 AC 368 ms
101,376 KB
testcase_14 AC 818 ms
115,700 KB
testcase_15 AC 662 ms
106,672 KB
testcase_16 AC 1,042 ms
115,756 KB
testcase_17 AC 1,872 ms
135,648 KB
testcase_18 AC 1,825 ms
136,484 KB
testcase_19 AC 1,333 ms
126,336 KB
testcase_20 AC 1,898 ms
135,864 KB
testcase_21 AC 2,023 ms
137,964 KB
testcase_22 AC 417 ms
115,792 KB
testcase_23 AC 1,373 ms
137,316 KB
testcase_24 AC 312 ms
102,792 KB
testcase_25 AC 1,206 ms
134,316 KB
testcase_26 AC 463 ms
113,104 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