题目链接:POJ 2151 Check the difficulty of problems
题面:
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 5519 | Accepted: 2431 | 
Description
Input
Output
Sample Input
2 2 2 0.9 0.9 1 0.9 0 0 0
Sample Output
0.972
Source
第一次写概率DP,感觉和DP有些不一样,借鉴了这篇博客:金海峰
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
double correspond[1005][31];
double dp[1005][31][31];
int main()
{
	int n,m,t;
	double ans=1,tmp,sum;
	while(scanf("%d%d%d",&m,&t,&n)&&(m||t||n))
	{
		ans=tmp=1;
		for(int i=0;i<t;i++)
		{
			for(int j=1;j<=m;j++)
			  scanf("%lf",&correspond[i][j]); 
		}
		memset(dp,0,sizeof(dp)); 
		//dp[i][j][k] 第i只队伍,做前j题,做对k题的概率 
		for(int i=0;i<t;i++)
	     {
          dp[i][0][0]=1;
          for (int j=1;j<=m;j++)
          {
       	     //还是做对0道,那么就是当前这一道没做对 
             dp[i][j][0] = dp[i][j -1][0] * (1- correspond[i][j]);
             for (int k =1; k <= j; k++)
               //做对k道,要么是之前就做对了k道,这次没对,要么就是之前对了k-1道,这次对了 
               dp[i][j][k] = dp[i][j -1][k -1] * correspond[i][j]+ dp[i][j -1][k] * (1- correspond[i][j]);
          }
		 }
	    //排除任何一个人一道都没做对的情况 
        for(int i=0;i<t;i++)
          ans *=(1- dp[i][m][0]);
        //排除所有人都只做对1~n-1的情况 
       for(int i=0;i<t;i++)
        {
          sum=0;
          for(int j=1;j<n;j++)
          sum+=dp[i][m][j];
          tmp*=sum;
        }
       ans-=tmp;
       printf("%.3f\n", ans);
	}
	return 0;
} POJ 2151 Check the difficulty of problems
原文:http://blog.csdn.net/david_jett/article/details/45588013