結果

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

ソースコード

diff #

from collections import deque

def main():
    n, m = map(int, input().split())
    adj_out = [[] for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        adj_out[a].append((b, c))
    
    # Find reachable nodes using BFS
    reachable = set()
    queue = deque([0])
    reachable.add(0)
    while queue:
        u = queue.popleft()
        for (v, _) in adj_out[u]:
            if v not in reachable:
                reachable.add(v)
                queue.append(v)
    
    target = n - 1
    if target not in reachable:
        print(0.0)
        return
    
    # Build incoming edges for all nodes (original input edges)
    incoming_edges = [[] for _ in range(n)]
    for a in range(n):
        for (b, c) in adj_out[a]:
            incoming_edges[b].append((a, c))
    
    # Build in_degree for reachable nodes only
    in_degree = {u: 0 for u in reachable}
    adj_reachable = [[] for _ in range(n)]
    for u in reachable:
        for (v, c) in adj_out[u]:
            if v in reachable:
                adj_reachable[u].append((v, c))
                in_degree[v] += 1
    
    # Kahn's algorithm to get topological order of reachable nodes
    queue = deque()
    for u in reachable:
        if in_degree[u] == 0:
            queue.append(u)
    top_order = []
    while queue:
        u = queue.popleft()
        top_order.append(u)
        for (v, _) in adj_reachable[u]:
            in_degree[v] -= 1
            if in_degree[v] == 0:
                queue.append(v)
    
    # Calculate probabilities
    prob = [0.0] * n
    prob[0] = 1.0
    for y in top_order:
        if y == 0:
            continue  # prob[0] is already set
        product = 1.0
        for (a, c) in incoming_edges[y]:
            contrib = prob[a] * (c / 100.0)
            product *= (1.0 - contrib)
        prob[y] = 1.0 - product
    
    print("{0:.10f}".format(prob[target]))

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