結果

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

ソースコード

diff #

from collections import deque

def main():
    n, m = map(int, input().split())
    adj = [[] for _ in range(n)]  # adjacency list for forward edges
    in_edges = [[] for _ in range(n)]  # list of incoming edges (A, C) for each node B

    for _ in range(m):
        a, b, c = map(int, input().split())
        adj[a].append(b)
        in_edges[b].append((a, c))

    # Find reachable nodes from group 0 using BFS
    def get_reachable(start):
        visited = [False] * n
        q = deque([start])
        visited[start] = True
        while q:
            u = q.popleft()
            for v in adj[u]:
                if not visited[v]:
                    visited[v] = True
                    q.append(v)
        return [i for i in range(n) if visited[i]]

    reachable = get_reachable(0)
    if (n-1) not in reachable:
        print(0.0)
        return

    # Perform topological sort on the reachable nodes
    def topological_sort(nodes):
        visited = {node: False for node in nodes}
        order = []
        def dfs(u):
            if visited[u]:
                return
            visited[u] = True
            for v in adj[u]:
                if v in visited and not visited[v]:
                    dfs(v)
            order.append(u)
        for node in nodes:
            if not visited[node]:
                dfs(node)
        return order[::-1]

    topo_order = topological_sort(reachable)

    # Calculate probabilities
    prob = [0.0] * n
    prob[0] = 1.0  # initial group

    for u in topo_order:
        if u == 0:
            continue
        product = 1.0
        for (a, c) in in_edges[u]:
            if a < 0 or a >= n:
                continue  # should not happen as per input constraints
            product *= (1.0 - prob[a] * (c / 100.0))
        prob[u] = 1.0 - product

    print("{0:.10f}".format(prob[n-1]))

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