标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1480 Accepted Submission(s): 327
题意:一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,e(终点),s(起点),d(方向),求从s走到e经过的点数期望
解析:设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0
一般的:E[x] = sum((E[x+i]+i) * p[i])(i∈[1, m]) (走i步经过i个点,所以是E[x+i]+i)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <queue> 6 using namespace std; 7 8 const int maxn=205; 9 const double eps=1e-8; 10 int map[maxn],flag[maxn]; 11 double p[maxn],A[maxn][maxn]; 12 int cnt,n,m,st,ed,d; 13 int dcmp(double x) 14 { 15 if(fabs(x)<eps) return 0; 16 else if(x-0>eps) return 1; 17 return -1; 18 } 19 void swap(double &a,double &b){double t=a;a=b;b=t;} 20 21 bool bfs() 22 { 23 memset(flag,-1,sizeof(flag)); 24 queue<int>Q; 25 cnt=0;flag[st]=cnt++; 26 Q.push(st); 27 bool ret=false; 28 while(!Q.empty()) 29 { 30 int u=Q.front();Q.pop(); 31 for(int i=1;i<=m;i++) 32 { 33 int v=(u+i)%(2*n-2); 34 if(dcmp(p[i])==0) continue; 35 if(flag[v]!=-1) continue; 36 flag[v]=cnt++; 37 if(map[v]==ed) ret=true; 38 Q.push(v); 39 } 40 } 41 return ret; 42 } 43 44 void bulidmatrix() 45 { 46 memset(A,0,sizeof(A)); 47 for(int i=0;i<2*n-2;i++) 48 { 49 if(flag[i]==-1) continue; 50 int u=flag[i];A[u][u]=1; 51 if(map[i]==ed){A[u][cnt]=0;continue;} 52 for(int j=1;j<=m;j++) 53 { 54 int v=(i+j)%(2*n-2); 55 if(flag[v]==-1) continue; 56 v=flag[v]; 57 A[u][v]-=p[j];A[u][cnt]+=p[j]*j; 58 } 59 } 60 } 61 62 void gauss(int n) 63 { 64 int i,j,k,r; 65 for(i=0;i<n;i++) 66 { 67 r=i; 68 for(j=i+1;j<n;j++) 69 if(fabs(A[j][i])>fabs(A[r][i])) r=j; 70 if(dcmp(A[r][i])==0) continue; 71 if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]); 72 for(k=0;k<n;k++) if(k!=i) 73 for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j]; 74 } 75 } 76 77 int main() 78 { 79 int i,j,t; 80 scanf("%d",&t); 81 while(t--) 82 { 83 scanf("%d%d%d%d%d",&n,&m,&ed,&st,&d); 84 for(i=1;i<=m;i++){ scanf("%lf",p+i);p[i]/=100;} 85 if(st==ed){ printf("0.00\n");continue;} 86 for(i=0;i<n;i++) map[i]=i; 87 for(i=n,j=n-2;i<2*n-2;i++,j--) map[i]=j; 88 if(d==1) st=2*n-2-st; 89 if(!bfs()){ printf("Impossible !\n");continue;} 90 bulidmatrix();gauss(cnt); 91 for(i=cnt-1;i>=0;i--) 92 { 93 for(j=i+1;j<cnt;j++) 94 A[i][cnt]-=A[j][cnt]*A[i][j]; 95 A[i][cnt]/=A[i][i]; 96 } 97 printf("%.2lf\n",A[0][cnt]); 98 } 99 return 0; 100 }
hdu 4418 高斯消元求期望,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/xiong-/p/3910701.html