結果

問題 No.298 話の伝達
ユーザー yosupotyosupot
提出日時 2015-11-07 00:30:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 3,757 ms
コンパイル使用メモリ 160,760 KB
実行使用メモリ 178,916 KB
最終ジャッジ日時 2023-09-02 20:24:13
合計ジャッジ時間 5,011 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 148 ms
170,036 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 6 ms
6,584 KB
testcase_11 AC 146 ms
178,664 KB
testcase_12 AC 71 ms
88,928 KB
testcase_13 AC 146 ms
177,620 KB
testcase_14 AC 71 ms
84,980 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 145 ms
178,916 KB
testcase_17 AC 4 ms
4,512 KB
testcase_18 AC 35 ms
41,624 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 70 ms
85,728 KB
権限があれば一括ダウンロードができます

ソースコード

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