結果

問題 No.298 話の伝達
ユーザー lam6er
提出日時 2025-04-15 22:41:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,999 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 81,916 KB
実行使用メモリ 56,084 KB
最終ジャッジ日時 2025-04-15 22:42:17
合計ジャッジ時間 1,698 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    N, M = map(int, sys.stdin.readline().split())
    edges = []
    in_edges = [[] for _ in range(N)]  # in_edges[B] contains list of (A, C)
    adj = [[] for _ in range(N)]       # adj[A] contains list of B
    for _ in range(M):
        A, B, C = map(int, sys.stdin.readline().split())
        edges.append((A, B, C))
        in_edges[B].append((A, C))
        adj[A].append(B)
    
    # Find reachable nodes from 0 using BFS
    reachable = set()
    queue = deque([0])
    reachable.add(0)
    while queue:
        u = queue.popleft()
        for v in adj[u]:
            if v not in reachable:
                reachable.add(v)
                queue.append(v)
    
    # If N-1 is not reachable, output 0.0
    if (N-1) not in reachable:
        print("0.0")
        return
    
    # Compute in-degree for reachable nodes
    in_degree = {node: 0 for node in reachable}
    for B in reachable:
        for (A, C) in in_edges[B]:
            if A in reachable:
                in_degree[B] += 1
    
    # Topological sort
    topological_order = []
    queue = deque()
    for node in reachable:
        if in_degree[node] == 0:
            queue.append(node)
    
    while queue:
        u = queue.popleft()
        topological_order.append(u)
        for v in adj[u]:
            if v in reachable:
                in_degree[v] -= 1
                if in_degree[v] == 0:
                    queue.append(v)
    
    # Calculate probabilities
    prob = {node: 0.0 for node in reachable}
    prob[0] = 1.0
    
    for u in topological_order:
        if u == 0:
            continue
        product = 1.0
        for (A, C) in in_edges[u]:
            if A not in reachable:
                continue
            p = prob[A] * (C / 100.0)
            product *= (1.0 - p)
        prob[u] = 1.0 - product
    
    # Output the result with sufficient precision
    print("{0:.10f}".format(prob[N-1]))

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