期望DP HDU4418-Time travel

博客介绍了如何应用期望动态规划解决HDU4418-Time travel问题,通过扩展数轴、广度优先搜索找到有效节点,并利用高斯消元方法求解双向状态转移的期望路程。

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

一般情况下,动态规划转移方程是单向的。期望DP中,若状态转移不是单向的,即状态之间相互影响。则使用解方程的思想,将状态设为变量,将状态转移方程转化为变量方程,构造矩阵。通过高斯消元求解。

题意

人在0到n-1的数轴上来回移动,给定起始点x,终止点y,初始方向d,每步移动k个单位的概率pk。求到达终止点的期望路程。

分析

  1. 首先解决来回移动的问题,将数轴扩展到0到2n-2。大于n-1的节点i表示在原位置2n-2-i反向移动。
  2. 然后,用bfs选择从起始点可达的有效节点。
  3. 再然后,将从有效节点到达终止点的期望设为变量,并根据状态转移方程为每个节点构建一个方程,构成一个方阵。
  4. 最后,高斯消元求解变量值。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_N = 100;
const double eps = 1e-10;
double p[MAX_N+10];
int tot;                                // 有意义的位置 | 0 ~ tot | tot代表常数项
int h[2*MAX_N+10];                      // 映射到的位置
double res[2*MAX_N+10];                 // 存放结果
double mat[2*MAX_N+10][2*MAX_N+10];     // 矩阵
int n, m, y, x, d;

// 可以抵达的位置
bool bfs()
{
    queue<int> que;
    que.push(x);
    memset(h, -1, sizeof(h));
    tot = 0;
    h[x] = tot++;
    bool flag = false;
    while (!que.empty())
    {
        int pos = que.front(); que.pop();
        for (int i = 1; i <= m; i++)
        {
            if (p[i] > eps)
            {
                int next_pos = (pos+i) % n; 
                if (next_pos == y || next_pos == n-y) flag = true;
                else if (h[next_pos] < 0)
                {
                    h[next_pos] = tot++;
                    que.push(next_pos);
                }
            } 
        }
    }
    return flag;
}

// 构建矩阵
void build()
{
    memset(mat, 0, sizeof(mat));
    for (int i = 0; i < n; i++)
    {
        if (h[i] >= 0)
        {
            mat[h[i]][h[i]] = 1;
            for (int j = 1; j <= m; j++)
            {
                if (p[j] > eps)
                {
                    if (h[(i+j)%n] >= 0) mat[h[i]][h[(i+j)%n]] -= p[j];
                    mat[h[i]][tot] += 1.0*j*p[j];
                }
            }
        }
    }
}

// 矩阵求解
void solve()
{
    for (int i = 0; i < tot; i++)
    {
        for (int j = 0; j < tot; j++)
        {
            if (i == j) continue;
            double mul = mat[j][i]/mat[i][i];
            for (int k = 0; k <= tot; k++)
                mat[j][k] -= mul * mat[i][k];
        }
    }
    for (int i = 0; i < tot; i++) 
        res[i] = mat[i][tot] / mat[i][i];
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t-->0)
    {
        scanf("%d%d%d%d%d", &n, &m, &y, &x, &d);
        // 处理成线性
        n = 2*n - 2;
        for (int i = 1; i <= m; i++) 
        {
            scanf("%lf", &p[i]);
            p[i] /= 100;
        }
        if (x == y) 
        {
            printf("0.00\n");
            continue;
        }
        else 
        {
            if (d == 1) x = (n - x) % n;
            if (!bfs()) printf("Impossible !\n");
            else 
            {
                build();
                solve();
                printf("%.2lf\n", res[h[x]]);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值