【HDU 4418】【概率DP 高斯消元】 Time travel 给出一个数轴,有一个起点和终点,某人可以走1-m步,每一种有一个概率,初始有一个方向,走到头则返回,问到达终点期望

传送门:HDU 4418

描述:

Time travel


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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5906  5905  5904  5903  5902 
题意:

给出一个数轴,有一个起点和一个终点,某个人可以走1,2,3……m步,每一种情况有一个概率,初始有一个方向,走到头则返回,问到达终点的期望步数为多少

思路:

高斯消元概率dp,dp[i]表示走到该点到达终点还需要的步数的期望。

那么有dp[i]=∑pk/100*(dp[i+k]+k)。

方程式很简单的,但是细节很多。

1. 只选择x能到达的点作为guass中的未知数。

比如这组hack的数据:

4 2 2 0 -1

0 100

答案应该为2,而高斯消元解出来的结果是no solution。原因就在于有些状态从s开始走是根本访问不到的,同时也访问不到e,也就是这些状态和整个markov过程根本无关。这些状态之间构成的方程无解就会导致整个解向量不存在,但并不代表x[s]不存在。解决的办法就是将这些状态的期望强制设为-1。判断某个状态能否访问到用bfs就可以了

       2. m可能大于n,所以在构建方程组时未知数的系数不能直接等于,要+=

       3.(pos,dir)二维展开成一维,虚拟出n-2个节点(除起点终点外其他点都有两个点),这样就不用考虑来回走了,

加上对称性,如果开始方向为1, 可以将起点e变为n-e,这样方向就不用处理了

       4.题意貌似说的有问题,D为-1的时候,和题目说的不一样.

      代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include<cmath>
#define eps 1e-9
using  namespace std;

const int maxn=220;
double a[maxn][maxn],x[maxn];//方程的左边的矩阵和等式右边的值,求解之后x存的就是结果
int equ,var;//方程数和未知数个数
/*
 *返回0表示无解,1表示有解
 */
int Gauss(){
  int i,j,k,col,max_r;
  for(k=0,col=0; k<equ&&col<var; k++,col++){
    max_r=k;
    for(i=k+1; i<equ; i++)
      if(fabs(a[i][col])>fabs(a[max_r][col]))
        max_r=i;
    if(fabs(a[max_r][col])<eps)return 0;
    if(k!=max_r){
      for(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(j=col+1; j<var; j++)a[k][j]/=a[k][col];
      a[k][col]=1;
    for(i=0; i<equ; i++)
      if(i!=k){
        x[i]-=x[k]*a[i][k];
        for(j=col+1; j<var; j++)a[i][j]-=a[k][j]*a[i][col];
          a[i][col]=0;
      }
  }
  return 1;
}

int num[maxn];
double p[maxn];
int cnt;
int n,N;//n=2*N-2
int M;
void bfs(int s){
  memset(num, -1, sizeof(num));
  queue<int>que;
  cnt=0;
  num[s]=cnt++;
  que.push(s);
  while(!que.empty()){
    int now=que.front();
    que.pop();
    for(int i=1; i<=M; i++){
      if(fabs(p[i])<eps)continue;//这点很重要,这个想到不能达到的点
      int tmp=(now+i)%n;
      if(num[tmp]==-1){
        num[tmp]=cnt++;
        que.push(tmp);
      }
    }
  }
}

int  main(){
  int s,e;
  int D,T;
  scanf("%d",&T);
  while(T--){
    scanf("%d%d%d%d%d",&N,&M,&e,&s,&D);
    for(int i=1; i<=M; i++)scanf("%lf",&p[i]),p[i]/=100;
    if(e==s){//这个特判一定需要,否则可能N==1,会被0除,RE
      printf("0.00\n");
      continue;
    }
    n=2*(N-1);
    if(D==1)s=n-s;
    bfs(s);
    if(num[e]==-1&&num[n-e]==-1){
      printf("Impossible !\n");
      continue;
    }
    equ=var=cnt;
    memset(a, 0, sizeof(a));
    memset(x, 0, sizeof(x));
    for(int i=0; i<n; i++){
      if(num[i]!=-1){
        if(i==e || i==n-e){
          a[num[i]][num[i]]=1;
          x[num[i]]=0;
          continue;
        }
        a[num[i]][num[i]]=1;
        for(int j=1; j<=M; j++){
          int t=(i+j)%n;
          if(num[t]!=-1){
            a[num[i]][num[t]]-=p[j];
            x[num[i]]+=j*p[j];
            }
        }
      }
    }
    if(Gauss())printf("%.2lf\n",x[num[s]]);
    else  puts("Impossible");
  }
  return 0;
}

//3
//4 2 0 1 0
//50 50
//4 1 0 2 1
//100
//4 2 2 0 -1
//0 100

PS:确实是一道好题,可能是本弱太菜啦_(:зゝ∠)_



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值