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;
    }
    /**
    
    */
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    使用 kubeadm 部署 v1.18.5 版本 Kubernetes 集群
    MHA高可用主从复制实现
    基于Keepalived高可用集群的MariaDB读写分离机制实现
    MariaDB主从半同步复制详解
    postman学习网址
    程序员的脑子是更新换代最快的
    带组装3ROS_1节点_192通道
    带组装4ROS_1节点_256通道
    带组装5ROS_1node_320Chan
    带组装7ROS_1节点_448通道
  • 原文地址:https://www.cnblogs.com/caijiaming/p/12487850.html
Copyright © 2011-2022 走看看