hdu4418——Time travel(概率DP+高斯消元)

8 篇文章 0 订阅
7 篇文章 0 订阅

Time travel

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


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

开始跟KB大神学的,没学明白,华农校赛碰到一道类似的,就去理解了一下这题,感觉懂多了。。

题意

一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,e(终点),s(起点),d(方向),求从s走到e经过的点数期望

分析

设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0
一般的:

E[x]=im((E[x+i]+i)p[i]) E[x]=∑im((E[x+i]+i)∗p[i])

(走i步经过i个点,所以是E[x+i]+i)

建立模型:高斯消元每个变量都是一个互不相同的独立的状态,由于人站在一个点,还有一个状态是方向!例如人站在x点,有两种状态向前、向后,不能都当成一种状态建立方程,所以要把两个方向化为一个方向从而使状态不受方向的影响
实现:
n个点翻过去(除了头尾两个点~~~)变为2*(n-1)个点,例如:
6012345>0123454321 6个点:012345−>0123454321
那么显然,从5开始向右走其实就是相当于往回走
然后方向就由两个状态转化成一个状态的,然后每个点就是只有一种状态了,对每个点建立方程高斯消元即可

bfs判断是否可以到达终点,顺便建立方程
参考KIDx的解题报告

KB大神代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;

#define eps 1e-9
const int MAXN=220;
double a[MAXN][MAXN],x[MAXN];//方程的左边的矩阵和等式右边的值,求解之后x存的就是结果
int equ,var;//方程数和未知数个数

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 t=que.front();
        que.pop();
        for(int i=1;i<=M;i++)
        {
            if(fabs(p[i])<eps)continue;//这点很重要,这个想到不能达到的点
            int temp=(t+i)%n;
            if(num[temp]==-1)
            {
                num[temp]=cnt++;
                que.push(temp);
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int s,e;
    int D;
    int 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 printf("Impossible !\n");
    }
    return 0;
}
还有两个我觉得写的很好
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <queue>  
  6. #include <algorithm>  
  7. #include <math.h>  
  8. using namespace std;  
  9. #define M 205  
  10. #define eps 1e-8  
  11. int equ, var;  
  12. double a[M][M], x[M];  
  13.   
  14. int Gauss ()  
  15. {  
  16.     int i, j, k, col, max_r;  
  17.     for (k = 0, col = 0; k < equ && col < var; k++, col++)  
  18.     {  
  19.         max_r = k;  
  20.         for (i = k+1; i < equ; i++)  
  21.             if (fabs (a[i][col]) > fabs (a[max_r][col]))  
  22.                 max_r = i;  
  23.         if (k != max_r)  
  24.         {  
  25.             for (j = col; j < var; j++)  
  26.                 swap (a[k][j], a[max_r][j]);  
  27.             swap (x[k], x[max_r]);  
  28.         }  
  29.         x[k] /= a[k][col];  
  30.         for (j = col+1; j < var; j++) a[k][j] /= a[k][col];  
  31.         a[k][col] = 1;  
  32.         for (i = 0; i < equ; i++) if (i != k)  
  33.         {  
  34.             x[i] -= x[k] * a[i][k];  
  35.             for (j = col+1; j < var; j++) a[i][j] -= a[k][j] * a[i][col];  
  36.             a[i][col] = 0;  
  37.         }  
  38.     }  
  39.     return 1;  
  40. }  
  41.   
  42. //has[x]表示人在x点时的变量号,因为我们只用可达状态建立方程,所以需要编号  
  43. int has[M], vis[M], k, e, n, m;  
  44. double p[M], sum;  
  45.   
  46. int bfs (int u)  
  47. {  
  48.     memset (has, -1, sizeof(has));  
  49.     memset (a, 0, sizeof(a));           //忘记初始化WA勒,以后得注意  
  50.     memset (vis, 0, sizeof(vis));  
  51.     int v, i, flg = 0;  
  52.     queue<int> q;  
  53.     q.push (u);  
  54.     k = 0;  
  55.     has[u] = k++;  
  56.     while (!q.empty ())  
  57.     {  
  58.         u = q.front ();  
  59.         q.pop ();  
  60.         if (vis[u]) continue;  
  61.         vis[u] = 1;  
  62.         if (u == e || u == n-e)     //终点有两个,你懂的~  
  63.         {  
  64.             a[has[u]][has[u]] = 1;  
  65.             x[has[u]] = 0;  
  66.             flg = 1;  
  67.             continue;  
  68.         }  
  69.         //E[x] = sum ((E[x+i]+i) * p[i])  
  70.         // ----> E[x] - sum(p[i]*E[x+i]) = sum(i*p[i])  
  71.         a[has[u]][has[u]] = 1;  
  72.         x[has[u]] = sum;  
  73.         for (i = 1; i <= m; i++)  
  74.         {  
  75.             //非常重要!概率为0,该状态可能无法到达,如果还去访问并建立方程会导致无解  
  76.             if (fabs (p[i]) < eps) continue;  
  77.             v = (u + i) % n;  
  78.             if (has[v] == -1) has[v] = k++;  
  79.             a[has[u]][has[v]] -= p[i];  
  80.             q.push (v);  
  81.         }  
  82.     }  
  83.     return flg;  
  84. }  
  85.   
  86. int main()  
  87. {  
  88.     int t, s, d, i;  
  89.     scanf ("%d", &t);  
  90.     while (t--)  
  91.     {  
  92.         scanf ("%d%d%d%d%d", &n, &m, &e, &s, &d);  
  93.         n = 2*(n-1);  
  94.         sum = 0;  
  95.         for (i = 1; i <= m; i++)  
  96.         {  
  97.             scanf ("%lf", p+i);  
  98.             p[i] = p[i] / 100;  
  99.             sum += p[i] * i;  
  100.         }  
  101.         if (s == e)  
  102.         {  
  103.             puts ("0.00");  
  104.             continue;  
  105.         }  
  106.         //一开始向左,起点要变  
  107.         if (d > 0) s = (n - s) % n;  
  108.         if (!bfs (s))  
  109.         {  
  110.             puts ("Impossible !");  
  111.             continue;  
  112.         }  
  113.         equ = var = k;  
  114.         Gauss ();  
  115.         printf ("%.2f\n", x[has[s]]);  
  116.     }  
  117.     return 0;  
  118. }  

