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]);
    }
}
时间: 2024-12-27 10:21:14

HDU 5013 City Tour的相关文章

hdu 1853 Cyclic Tour &amp;&amp; hdu 3435 A new Graph Game(简单KM算法)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1478    Accepted Submission(s): 750 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

HDU 之 City Game

City Game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas,

HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

HDU 1853 Cyclic Tour(KM完美匹配)

HDU 1853 Cyclic Tour 题目链接 题意:一个有向图,边有权值,求把这个图分成几个环,每个点只能属于一个环,使得所有环的权值总和最小,求这个总和 思路:KM完美匹配,由于是环,所以每个点出度入度都是1,一个点拆成两个点,出点和入点,每个点只能用一次,这样就满足了二分图匹配,然后用KM完美匹配去就最小权值的匹配即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <

HDU 3274 City Planning

题意:给你一组数n  m  n的意思是有多少个村庄,并且给你n-1个关系,m的意思是要你连通的村庄.现在要你求出连通m个村庄所花费的钱 思路:题目一看数据,就像是要你去求最小生成树的子数,但是仔细审题会发现一句"Meanwhile you should use the least money. You may suppose that the initial transportation network makes up a tree."好吧,原来给你的网络图是一棵树,这样题目就直接简

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1904    Accepted Submission(s): 951 Problem Description There are N cities in our c

hdu 1853 Cyclic Tour 最大权值匹配 所有点连成环的最小边权和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1904    Accepted Submission(s): 951 Problem Description There are N cities in our c

HDU 1505 City Game (hdu1506 dp二维加强版)

F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1505 Appoint description: Description Bob is a strategy game programming specialist. In his new city building game the gaming enviro