結果

問題 No.298 話の伝達
ユーザー yosupot
提出日時 2015-11-07 00:30:11
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 185 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 160,388 KB
実行使用メモリ 166,656 KB
最終ジャッジ日時 2024-06-12 02:46:45
合計ジャッジ時間 4,109 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.conv;
import std.algorithm, std.range, std.random;
import std.string, std.array, std.container, std.bigint;
import std.typecons;

alias E = Tuple!(int, int, double);


int[] tpor(int n, E[] e) {
    bool[] used = new bool[n];
    int[] res;
    while (res.length != n) {
        foreach (i; 0..n) {
            if (used[i]) continue;
            bool f = true;
            foreach (v; e) {
                if (v[1] != i) continue;
                if (used[v[0]]) continue;
                f = false;
                break;
            }
            if (!f) continue;
            res ~= i;
            used[i] = true;
//            writeln(res);
        }
    }
    return res;
}

int main() {
    int n, m;
    readf("%d %d\n", &n, &m);

    double[][] g = new double[][](n, n);
    foreach (i; 0..n) {
        foreach (j; 0..n) {
            g[i][j] = 0;
        }
    }

    E[] ed;
    foreach (i; 0..m) {
        int a, b; double c;
        readf("%d %d %f\n", &a, &b, &c);
        ed ~= tuple(a, b, c);
//        g[a][b] = c / 100.0;
    }
    auto l = tpor(n, ed);
    int[] rl = new int[](n);
    foreach (i; 0..n) {
        rl[l[i]] = i;
    }

    foreach (e; ed) {
        g[rl[e[0]]][rl[e[1]]] = e[2] / 100.0;
    }

    double[][] dp = new double[][](n, 1<<n);

    foreach (i; 0..n) {
        foreach (j; 0..1<<n) {
            dp[i][j] = 0;
        }
    }
    dp[0][1] = 1;

    foreach (i; 1..n) {
        foreach (j; 0..1<<i) {
            double sm = 1;
            foreach (k; 0..i) {
                if (j & (1<<k)) {
                    sm *= (1 - g[k][i]);
                }
            }
            dp[i][j] += sm * dp[i-1][j];
            dp[i][j | (1<<i)] += (1 - sm) * dp[i-1][j];
        }
    }

    double res = 0;
    foreach (j; 0..1<<(rl[n-1]+1)) {
        if (j & (1<<rl[n-1])) {
            res += dp[rl[n-1]][j];
        }
    }

    writef("%.20f\n", res);
    return 0;
}
0