結果

問題 No.298 話の伝達
ユーザー lam6er
提出日時 2025-04-15 22:43:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,075 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 56,324 KB
最終ジャッジ日時 2025-04-15 22:44:40
合計ジャッジ時間 1,874 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 = []
    for _ in range(M):
        A, B, C = map(int, sys.stdin.readline().split())
        edges.append((A, B, C))
    
    # Determine reachable nodes using BFS
    adj = [[] for _ in range(N)]
    for A, B, C in edges:
        adj[A].append(B)
    
    reachable = [False] * N
    q = deque([0])
    reachable[0] = True
    while q:
        u = q.popleft()
        for v in adj[u]:
            if not reachable[v]:
                reachable[v] = True
                q.append(v)
    
    # Extract subgraph edges
    sub_edges = []
    for A, B, C in edges:
        if reachable[A] and reachable[B]:
            sub_edges.append((A, B, C))
    
    sub_nodes = [i for i in range(N) if reachable[i]]
    if not sub_nodes:
        print(0.0)
        return
    
    # Prepare for topological sort using Kahn's algorithm
    in_degree = {i: 0 for i in sub_nodes}
    adj_sub = {i: [] for i in sub_nodes}
    for A, B, C in sub_edges:
        adj_sub[A].append((B, C))
        in_degree[B] += 1
    
    q = deque()
    for node in sub_nodes:
        if in_degree[node] == 0:
            q.append(node)
    
    top_order = []
    while q:
        u = q.popleft()
        top_order.append(u)
        for (v, _) in adj_sub[u]:
            in_degree[v] -= 1
            if in_degree[v] == 0:
                q.append(v)
    
    # Collect incoming edges for each node
    in_edges = {i: [] for i in sub_nodes}
    for A, B, C in sub_edges:
        in_edges[B].append((A, C))
    
    # Calculate probabilities
    p = [0.0] * N
    if reachable[0]:
        p[0] = 1.0
    
    for u in top_order:
        if u == 0:
            continue
        prob_fail = 1.0
        for (A, C) in in_edges[u]:
            prob = p[A] * (C / 100.0)
            prob_fail *= (1.0 - prob)
        p[u] = 1.0 - prob_fail
    
    # Output the result
    if reachable[N-1]:
        print("{0:.10f}".format(p[N-1]))
    else:
        print(0.0)

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