hdu4418 Time travel

Posted wx62f0894128448

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4418 Time travel相关的知识,希望对你有一定的参考价值。


​http://www.elijahqi.win/archives/3616​​​
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.14 2.00
题面很毒瘤
钦定一个起点 询问 走到终点需要经过的点数的期望值是多少
由于这是一个环的形式且两个方向所以我们把它展开 然后这样方向就唯一了
那设dp[i]表示从i到终点经过点的期望 首先bfs 确定哪些点从起点并不能走到 然后列方程的时候注意给inf 即可 方程就很简单了dp[x]=sigma(i=1,i<=m) (dp[(x+i)%n]+i)*p[i] 然后把常数项提出来即可

#include<cmath>
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define eps 1e-9
using namespace std;
inline char gc()
static char now[1<<16],*S,*T;
if (T==S)T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;
return *S++;

inline int read()
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) if (ch==-) f=-1;ch=gc();
while(isdigit(ch)) x=x*10+ch-0,ch=gc();
return x*f;

const int N=220;
bool visit[N];int T,n,m,t,s,d;
double p[N],a[N][N],sum,ans[N];
inline void bfs()
queue<int>q;q.push(s);memset(visit,0,sizeof(visit));visit[s]=1;
while(!q.empty())
int x=q.front();q.pop();
for (int i=1;i<=m;++i)
int to=x+i;to%=n;if (visit[to])continue;
if (fabs(p[i])<eps) continue;visit[to]=1;q.push(to);



inline void gauss()
for (int i=0;i<n;++i)int now=i;
for (int j=i+1;j<n;++j) if(fabs(a[now][i])<fabs(a[j][i])) now=j;
if (now!=i) swap(a[now],a[i]);double t=a[i][i];
for (int j=i;j<=n;++j) a[i][j]/=t;
for (int j=i+1;j<n;++j)
t=a[j][i];
for (int k=i;k<=n;++k) a[j][k]-=a[i][k]*t;


for (int i=n-1;~i;--i)
for (int j=i+1;j<n;++j)
a[i][n]-=ans[j]*a[i][j];
ans[i]=a[i][n]/a[i][i];


int main()
freopen("hdu4418.in","r",stdin);
T=read();
while(T--)
n=read();m=read();t=read();s=read();d=read();sum=0;n=n-1<<1;
for (int i=1;i<=m;++i) p[i]=read(),p[i]/=100,sum+=i*p[i];
if (s==t) puts("0.00");continue;if (d==1) s=n-s;bfs();
if(!visit[t]&&!visit[n-t]) puts("Impossible !");continue;
memset(a,0,sizeof(a));
for (int i=0;i<n;++i)
a[i][i]=1;if (!visit[i]) a[i][n]=1e9;continue;
a[i][n]=sum;if(i==t||i==n-t) a[i][n]=0;continue;
for (int j=1;j<=m;++j) a[i][(i+j)%n]-=p[j];
gauss();printf("%.2f\\n",ans[s]);

return 0;


以上是关于hdu4418 Time travel的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4418 Time travel

HDOJ4418 Time travel

期望dp+高斯消元+bfs——hdu4418

龟兔赛跑(HDU2059)解题报告

HDU - 4284 Travel(floyd+状压dp)

hdu 2433 Travel (最短路树)