hdu5045状态压缩DP

    xiaoxiao2025-05-27  5

    题目描述

    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

    算法思路

    这一题最关键的一点是,每个人的时间之差不可以超过一小时,那么从这一点,我们可以得到很多的结论。首先,我们可以将M个问题分成M/N个组,在每个组当中我们可以知道,每个人都会做1题,如果超过1题,显然会存在一个人在这一组题目当中没有做题,那么就不符合当前的定义了。所以我们采用状态压缩,二进制表示的第i位表示当前的这个组中已经选择了第i个人来做题。为了减少转移状态的数量,采用递归的方式来实现。

    代码

    #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; }
    转载请注明原文地址: https://ju.6miu.com/read-1299316.html
    最新回复(0)