zoukankan      html  css  js  c++  java
  • E

    题目链接:http://codeforces.com/problemset/problem/16/E

    n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.

    Input

    The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix aaij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.

    Output

    Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.

    Examples

    Input
    2
    0 0.5
    0.5 0
    Output
    0.500000 0.500000 
    Input
    5
    0 1 1 1 1
    0 0 0.5 0.5 0.5
    0 0.5 0 0.5 0.5
    0 0.5 0.5 0 0.5
    0 0.5 0.5 0.5 0
    Output
    1.000000 0.000000 0.000000 0.000000 0.000000 
    题目大意:

    有一堆鱼,n条,每两只相遇都会有其中一只吃掉对方,现在给出每条鱼吃掉其他鱼的概率,问最后每条鱼存活的概率。

     思路:状压dp的模板题

    看代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<queue>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ull;
    #define sc1(a) scanf("%lld",&a)
    #define pf1(a) printf("%lld
    ",a)
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    const LL INF=1e18;
    const ull base=2333;
    const int maxn=1e1+50;
    const int maxm=1e3+50;
    const int maxv=1e6+5;
    const int mod=1e9+7;
    const int ba=3e5;
    double a[maxn][maxn];
    double dp[1<<20];
    int main()
    {
        LL N;sc1(N);
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++) scanf("%lf",&a[i][j]);
        }
        dp[(1<<N)-1]=1;//全部鱼存在的情况下 结果为1
        for(int s=(1<<N)-1;s>=0;s--)
        {
            LL sum=0;
            for(int i=0;i<N;i++)  if(s&(1<<i)) sum++;
            for(int i=0;i<N;i++)//枚举i吃掉 j
            {
                if(s&(1<<i)) //该位还活着
                {
                    for(int j=0;j<N;j++)
                    {
                        if(i==j) continue;//自己不会吃自己
                        if(s&(1<<j))
                        {
                            dp[s^(1<<j)]+=dp[s]*2.0/(sum*(sum-1))*a[i][j];
                        }
                    }
                }
            }
        }
        for(int i=0;i<N;i++) printf("%lf ",dp[(1<<i)]);
        return 0;
    }
    /**
    
    */
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    【前端技术文章阅读】--201608
    极简版 react+webpack 脚手架
    《JavaScript模式》第6章 代码复用模式
    《JavaScript模式》第5章 对象创建模式
    《JavaScript模式》第4章 函数
    《JavaScript模式》第3章 字面量和构造函数
    简版导航栏实现
    2014年第五届蓝桥杯试题C/C++程序设计B组——李白打酒
    2014第五届蓝桥杯试题C/C++程序设计B组——切面条
    2014年第五届蓝桥杯试题——啤酒和饮料
  • 原文地址:https://www.cnblogs.com/caijiaming/p/12487850.html
Copyright © 2011-2022 走看看