书山有路,学海无涯

POJ 3071 Football

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 matrix P will satisfy the constraints that pij = 1.0 − pji for all ij, 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


有2^n个球队比赛,一开始按次序1、2比,3、4比。。。。。2^n-1、2^n比。输的直接淘汰,赢者进入下一轮,一个矩阵中p[i][j]表示i打败j的概率。
最后最可能哪个队夺冠?



概率dp,因为是两两一起比赛,此淘汰赛次序可当成一个完全二叉树,每轮都存下此队能走到这一轮的概率。dfs下去写法与线段树类似。


 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<math.h>
 7 using namespace std;
 8 double p[222][222];
 9 double dp[222][10];
10 int  N;
11 void dfs(int l, int r, int temp)//temp is the number of rounds||可以当成二叉树的层数
12 {
13     if (l == r)
14         return;
15     int mid = l + r >> 1;
16     dfs(l, mid, temp + 1);//向下递归
17     dfs(mid + 1, r, temp + 1);
18     int i, j;
19     for (i = l; i <= r; i++)
20     {
21         dp[i][temp] += (dp[i][temp] == 0);//如果当前点概率是0,则改为1,为后面相乘做准备
22         dp[i][temp - 1] = 0;//把走到下一轮的数组清空,准备累加
23     }
24     for (i = l; i <= mid; i++)
25     {
26         for (j = mid + 1; j <= r; j++)
27         {
28             dp[i][temp - 1] += dp[i][temp] * p[i][j] * dp[j][temp];//新的答案往temp-1放,最终答案是temp=0
29             dp[j][temp - 1] += dp[j][temp] * p[j][i] * dp[i][temp];//当前i走到这一轮的概率*j走到这一轮的概率*i打败j的概率
30         }//然后i与每个比赛的都加起来,所以是累加
31     }
32 }
33 int main()
34 {
35     int n;
36     while (scanf("%d", &n) != EOF)
37     {
38         if (n == -1)
39             break;
40         memset(p, 0, sizeof(p));
41         memset(dp, 0, sizeof(dp));
42         N = 1 << n;
43         int i, j;
44         for (i = 1; i <= N; i++)
45         {
46             for (j = 1; j <= N; j++)
47             {
48                 scanf("%lf", &p[i][j]);
49             }
50         }
51         dfs(1, N, 1);
52         int ans = 0;
53         double ma = 0;
54         for (i = 1; i <= N; i++)
55         {
56             if (dp[i][0]>ma)
57             {
58                 ma = dp[i][0];
59                 ans = i;
60             }
61         }//概率最大的那队获胜
62         printf("%d\n", ans);
63     }
64 }

 



posted @ 2017-08-08 21:26  Jimmy_King  阅读(146)  评论(0编辑  收藏  举报