codeforces 16E Fish (概率-期望DP)

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.

Example
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条,每两只相遇都会有其中一只吃掉对方,现在给出每条鱼吃掉其他鱼的概率,问最后每条鱼存活的概率。

 分析:因为最多有18条鱼,吃掉的概率都不一样,所以可以用状态压缩的方法来写,设dp[1<<n]种状态,

 dp[i]表示当前鱼的状态为i时的概率;

 那么P(i吃掉j) = P(i和j同时存在) *P(ij相遇)* P(i战胜j) 

 方程就是dp[sta^(1<<j)] = dp[sta]*p[i][j]/(sum*(sum-1)/2);

 具体解释见代码注释;


【AC代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int N=18;
double dp[1<<(N+1)];
int m,n;
double p[N+1][N+1];

int main()
{
    while(~scanf("%d",&m))
    {
        for(int i=0;i<m;++i)
            for(int j=0;j<m;++j)
            scanf("%lf",&p[i][j]);
        dp[(1<<m)-1]=1.0;//所有鱼都在  概率是1
        for(int sta=(1<<m)-1;sta>=0;--sta){ //遍历所有状态
            for(int i=0;i<m;++i){//遍历获胜的鱼的编号
                if(sta & (1<<i)){//i号鱼还活着 ( 小小的剪枝  能比网上其他的代码快200ms左右)
                for(int j=0;j<m;++j){//遍历获胜鱼i要吃掉的j号鱼
                    if((sta & (1<<j))){//j号鱼还活着
                        if(i == j) continue ;//是自己就跳
                        int num=__builtin_popcount(sta);//获取32位二进制数数中1的个数 函数
                        dp[sta^(1<<j)] += dp[sta]*p[i][j]*1.0/(num*(num-1)/2);// 式子左边 sta中第j位的1一定是存在的,这里是对第j位取0 表示i号鱼吃掉了j号鱼
                    }   //式子右边表示未吃掉之前i号鱼和j号鱼都存在的状态概率*i吃掉j的概率*i和j相遇的概率
                }
                }
            }
        }
        for(int i=0;i<m;++i)
            printf("%.6f ",dp[(1<<i)]);//分别输出只有第i条鱼活着的概率;
        printf("\n");
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值