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.
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.14
2.00
2.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)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std; const int maxn=;
const double eps=1e-;
int map[maxn],flag[maxn];
double p[maxn],A[maxn][maxn];
int cnt,n,m,st,ed,d;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
else if(x->eps) return ;
return -;
}
void swap(double &a,double &b){double t=a;a=b;b=t;} bool bfs()
{
memset(flag,-,sizeof(flag));
queue<int>Q;
cnt=;flag[st]=cnt++;
Q.push(st);
bool ret=false;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=;i<=m;i++)
{
int v=(u+i)%(*n-);
if(dcmp(p[i])==) continue;
if(flag[v]!=-) continue;
flag[v]=cnt++;
if(map[v]==ed) ret=true;
Q.push(v);
}
}
return ret;
} void bulidmatrix()
{
memset(A,,sizeof(A));
for(int i=;i<*n-;i++)
{
if(flag[i]==-) continue;
int u=flag[i];A[u][u]=;
if(map[i]==ed){A[u][cnt]=;continue;}
for(int j=;j<=m;j++)
{
int v=(i+j)%(*n-);
if(flag[v]==-) continue;
v=flag[v];
A[u][v]-=p[j];A[u][cnt]+=p[j]*j;
}
}
} void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(dcmp(A[r][i])==) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&m,&ed,&st,&d);
for(i=;i<=m;i++){ scanf("%lf",p+i);p[i]/=;}
if(st==ed){ printf("0.00\n");continue;}
for(i=;i<n;i++) map[i]=i;
for(i=n,j=n-;i<*n-;i++,j--) map[i]=j;
if(d==) st=*n--st;
if(!bfs()){ printf("Impossible !\n");continue;}
bulidmatrix();gauss(cnt);
for(i=cnt-;i>=;i--)
{
for(j=i+;j<cnt;j++)
A[i][cnt]-=A[j][cnt]*A[i][j];
A[i][cnt]/=A[i][i];
}
printf("%.2lf\n",A[][cnt]);
}
return ;
}
總結
以上是生活随笔為你收集整理的hdu 4418 高斯消元求期望的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机油子是什么虫?
- 下一篇: Json数据产生树形结构