POJ-3071 Football 概率DP

  题目链接:http://poj.org/problem?id=3071

  题意:有2^n只足球队打比赛,编号1和2,3和4等进行淘汰制,胜利的进入下一轮接着淘汰,求最后哪支球队赢的概率最大。

  简单题概率DP,画一颗树就知道方程了,f[i][j]表示第 i 轮第 j 只球队获胜的概率,则f[i][j]=Σ( f[i-1][k] ),k为第 j 只球队能遇见的所有球队。

  1 //STATUS:C++_AC_94MS_320KB

  2 #include <functional>

  3 #include <algorithm>

  4 #include <iostream>

  5 //#include <ext/rope>

  6 #include <fstream>

  7 #include <sstream>

  8 #include <iomanip>

  9 #include <numeric>

 10 #include <cstring>

 11 #include <cassert>

 12 #include <cstdio>

 13 #include <string>

 14 #include <vector>

 15 #include <bitset>

 16 #include <queue>

 17 #include <stack>

 18 #include <cmath>

 19 #include <ctime>

 20 #include <list>

 21 #include <set>

 22 #include <map>

 23 using namespace std;

 24 //#pragma comment(linker,"/STACK:102400000,102400000")

 25 //using namespace __gnu_cxx;

 26 //define

 27 #define pii pair<int,int>

 28 #define mem(a,b) memset(a,b,sizeof(a))

 29 #define lson l,mid,rt<<1

 30 #define rson mid+1,r,rt<<1|1

 31 #define PI acos(-1.0)

 32 //typedef

 33 typedef __int64 LL;

 34 typedef unsigned __int64 ULL;

 35 //const

 36 const int N=130;

 37 const int INF=0x3f3f3f3f;

 38 const LL MOD=1000000007,STA=8000010;

 39 const LL LNF=1LL<<55;

 40 const double EPS=1e-9;

 41 const double OO=1e30;

 42 const int dx[4]={-1,0,1,0};

 43 const int dy[4]={0,1,0,-1};

 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

 45 //Daily Use ...

 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}

 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}

 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}

 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}

 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

 56 //End

 57 

 58 double f[8][N],p[N][N];

 59 int n;

 60 

 61 void dfs(int l,int r,int d)

 62 {

 63     if(l==r){f[d][l]=1;return;}

 64     int i,j,mid=(l+r)>>1;

 65     dfs(l,mid,d+1);

 66     dfs(mid+1,r,d+1);

 67     for(i=l;i<=mid;i++){

 68         f[d][i]=0;

 69         for(j=mid+1;j<=r;j++){

 70             f[d][i]+=f[d+1][i]*f[d+1][j]*p[i][j];

 71         }

 72     }

 73     for(i=mid+1;i<=r;i++){

 74         f[d][i]=0;

 75         for(j=l;j<=mid;j++){

 76             f[d][i]+=f[d+1][i]*f[d+1][j]*p[i][j];

 77         }

 78     }

 79 }

 80 

 81 int main(){

 82  //   freopen("in.txt","r",stdin);

 83     int i,j,k,tot;

 84     while(~scanf("%d",&n) && n!=-1)

 85     {

 86         tot=1<<n;

 87         for(i=0;i<tot;i++){

 88             for(j=0;j<tot;j++)

 89                 scanf("%lf",&p[i][j]);

 90         }

 91 

 92         dfs(0,tot-1,0);

 93         double hig=0;

 94         int w;

 95         for(i=0;i<tot;i++){

 96             if(f[0][i]>hig){hig=f[0][i],w=i+1;}

 97         }

 98         printf("%d\n",w);

 99     }

100     return 0;

101 }

 

你可能感兴趣的:(poj)