首页 > 代码库 > hdu 4418 高斯消元求期望

hdu 4418 高斯消元求期望

Time travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1480    Accepted Submission(s): 327


Problem Description

Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by traveling through time with the Time machine. The Time machine can take agent K to some point (0 to n-1) on the timeline and when he gets to the end of the time line he will come back (For example, there are 4 time points, agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...). But when agent K gets into the Time machine he finds it has broken, which make the Time machine can‘t stop (Damn it!). Fortunately, the time machine may get recovery and stop for a few minutes when agent K arrives at a time point, if the time point he just arrive is his destination, he‘ll go and finish his mission, or the Time machine will break again. The Time machine has probability Pk% to recover after passing k time points and k can be no more than M. We guarantee the sum of Pk is 100 (Sum(Pk) (1 <= k <= M)==100). Now we know agent K will appear at the point X(D is the direction of the Time machine: 0 represents going from the start of the timeline to the end, on the contrary 1 represents going from the end. If x is the start or the end point of the time line D will be -1. Agent K want to know the expectation of the amount of the time point he need to pass before he arrive at the point Y to finish his mission.
If finishing his mission is impossible output "Impossible !" (no quotes )instead.
 

 

Input
There is an integer T (T <= 20) indicating the cases you have to solve. The first line of each test case are five integers N, M, Y, X .D (0< N,M <= 100, 0 <=X ,Y < 100 ). The following M non-negative integers represent Pk in percentile.
 

 

Output
For each possible scenario, output a floating number with 2 digits after decimal point
If finishing his mission is impossible output one line "Impossible !" 
(no quotes )instead.
 

 

Sample Input
2
4 2 0 1 0
50 50
4 1 0 2 1
100
 

 

Sample Output
8.142.00
 

 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online

题意:一个人在数轴上来回走,以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 }