結果

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

ソースコード

diff #

import sys
from collections import defaultdict, deque

def main():
    N, M = map(int, sys.stdin.readline().split())
    edges = defaultdict(list)  # Forward edges for BFS/DFS
    reverse_edges = defaultdict(list)  # Reverse edges for probability calculation

    for _ in range(M):
        A, B, C = map(int, sys.stdin.readline().split())
        edges[A].append(B)
        reverse_edges[B].append((A, C))

    # Determine all nodes reachable from group 0 using BFS
    reachable = set()
    queue = deque([0])
    reachable.add(0)
    while queue:
        u = queue.popleft()
        for v in edges[u]:
            if v not in reachable:
                reachable.add(v)
                queue.append(v)

    # Perform topological sort using DFS on reachable nodes
    visited = set()
    topo_order = []

    def dfs(u):
        if u in visited or u not in reachable:
            return
        visited.add(u)
        for v in edges[u]:
            dfs(v)
        topo_order.append(u)

    dfs(0)
    topo_order.reverse()  # Reverse to get the correct topological order

    # Initialize probabilities
    prob = [0.0] * N
    prob[0] = 1.0  # Group 0 starts with probability 1.0

    # Process each node in topological order
    for v in topo_order:
        if v == 0:
            continue  # Skip group 0 as its probability is already set
        product = 1.0
        for (u, c) in reverse_edges[v]:
            if u in reachable:
                product *= (1.0 - prob[u] * (c / 100))
        prob[v] = 1.0 - product

    # Output the result with sufficient precision
    print("{0:.10f}".format(prob[N-1]))

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