poj 3071 Football (概率dp)

题目链接

Football
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3205   Accepted: 1632

Description

Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. In each round of the tournament, all teams still in the tournament are placed in a list in order of increasing index. Then, the first team in the list plays the second team, the third team plays the fourth team, etc. The winners of these matches advance to the next round, and the losers are eliminated. After n rounds, only one team remains undefeated; this team is declared the winner.

Given a matrix P = [pij] such that pij is the probability that team i will beat team j in a match determine which team is most likely to win the tournament.

Input

The input test file will contain multiple test cases. Each test case will begin with a single line containing n (1 ≤ n ≤ 7). The next 2n lines each contain 2n values; here, the jth value on the ith line represents pij. The matrixP will satisfy the constraints that pij = 1.0 − pji for all i ≠ j, and pii = 0.0 for all i. The end-of-file is denoted by a single line containing the number −1. Note that each of the matrix entries in this problem is given as a floating-point value. To avoid precision problems, make sure that you use either the double data type instead of float.

Output

The output file should contain a single line for each test case indicating the number of the team most likely to win. To prevent floating-point precision issues, it is guaranteed that the difference in win probability for the top two teams will be at least 0.01.

Sample Input

2
0.0 0.1 0.2 0.3
0.9 0.0 0.4 0.5
0.8 0.6 0.0 0.6
0.7 0.5 0.4 0.0
-1

Sample Output

2

Hint

In the test case above, teams 1 and 2 and teams 3 and 4 play against each other in the first round; the winners of each match then play to determine the winner of the tournament. The probability that team 2 wins the tournament in this case is:

P(2 wins)  P(2 beats 1)P(3 beats 4)P(2 beats 3) + P(2 beats 1)P(4 beats 3)P(2 beats 4)
p21p34p23 + p21p43p24
= 0.9 · 0.6 · 0.4 + 0.9 · 0.4 · 0.5 = 0.396.

The next most likely team to win is team 3, with a 0.372 probability of winning the tournament.

没大做过概率dp的题目,这题只看了一下别人的d[][]数组的定义,剩下的自己想了一会写的,居然1A。

题意:给定2^n行, 2^n列,i行j列代表第i 个人赢第j个人的概率,求经过n局,哪个人赢的概率最大,

比赛规则是相邻的人先比,赢的人进入下一局再与赢的相邻的人比,输的直接淘汰。

分析:d[i][j]代表第j个人,第i局赢的概率。d[i][j]  += d[i-1][j]*d[i-1][k]*p[j][k];

k是取相邻另一组的所有人。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 const int maxn = 150;
 7 double p[maxn][maxn], d[maxn][maxn];
 8 
 9 int main()
10 {
11     int n, i, j, k, sum, ans, l, r, x, y;
12     double Max;
13     while(~scanf("%d", &n) && n!=-1)
14     {
15         sum = pow(2, n);
16         memset(d, 0, sizeof(d));
17         for(i = 1; i <= sum; i++)
18             for(j = 1; j <= sum; j++)
19                 scanf("%lf", &p[i][j]);
20         for(j = 1; j <= sum; j++)
21             if(j&1)
22                 d[1][j] = p[j][j+1];
23             else
24                 d[1][j] = p[j][j-1];
25 
26         for(i = 2; i <= n; i++)
27             for(j = 1; j <= sum; j++)
28             {
29                 x = pow(2, i-1);
30                 y = (j-1)/x;
31                 if(y&1)
32                 {
33                     r = x*y + 1;  //用l和r计算当前的人和相邻的另外一组的比赛
34                     l = r - x;
35                 }
36                 else
37                 {
38                     l = (y+1)*x + 1;
39                     r = l+x;
40                 }
41                 for(k = l; k < r; k++)
42                 if(j != k)
43                 d[i][j] += d[i-1][j]*d[i-1][k]*p[j][k];
44             }
45         ans = 1; Max = d[n][1];
46         for(j = 1; j <= sum; j++)
47         if(d[n][j] > Max)
48         {
49             Max = d[n][j];
50             ans = j;
51         }
52         cout<<ans<<endl;
53     }
54     return 0;
55 }

 又重新做了一遍,代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <cmath>
 7 #include <algorithm>
 8 #define LL __int64
 9 #define eps 1e-8
10 const int maxn = (1<<7) + 10;
11 using namespace std;
12 double p[maxn][maxn], d[10][maxn];
13 
14 int main()
15 {
16     int n, i, j, k, sum;
17     while(~scanf("%d", &n))
18     {
19         if(n == -1) break;
20         memset(d, 0, sizeof(d));
21         sum = (1<<n);
22         for(i = 0; i < sum; i++)
23             for(j = 0; j < sum; j++)
24                 scanf("%lf", &p[i][j]);
25         for(i = 0; i < sum; i++)
26         {
27             if(i%2==0)
28                 d[1][i] = p[i][i+1];
29             else
30                 d[1][i] = p[i][i-1];
31         }
32         for(i = 2; i <= n; i++)
33         {
34             int x = (1<<(i-1));
35             for(j = 0; j < sum; j++)
36             {
37                 if(j/x%2==0)
38                     for(k = (j/x+1)*x; k < (j/x+2)*x; k++)
39                         d[i][j] += d[i-1][j]*d[i-1][k]*p[j][k];
40                 else
41                 {
42                     for(k = (j/x-1)*x; k < (j/x)*x; k++)
43                         d[i][j] += d[i-1][j]*d[i-1][k]*p[j][k];
44                 }
45             }
46         }
47         double ans = 0;
48         int ret = 0;
49         for(i = 0; i < sum; i++)
50         {
51             if(d[n][i]-ans > 0)
52             {
53                 ans = d[n][i];
54                 ret = i;
55             }
56         }
57         printf("%d
", ret+1);
58     }
59     return 0;
60 }
原文地址:https://www.cnblogs.com/bfshm/p/3770227.html