結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー chineristACchineristAC
提出日時 2021-03-26 22:43:56
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,026 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 82,392 KB
実行使用メモリ 301,428 KB
最終ジャッジ日時 2024-05-06 16:37:02
合計ジャッジ時間 32,477 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
57,480 KB
testcase_01 AC 42 ms
57,168 KB
testcase_02 AC 269 ms
82,340 KB
testcase_03 AC 420 ms
90,372 KB
testcase_04 AC 289 ms
82,552 KB
testcase_05 AC 185 ms
81,876 KB
testcase_06 AC 407 ms
89,652 KB
testcase_07 AC 240 ms
81,916 KB
testcase_08 AC 339 ms
88,064 KB
testcase_09 AC 375 ms
85,652 KB
testcase_10 AC 488 ms
90,728 KB
testcase_11 AC 416 ms
90,380 KB
testcase_12 AC 1,732 ms
176,388 KB
testcase_13 AC 794 ms
156,184 KB
testcase_14 AC 1,336 ms
166,480 KB
testcase_15 AC 1,188 ms
163,180 KB
testcase_16 AC 1,819 ms
174,308 KB
testcase_17 AC 2,957 ms
194,452 KB
testcase_18 AC 2,885 ms
194,376 KB
testcase_19 AC 1,942 ms
180,448 KB
testcase_20 AC 2,857 ms
195,080 KB
testcase_21 TLE -
testcase_22 AC 857 ms
289,628 KB
testcase_23 AC 1,826 ms
301,428 KB
testcase_24 AC 561 ms
162,948 KB
testcase_25 AC 1,554 ms
194,656 KB
testcase_26 AC 614 ms
163,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Dijkstra():
    class Edge():
        def __init__(self, _to, _cost):
            self.to = _to
            self.cost = _cost

    def __init__(self, V):
        self.G = [[] for i in range(V)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        return self._E

    @property
    def V(self):
        return self._V

    def add_edge(self, _from, _to, _cost):
        self.G[_from].append(self.Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, start):
        import heapq
        que = []
        d = [10**15] * self.V
        if type(start)==int:
            s = start
            d[s] = 0
            heapq.heappush(que, (0, s))
        else:
            for s in start:
                d[s] = 0
                heapq.heappush(que,(0,s))

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    heapq.heappush(que, (d[e.to], e.to))
        return d

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd

sys.setrecursionlimit(2*10**5)

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N,K = mi()
edge = [[] for i in range(N)]
tree = Dijkstra(N)
for _ in range(N-1):
    a,b,c = mi()
    edge[a-1].append((b-1,c))
    edge[b-1].append((a-1,c))
    tree.add_edge(a-1,b-1,c)
    tree.add_edge(b-1,a-1,c)

air = []
air_city = []
for _ in range(K):
    m,p = mi()
    air.append((m,p))
    air_city.append([int(a)-1 for a in input().split()])

dist_from_air = [[10**17 for i in range(N)] for a in range(K)]
for a in range(K):
    dist_from_air[a] = tree.shortest_path(air_city[a])

airs = Dijkstra(K)
for i in range(K):
    for j in range(K):
        tmp = 10**17
        for _from in air_city[i]:
            tmp = min(dist_from_air[j][_from],tmp)
        tmp += air[j][1]
        airs.add_edge(i,j,tmp)

dist = [airs.shortest_path(i) for i in range(K)]


# N: 頂点数
# G[v]: 頂点vの子頂点 (親頂点は含まない)

parent = [-1 for i in range(N)]
deq = deque([0])
while deq:
    v = deq.popleft()
    for nv,c in edge[v]:
        if parent[nv]==-1 and nv!=0:
            parent[nv] = v
            deq.append(nv)
G = [[(nv,c) for nv,c in edge[v] if nv!=parent[v]] for v in range(N)]

# Euler Tour の構築
S = []
F = [0]*N
depth = [0]*N
depth_dist = [0]*N
def dfs(v, d, di):
    F[v] = len(S)
    depth[v] = d
    depth_dist[v] = di
    S.append(v)
    for nv,c in G[v]:
        dfs(nv, d+1, di+c)
        S.append(v)
dfs(0, 0, 0)

# 存在しない範囲は深さが他よりも大きくなるようにする
INF = (N, None)

# LCAを計算するクエリの前計算
M = 2*N
M0 = 2**(M-1).bit_length()
data = [INF]*(2*M0)
for i, v in enumerate(S):
    data[M0-1+i] = (depth[v], i)
for i in range(M0-2, -1, -1):
    data[i] = min(data[2*i+1], data[2*i+2])

# LCAの計算 (generatorで最小値を求める)
def _query(a, b):
    res = INF
    a += M0; b += M0
    while a < b:
        if b & 1:
            b -= 1
            res = min(res,data[b-1])
        if a & 1:
            res = min(res,data[a-1])
            a += 1
        a >>= 1; b >>= 1
    return res

# LCAの計算 (外から呼び出す関数)
def lca(u, v):
    fu = F[u]; fv = F[v]
    if fu > fv:
        fu, fv = fv, fu
    idx = _query(fu,fv+1)
    return S[idx[1]]

def dist_in_tree(u,v):
    w = lca(u,v)
    return depth_dist[u] + depth_dist[v] - 2 * depth_dist[w]


ans = []
for _ in range(int(input())):
    u,v = mi()
    u,v = u-1,v-1
    res = dist_in_tree(u,v)
    for i in range(K):
        for j in range(K):
            tmp = dist_from_air[i][u] + air[i][1] + dist[i][j] + dist_from_air[j][v]
            res = min(res,tmp)
    ans.append(res)

print(*ans,sep="\n")
0