最近在准备上海的ACM现场赛,刷了多类题目,发现自己的短板相当明显,特别是在dp上面,简直惨不忍睹。。。于是决心重新学习dp,从hdu4800开始。
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4800
Josephina and RPG
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1272 Accepted Submission(s): 375
Special Judge
Problem Description
A role-playing game (RPG and sometimes roleplaying game) is a game in which players assume the roles of characters in a fictional setting. Players take responsibility for acting out these roles within a narrative, either through literal acting or through a process of structured decision-making or character development.
Recently, Josephina is busy playing a RPG named TX3. In this game, M characters are available to by selected by players. In the whole game, Josephina is most interested in the “Challenge Game” part.
The Challenge Game is a team play game. A challenger team is made up of three players, and the three characters used by players in the team are required to be different. At the beginning of the Challenge Game, the players can choose any characters combination as the start team. Then, they will fight with N AI teams one after another. There is a special rule in the Challenge Game: once the challenger team beat an AI team, they have a chance to change the current characters combination with the AI team. Anyway, the challenger team can insist on using the current team and ignore the exchange opportunity. Note that the players can only change the characters combination to the latest defeated AI team. The challenger team gets victory only if they beat all the AI teams.
Josephina is good at statistics, and she writes a table to record the winning rate between all different character combinations. She wants to know the maximum winning probability if she always chooses best strategy in the game. Can you help her?
Input
There are multiple test cases. The first line of each test case is an integer M (3 ≤ M ≤ 10), which indicates the number of characters. The following is a matrix T whose size is R × R. R equals to C(M, 3). T(i, j) indicates the winning rate of team i when it is faced with team j. We guarantee that T(i, j) + T(j, i) = 1.0. All winning rates will retain two decimal places. An integer N (1 ≤ N ≤ 10000) is given next, which indicates the number of AI teams. The following line contains N integers which are the IDs (0-based) of the AI teams. The IDs can be duplicated.
Output
For each test case, please output the maximum winning probability if Josephina uses the best strategy in the game. For each answer, an absolute error not more than 1e-6 is acceptable.
Sample Input
4
0.50 0.50 0.20 0.30
0.50 0.50 0.90 0.40
0.80 0.10 0.50 0.60
0.70 0.60 0.40 0.50
3
0 1 2
Sample Output
0.378000
题目意思很繁杂,大概就是我方军队有m个人,从m个角色中组成C(m,3)支队伍,现在要组团去打对面n个敌人。当i与j打架时i赢的概率为t(i,j),并且如果打赢了一个敌人,可以与之互换id。求我方全赢的最大概率。
训练赛的时候没能写出来,一直wa,只能说,dp的边界条件很重要。。
思路:
用dp[i][j]表示第j号战士打赢i号对手的概率,要考虑重复的情况。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
double dp[11000][122];
double str[122][122];
int st[11000];
int main()
{
int m,n;
while(~scanf("%d",&m))
{
m = m * (m - 1) * (m - 2) / 6;
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
scanf("%lf",&str[i][j]);
scanf("%d",&n);
for(int i = 1; i <= n; i++)
scanf("%d",&st[i]);
memset(dp,0,sizeof(dp));
for(int i = 0; i < m; i++)
dp[1][i] = str[i][st[1]];
for(int i = 2;i <= n;i++)
for(int j = 0;j < m;j++)
{
if(j == st[i - 1])
for(int k = 0;k < m;k++)
dp[i][j] = max(dp[i][j],dp[i - 1][k] * str[j][st[i]]);
else
dp[i][j] = dp[i - 1][j] * str[j][st[i]];
}
double ans = -99999999;
for(int i = 0;i < m;i++)
ans = max(ans,dp[n][i]); //枚举
printf("%lf\n",ans);
}
return 0;
}