結果

問題 No.1480 Many Complete Graphs
ユーザー gew1fw
提出日時 2025-06-12 12:59:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,229 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 105,640 KB
最終ジャッジ日時 2025-06-12 13:05:45
合計ジャッジ時間 14,187 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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..N, virtual nodes are N+1..N+M

    for op in range(M):
        k = int(input[ptr])
        ptr += 1
        c = int(input[ptr])
        ptr += 1
        s = list(map(int, input[ptr:ptr + k]))
        ptr += k

        virtual_node = N + 1 + op

        floor_c = c // 2
        ceil_c = (c + 1) // 2

        for a in s:
            cost_to_virtual = (a + 1) // 2 + floor_c
            adj[a].append((virtual_node, cost_to_virtual))

            cost_from_virtual = (a) // 2 + ceil_c
            adj[virtual_node].append((a, cost_from_virtual))

    INF = 1 << 60
    dist = [INF] * (N + M + 1)
    dist[1] = 0
    heap = []
    heapq.heappush(heap, (0, 1))

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

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

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