首页 > 代码库 > HDU 5013 City Tour

HDU 5013 City Tour

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5013

题意:

思路:

这里有错,是Hi(x)=sigama(Hji)(j属于x)

const int N=18;int m,n;double p[N],H[N][N];double f[N][1<<N];double dp[N][1<<N];double a[1<<N],aa[1<<N];double pp[1<<N],qq[1<<N];int cnt[1<<N];void init(){    int i;    for(i=1;i<(1<<N);i++) cnt[i]=cnt[i>>1]+(i&1);}int sgn(double x){    if(x>1e-20) return 1;    if(x<-1e-20) return -1;    return 0;}int main(){    init();    while(scanf("%d%d",&m,&n)!=-1)    {        int i,j,k;        for(i=0;i<m;i++)         {            scanf("%lf",&p[i]);            if(sgn(p[i]-1)==0) p[i]-=1e-10;        }        for(i=0;i<(1<<m);i++)        {            pp[i]=qq[i]=1;            for(j=0;j<m;j++) if(i&(1<<j)) pp[i]*=p[j],qq[i]*=1-p[j];        }        for(i=0;i<m;i++) for(j=1;j<=n;j++) scanf("%lf",&H[i][j]);        for(i=0;i<(1<<m);i++)        {            dp[n][i]=0;            f[n][i]=0;            for(j=0;j<m;j++) if(i&(1<<j))             {                f[n][i]+=H[j][n];                dp[n][i]+=H[j][n];            }        }        for(i=n-1;i>=1;i--)        {            for(j=0;j<(1<<m);j++)             {                a[j]=f[i+1][j]*pp[j]/qq[j];                aa[j]=dp[i+1][j]*cnt[j]*pp[j]/qq[j];            }            for(k=1;k<=m;k++) for(j=(1<<m)-1;j>=0;j--)            {                if(j&(1<<(k-1)))                 {                    a[j]=a[j]+a[j^(1<<(k-1))];                    aa[j]=aa[j]+aa[j^(1<<(k-1))];                }            }            f[i][0]=dp[i][0]=0;            for(j=1;j<(1<<m);j++)            {                dp[i][j]=0;                for(k=0;k<m;k++) if(j&(1<<k)) dp[i][j]+=H[k][i];                f[i][j]=dp[i][j]+qq[j]*a[j]+qq[j]/cnt[j]*aa[j];            }        }        printf("%.10lf\n",f[1][(1<<m)-1]);    }}

 

HDU 5013 City Tour