hdu4418 Time travel 期望+高斯

本文探讨了一个基于时间旅行的任务问题,通过概率论与算法结合的方式解决了一个特工如何利用故障的时间机器完成任务的问题。涉及数学期望计算及算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 
E[x] = sigma((E[x+i]+i) * p[i])(i∈[1, m])
(走i步经过i个点,所以是E[x+i]+i)
 
/*
题意:一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,
e(终点),s(起点),d(方向),求从s走到e经过的点数期望.
题意真是晦涩难懂,误导了我好久,纠结,痛苦。
设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0
n个点翻过去(除了头尾两个点)变为2*(n-1)个点
例如:6个点:012345  --->  0123454321
那么显然,从5开始向右走其实就是相当于往回走
然后方向就由两个状态转化成一个状态的,然后每个点就是只有一种状态了,对每个点建立方程高斯消元即可
bfs判断是否可以到达终点
建立方程:
E[i]=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+1*p[1]+2*p[2]……m*p[m]
*/
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<memory.h>
using namespace std;
int n,m,y,xx,d;
double p[105];
bool vis[210];
double a[210][210];
const double eps = 1e-12;
double x[230];
bool free_x[230];
void debug()
{
    for(int i=0;i<n;i++)
    for(int j=0;j<=n;j++)
    {
        printf("%.2lf ",a[i][j]);
        if(j==n) puts("");
    }
}
int sgn(double x)
{
    return (x>eps)-(x<-eps);
}
int gauss()
{
    int i, j, k;
    int max_r;
    int col;
    double temp;
    int free_x_num;
    int free_index;
    int equ = n,var = n;
    col = 0;
    memset(free_x,true,sizeof(free_x));
    for (k = 0; k < equ && col < var; k++, col++)
    {
        max_r = k;
        for (i = k + 1; i < equ; i++)
        {
            if (sgn(fabs(a[i][col]) - fabs(a[max_r][col]))>0) max_r = i;
        }
        if (max_r != k)
        {
            for (j = k; j < var + 1; j++) swap(a[k][j], a[max_r][j]);
        }
        if (sgn(a[k][col]) == 0 )
        {
            k--; continue;
        }
        for (i = k + 1; i < equ; i++)
        {
            if (sgn(a[i][col])!=0)
            {
                double t = a[i][col] / a[k][col];
                for (j = col; j < var + 1; j++)
                {
                    a[i][j] = a[i][j] - a[k][j] * t;
                }
            }
        }
    }
    for(i=k;i<equ;i++)
    if(sgn(a[i][col])!=0) {return 0;}
    if (k < var)
    {
        for (i = k - 1; i >= 0; i--)
        {
            free_x_num = 0;
            for (j = 0; j < var; j++)
            {
                if ( sgn(a[i][j])!=0 && free_x[j]){
                    free_x_num++, free_index = j;
                }
            }
            if(free_x_num>1)    continue;
            temp = a[i][var];
            for (j = 0; j < var; j++)
            {
                if (sgn(a[i][j])!=0 && j != free_index) temp -= a[i][j] * x[j];
            }
            x[free_index] = temp / a[i][free_index];
            free_x[free_index] = 0;
        }
        return var - k;
    }

    for (i = var - 1; i >= 0; i--)
    {
        temp = a[i][var];
        for (j = i + 1; j < var; j++)
        {
            if (sgn(a[i][j])!=0) temp -= a[i][j] * x[j];
        }
        x[i] = temp / a[i][i];
    }
    return 1;
}
int bfs()
{
    queue<int>qiqi;
    qiqi.push(xx);
    vis[xx]=true;
    while(!qiqi.empty())
    {
        int u=qiqi.front();
        qiqi.pop();
        for(int i=1;i<=m;i++)
        {
            int v=(u+i)%n;
            if(sgn(p[i])&&vis[v]==false)
            {
                vis[v]=true;
                qiqi.push(v);
            }
        }
    }
    if(vis[y]||vis[(n-y)%n]) return 1;
    else return 0;
}
void build()
{
        int i,j;
        double sum = 0;
        for (i = 1; i <=m;i++)
        sum += p[i]*i;
        memset(a, 0, sizeof a);
        for (i = 0; i < n; ++i)
        {
            a[i][i] = 1;
            if (!vis[i])
            {
                a[i][n] = 1e9;
                continue;
            }
            if (i==y||i==(n-y)%n)
            {
                a[i][n] = 0;
                continue;
            }
            a[i][n] = sum;
            int now = i;
            for (j = 1; j <=m; ++j)
            {
                now++;
                if (now==n) now = 0;
                a[i][now] -= p[j];
            }
        }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d",&n,&m,&y,&xx,&d);
        for(int i=1;i<=m;i++)
        {
            int g;
            scanf("%d",&g);
            p[i]=1.0*g/100;
        }
        if(y==xx) {puts("0.00");continue;}
        n=2*(n-1);
        if(d>0) xx=(n-xx)%n;
        memset(vis,false,sizeof(vis));
        if(!bfs()) {puts("Impossible !");continue;}
        build();
        //debug();
        int uu=gauss();
        if(uu==0)  {puts("Impossible !");continue;}
        printf("%.2lf\n",x[xx]);
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值