結果

問題 No.298 話の伝達
ユーザー lam6er
提出日時 2025-03-20 19:00:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,341 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2025-03-20 19:02:02
合計ジャッジ時間 1,843 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    N, M = map(int, sys.stdin.readline().split())
    adj = [[] for _ in range(N)]
    edges = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, sys.stdin.readline().split())
        if B != 0:  # Ignore edges pointing to group 0
            adj[A].append(B)
            edges[B].append((A, C))
    
    # Calculate in_degree for each node (number of incoming edges excluding those to 0)
    in_degree = [0] * N
    for b in range(N):
        in_degree[b] = len(edges[b])
    
    # Perform topological sort
    queue = deque()
    order = []
    for i in range(N):
        if in_degree[i] == 0:
            queue.append(i)
    while queue:
        u = queue.popleft()
        order.append(u)
        for v in adj[u]:
            in_degree[v] -= 1
            if in_degree[v] == 0:
                queue.append(v)
    
    # Initialize probabilities
    P = [0.0] * N
    P[0] = 1.0  # Group 0 starts the topic
    
    # Compute probabilities in topological order
    for u in order:
        if u == 0:
            continue  # already set to 1.0
        product = 1.0
        for (A, C) in edges[u]:
            product *= (1.0 - P[A] * (C / 100.0))
        P[u] = 1.0 - product
    
    print("{0:.10f}".format(P[N-1]))
    
if __name__ == "__main__":
    main()
0