hdu5045(状态压缩dp)

Contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1323    Accepted Submission(s): 534


Problem Description
In the ACM International Collegiate Programming Contest, each team consist of three students. And the teams are given 5 hours to solve between 8 and 12 programming problems. 

On Mars, there is programming contest, too. Each team consist of N students. The teams are given M hours to solve M programming problems. Each team can use only one computer, but they can’t cooperate to solve a problem. At the beginning of the ith hour, they will get the ith programming problem. They must choose a student to solve this problem and others go out to have a rest. The chosen student will spend an hour time to program this problem. At the end of this hour, he must submit his program. This program is then run on test data and can’t modify any more. 

Now, you have to help a team to find a strategy to maximize the expected number of correctly solved problems. 

For each problem, each student has a certain probability that correct solve. If the ith student solve the jth problem, the probability of correct solve is Pij .

At any time, the different between any two students’ programming time is not more than 1 hour. For example, if there are 3 students and there are 5 problems. The strategy {1,2,3,1,2}, {1,3,2,2,3} or {2,1,3,3,1} are all legal. But {1,1,3,2,3},{3,1,3,1,2} and {1,2,3,1,1} are all illegal. 

You should find a strategy to maximize the expected number of correctly solved problems, if you have know all probability
 

Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N ≤ 10,1 ≤ M ≤ 1000),denoting the number of students and programming problem, respectively.

The next N lines, each lines contains M real numbers between 0 and 1 , the jth number in the ith line is Pij .
 

Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then a single real number means the maximal expected number of correctly solved problems if this team follow the best strategy, to five digits after the decimal point. Look at the output for sample input for details.
 

Sample Input
1 2 3 0.6 0.3 0.4 0.3 0.7 0.9
 

Sample Output
Case #1: 2.20000

题意:有N个人组成一支队伍来答题,每个人答每道题的正确率都是已知的,但是有个要求:一直每个人答任何一道题目的时间都是1个小时,要求在任何时刻,任何两个人的答题累计时间都不能超过1.求这样答题的最优策略下最好的出题期望。

思路:

由于任何时间任何两个人的答题时间不能超过1(这句话看英文真的是看不懂!!英文伤T_T。)所以在每一轮里面要先用掉所有人然后开始下一轮。

状态压缩dp,其实这题这么写还是有点风险,复杂度到了1e6还多,估计数据有点水。。

这题我用的dp[i][j]表示的做题做到了第i题时人的使用状态为j时的过的最多的题。这么写只要注意一点,就是当状态j的所有元素满了之后,要将其的状态转移变成全空。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
double p[15][1010];
double dp[1010][1<<10];
int n,m;
bool ok(int t,int j)
{
    if(t==0)return 0;
    int s=0;
    for(int i=0; i<n; i++)
        if(j&(1<<i))
            s++;
    if(s==t)
        return 1;
    return 0;
}

int main()
{
    int t,o=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                scanf("%lf",&p[i][j]);
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
            dp[1][1<<i]=p[i][0];
        for(int i=1; i<m; i++)
            for(int j=0; j<(1<<n); j++)
                if(dp[i][j]&&ok(i%n,j))
                {
                    for(int k=0; k<n; k++)
                        if(!((1<<k)&j))
                            dp[i+1][j|(1<<k)]=max(dp[i+1][j|(1<<k)],dp[i][j]+p[k][i]);
                }
                else if(dp[i][j]&&i%n==0)
                {
                    for(int k=0; k<n; k++)
                        dp[i+1][1<<k]=max(dp[i+1][j|(1<<k)],dp[i][j]+p[k][i]);
                }
        double ans=0;
        for(int i=0; i<(1<<n); i++)
            ans=max(ans,dp[m][i]);
        printf("Case #%d: %.5f\n",o++,ans);
    }
    return 0;
}

posted @ 2016-04-18 15:48  martinue  阅读(142)  评论(0编辑  收藏  举报