結果

問題 No.298 話の伝達
ユーザー maspymaspy
提出日時 2020-04-08 19:56:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 345 ms / 5,000 ms
コード長 1,014 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 87,356 KB
実行使用メモリ 85,516 KB
最終ジャッジ日時 2023-09-26 10:51:03
合計ジャッジ時間 4,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,400 KB
testcase_01 AC 72 ms
71,384 KB
testcase_02 AC 70 ms
71,328 KB
testcase_03 AC 74 ms
71,468 KB
testcase_04 AC 71 ms
71,560 KB
testcase_05 AC 345 ms
85,432 KB
testcase_06 AC 91 ms
76,848 KB
testcase_07 AC 71 ms
71,172 KB
testcase_08 AC 71 ms
71,220 KB
testcase_09 AC 88 ms
77,068 KB
testcase_10 AC 97 ms
77,048 KB
testcase_11 AC 341 ms
85,460 KB
testcase_12 AC 207 ms
81,224 KB
testcase_13 AC 342 ms
85,300 KB
testcase_14 AC 215 ms
81,224 KB
testcase_15 AC 74 ms
71,176 KB
testcase_16 AC 326 ms
85,516 KB
testcase_17 AC 87 ms
76,776 KB
testcase_18 AC 144 ms
79,196 KB
testcase_19 AC 72 ms
71,356 KB
testcase_20 AC 203 ms
81,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
m = map(int, read().split())
ABC = tuple(zip(m,m,m))
graph = [[] for _ in range(N)]
in_deg = [0] * N
for a, b, c in ABC:
    graph[a].append(b)
    in_deg[b] += 1

order = []
for _ in range(N):
    i = in_deg.index(min(in_deg))
    in_deg[i] = 1000
    order.append(i)
    for j in graph[i]:
        in_deg[j] -= 1

goal = order.index(N - 1)
mat = [0] * (N * N)
for a, b, c in ABC:
    i = order.index(a)
    j = order.index(b)
    mat[N * i + j] = c / 100

dp = [0.0] * (1 << N)
dp[1] = 1.0
for i in range(1, N):
    for n in range(1 << i):
        m = n + (1 << i)
        p = 1  # not talk
        for j in range(i):
            if (n >> j) & 1:
                p *= (1 - mat[N * j + i])
        dp[m] = (1 - p) * dp[n]
        dp[n] *= p

answer = 0
for n in range(1 << N):
    if n & (1 << goal):
        answer += dp[n]
print(answer)
0