POJ 3701 概率DP

给定2^n 支足球队进行比赛,n<=7. 队伍两两之间有一个获胜的概率,求每一个队伍赢得最后比赛的概率是多少?

状态其实都是很显然的,一开始觉得这个问题很难啊,不会。dp[i][j] 表示第i支队伍赢得前j轮比赛的概率。(这个题目处理区间的时候比较恶心,小心点即可)。

   1:   
   2:  #include <iostream>
   3:  #include <cstdio>
   4:  #include <cstring>
   5:  #include <map>
   6:  #include <algorithm>
   7:  using namespace std;
   8:   
   9:  double p[135][135];
  10:  double dp[135][10];
  11:   
  12:  pair<int,int> range(int idx, int round)
  13:  {
  14:      int x = (idx>>round) ^1;
  15:      return make_pair(x<<round, (x<<round) + (1<<(round)) - 1);
  16:  }
  17:  int main()
  18:  {
  19:  //    freopen("1.txt","r",stdin);
  20:      int n;
  21:      while(cin>>n && n !=-1)
  22:      {
  23:          for(int i=0; i<(1<<n); i++)
  24:          {
  25:              for(int j=0; j<(1<<n); j++)
  26:              {
  27:                  cin>>p[i][j];
  28:              }
  29:          }
  30:          memset(dp,0,sizeof(dp));
  31:          for(int i=0; i< (1<<n); i++)
  32:          {
  33:              dp[i][0] = p[i][i^1];
  34:          }
  35:          for(int round=1; round<n; round++) // round
  36:          {
  37:              for(int j=0; j< (1<<n); j++)
  38:              {
  39:                  pair<int,int> r = range(j, round);
  40:                  for(int k = r.first; k<= r.second; k++)
  41:                  {
  42:                      dp[j][round] += p[j][k] *dp[k][round - 1]*dp[j][round-1];
  43:                  }
  44:              }
  45:          }
  46:          int idx  = -1;
  47:          double ret = 0;
  48:          double sum = 0;
  49:          for(int i=0; i<(1<<n); i++)
  50:          {
  51:              sum+= dp[i][n-1];
  52:              if(dp[i][n-1] > ret)
  53:              {
  54:                  idx = i;
  55:                  ret = dp[i][n-1];
  56:              }
  57:          }
  58:          cout<<idx+1<<endl;
  59:      }
  60:  }

你可能感兴趣的:(poj)