結果

問題 No.298 話の伝達
ユーザー gew1fw
提出日時 2025-06-12 21:37:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,450 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 55,600 KB
最終ジャッジ日時 2025-06-12 21:39:37
合計ジャッジ時間 1,861 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    idx = 0
    N = int(data[idx])
    idx +=1
    M = int(data[idx])
    idx +=1
    
    adj_out = [[] for _ in range(N)]
    predecessors = [[] for _ in range(N)]  # (A, C)
    
    for _ in range(M):
        A = int(data[idx])
        idx +=1
        B = int(data[idx])
        idx +=1
        C = int(data[idx])
        idx +=1
        adj_out[A].append(B)
        predecessors[B].append( (A, C) )
    
    # Compute in_degree for each node
    in_degree = [0]*N
    for B in range(N):
        in_degree[B] = len(predecessors[B])
    
    # Kahn's algorithm to find topological order
    queue = deque()
    for i in range(N):
        if in_degree[i] == 0:
            queue.append(i)
    
    top_order = []
    while queue:
        u = queue.popleft()
        top_order.append(u)
        for v in adj_out[u]:
            in_degree[v] -=1
            if in_degree[v] == 0:
                queue.append(v)
    
    # Initialize probabilities
    P = [0.0] * N
    P[0] = 1.0  # Starting group
    
    for B in top_order:
        if B == 0:
            continue
        product = 1.0
        for (A, C) in predecessors[B]:
            prob = C / 100.0
            term = 1.0 - (P[A] * prob)
            product *= term
        P[B] = 1.0 - product
    
    print("{0:.12f}".format(P[N-1]))

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