#include<bits/stdc++.h>
using namespace std;  
#define M 205  
#define eps 1e-8  
int equ, var;  
double a[M][M], x[M];  
  
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 (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;  
}  
  
//has[x]表示人在x点时的变量号,因为我们只用可达状态建立方程,所以需要编号  
int has[M], vis[M], k, e, n, m;  
double p[M], sum;  
  
int bfs (int u)  
{  
    memset (has, -1, sizeof(has));  
    memset (a, 0, sizeof(a));           //忘记初始化WA勒,以后得注意  
    memset (vis, 0, sizeof(vis));  
    int v, i, flg = 0;  
    queue<int> q;  
    q.push (u);  
    k = 0;  
    has[u] = k++;  
    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;  
            flg = 1;  
            continue;  
        }  
        //E[x] = sum ((E[x+i]+i) * p[i])  
        // ----> E[x] - sum(p[i]*E[x+i]) = sum(i*p[i])  
        a[has[u]][has[u]] = 1;  
        x[has[u]] = sum;  
        for (i = 1; i <= m; i++)  
        {  
            //非常重要!概率为0,该状态可能无法到达,如果还去访问并建立方程会导致无解  
            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 flg;  
}  
  
int main()  
{  
    int t, s, d, i;  
    scanf ("%d", &t);  
    while (t--)  
    {  
        scanf ("%d%d%d%d%d", &n, &m, &e, &s, &d);  
        n = 2*(n-1);  
        sum = 0;  
        for (i = 1; i <= m; i++)  
        {  
            scanf ("%lf", p+i);  
            p[i] = p[i] / 100;  
            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 ("%.2f\n", x[has[s]]);  
    }  
    return 0;  
}  
这里有更多,哈哈=》=>hdu4418_百度搜索
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值