結果

問題 No.1480 Many Complete Graphs
ユーザー lam6er
提出日時 2025-04-15 21:25:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,946 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 101,860 KB
最終ジャッジ日時 2025-04-15 21:29:55
合計ジャッジ時間 8,061 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    ptr = 0
    N = int(data[ptr])
    ptr += 1
    M = int(data[ptr])
    ptr += 1
    ops = []
    for _ in range(M):
        k_i = int(data[ptr])
        ptr += 1
        c_i = int(data[ptr])
        ptr += 1
        s_i = list(map(int, data[ptr:ptr + k_i]))
        ptr += k_i
        ops.append((k_i, c_i, s_i))
    
    INF = 1 << 60
    distance = [INF] * (N + 1)
    distance[1] = 0
    heap = []
    heapq.heappush(heap, (0, 1))
    
    # Process each operation once
    for op in ops:
        k_i, c_i, s_i = op
        min_val = INF
        # Find the minimal 2*d + u + 1 among nodes in s_i that have been reached
        for u in s_i:
            if distance[u] < INF:
                current_val = 2 * distance[u] + u + 1
                if current_val < min_val:
                    min_val = current_val
        if min_val == INF:
            continue  # No nodes in this operation are reachable yet
        # Update distances for all nodes in s_i
        for v in s_i:
            new_dist = (min_val + v) // 2 + c_i
            if new_dist < distance[v]:
                # Only push to heap if this is a better distance
                if distance[v] == INF:
                    distance[v] = new_dist
                    heapq.heappush(heap, (new_dist, v))
                else:
                    if new_dist < distance[v]:
                        distance[v] = new_dist
                        heapq.heappush(heap, (new_dist, v))
    
    # Continue with Dijkstra's algorithm to ensure all nodes are processed
    while heap:
        d, u = heapq.heappop(heap)
        if u == N:
            break
        if d > distance[u]:
            continue
        # No explicit edges to process here; all edges are handled via the operations
    
    print(distance[N] if distance[N] != INF else -1)

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