HDU 4418 Time travel 概率dp+高斯消元

B - Time travel
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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

题目的大意是这样的

黑衣人在一个环上行走每次可以走【i~m】步每步的概率各不相同给你起点终点和最开始的方向求到终点的期望

使得E【x】是人从x走到e走的步数的期望值

那么 E【e】=0;

对于一般点 E【x】=sigma(E【x+i】+i)*p【i】)

我们把环转化为链然后建立方程 跑一下高斯消元就好了

但存在一种情况即终点不可达 那么先bfs跑一下判断是否能到终点就好了

参考bolg:http://972169909-qq-com.iteye.com/blog/1689107

ACcode:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#define eps 1e-8
#define maxn 444
using namespace std;
int equ,var;
double a[maxn][maxn],x[maxn];
int Gauss(){
    int k,col,max_r;
    for(k=0,col=0;k<equ&&col<var;k++,col++){
        max_r=k;
        for(int i=k+1;i<equ;++i)
            if(fabs(a[i][col])>fabs(a[max_r][col]))
                max_r=i;
            if(k!=max_r){
                for(int j=col;j<var;++j)
                    swap(a[k][j],a[max_r][j]);
                swap(x[k],x[max_r]);
            }
            x[k]/=a[k][col];
            for(int j=col+1;j<var;++j)a[k][j]/=a[k][col];
            a[k][col]=1;
            for(int i=0;i<equ;++i)
                if(i!=k){
                    x[i]-=x[k]*a[i][k];
                    for(int j=col+1;j<var;++j)a[i][j]-=a[k][j]*a[i][col];
                    a[i][col]=0;
                }
        }
    return 1;
}
int has[maxn],vis[maxn],k;
double p[maxn],sum;
int n,m,d,s,e;
int bfs(int u){
    memset(has,-1,sizeof(has));
    memset(a,0,sizeof(a));
    memset(vis,0,sizeof(vis));
    int v,flag=0;
    queue<int>q;
    k=0;
    has[u]=k++;
    q.push(u);
    while (!q.empty ()){
        u = q.front ();q.pop ();
        if (vis[u]) continue;
        vis[u] = 1;
        if (u==e||u==n-e){
            a[has[u]][has[u]] = 1;
            x[has[u]] = 0;
            flag = 1;
            continue;
        }
        a[has[u]][has[u]] = 1;
        x[has[u]] = sum;
        for (int i = 1; i <= m; i++){
           if (fabs(p[i])<eps) continue;
            v=(u+i) % n;
            if(has[v]==-1)has[v]=k++;
            a[has[u]][has[v]]-=p[i];
            q.push(v);
        }
    }
    return flag;
}
int main(){
    int loop;
    scanf("%d",&loop);
    while(loop--){
        scanf("%d%d%d%d%d",&n,&m,&e,&s,&d);
        sum=0;n=n*2-2;
        for(int i=1;i<=m;++i){scanf("%lf",&p[i]);p[i]/=100.0;sum+=p[i]*i;}
        if(s==e){puts("0.00");continue;}
        if(d>0)s=(n-s)%n;
        if(!bfs(s)){puts("Impossible !");continue;}
        equ=var=k;Gauss();
        printf("%.2lf\n",x[has[s]]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值