結果

問題 No.1480 Many Complete Graphs
ユーザー gew1fw
提出日時 2025-06-12 12:53:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,357 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,284 KB
実行使用メモリ 105,748 KB
最終ジャッジ日時 2025-06-12 12:54:55
合計ジャッジ時間 11,797 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1

    adj = [[] for _ in range(N + M + 1)]  # Nodes are 1-based, virtual nodes are N+1 to N+M

    for i in range(M):
        k_i = int(input[ptr])
        ptr += 1
        c_i = int(input[ptr])
        ptr += 1
        s = list(map(int, input[ptr:ptr + k_i]))
        ptr += k_i
        virtual_node = N + 1 + i  # Virtual nodes are N+1, N+2, ..., N+M
        for x in s:
            # Edge from x to virtual_node: cost is (x +1 + 2*c_i)
            cost1 = (x + 1) + 2 * c_i
            adj[x].append((virtual_node, cost1))
            # Edge from virtual_node to x: cost is x
            cost2 = x
            adj[virtual_node].append((x, cost2))

    # Dijkstra's algorithm
    INF = float('inf')
    dist = [INF] * (N + M + 1)
    dist[1] = 0
    heap = []
    heapq.heappush(heap, (0, 1))

    while heap:
        current_dist, u = heapq.heappop(heap)
        if current_dist > dist[u]:
            continue
        for (v, cost) in adj[u]:
            if dist[v] > dist[u] + cost:
                dist[v] = dist[u] + cost
                heapq.heappush(heap, (dist[v], v))

    if dist[N] == INF:
        print(-1)
    else:
        print(dist[N] // 2)

if __name__ == '__main__':
    main()
0