hdu5045狀態壓縮DP

題目描述

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

算法思路

  1. 這一題最關鍵的一點是,每個人的時間之差不可以超過一小時,那麼從這一點,我們可以得到很多的結論。
  2. 首先,我們可以將M個問題分成M/N個組,在每個組當中我們可以知道,每個人都會做1題,如果超過1題,顯然會存在一個人在這一組題目當中沒有做題,那麼就不符合當前的定義了。
  3. 所以我們採用狀態壓縮,二進制表示的第i位表示當前的這個組中已經選擇了第i個人來做題。
  4. 爲了減少轉移狀態的數量,採用遞歸的方式來實現。

代碼

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

#define MAXN 1005
#define MAXM 10

int T,N,M;
double P[MAXM][MAXN];
double dp[MAXN][1<<MAXM];
bool vis[MAXN][1<<MAXM];
int cas = 0;

double Dfs(int index,int position)
{
    if(index == M+1)return 0.0;
    if(vis[index][position])return dp[index][position];
    vis[index][position]=true;

    int i;
    int nextPosition;
    double temp = 0.0;
    for(i=0;i<N;i++){
        if(!(position&(1<<i))){
        //we should reset the position when a group 
        // is over!
            if(index%N==0)nextPosition=0;
            else nextPosition = position | (1<<i);
            temp = max(temp,P[i][index]+Dfs(index+1,nextPosition));
        }
    }
    return dp[index][position]=temp;
}

int main()
{
    //freopen("input","r",stdin);
    int i,j;
    scanf("%d",&T);

    while(T--){

        scanf("%d%d",&N,&M);

        memset(vis,false,sizeof(vis));

        for(i=0;i<N;i++){
            for(j=1;j<=M;j++)
                scanf("%lf",&P[i][j]);
        }

        printf("Case #%d: ",++cas);
        printf("%.5f\n",Dfs(0,0));

    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章