题目链接

BZOJ4418

题解

题意:从一个序列上某一点开始沿一个方向走,走到头返回,每次走的步长各有概率,问走到一点的期望步数,或者无解

我们先将序列倍长形成循环序列,\(n = (N - 1) \times 2\)

按期望\(dp\)的套路,我们设\(f[i]\)为从\(i\)点出发到达终点的期望步数【一定要这么做,不然转移方程很难处理】,显然终点\(f[Y] = f[(n - Y) \mod n] = 0\)

剩余的点

\[f[i] = \sum\limits_{j = 1}^{M} p_j(f[(i + j) \mod n] + j)
\]

这是一个有后效性的转移方程,高斯消元即可

但还没完,有时候有些点是无法到达的,比如每次\(100 \%\)走两步时,恰好\(n\)又是奇数

这个时候这些点无解,但不代表终点无解

我们只需\(bfs\)一遍,强行将无法到达的点设为\(INF\)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define res register
#define eps 1e-9
using namespace std;
const int maxn = 205,maxm = 100005;
const double INF = 100000000000000000ll;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
double A[maxn][maxn],p[maxn],ans[maxn];
int n,N,M,Y,X,D,vis[maxn];
int q[maxn],head,tail;
bool bfs(){
for (res int i = 0; i < n; i++) vis[i] = false;
vis[X] = true; q[head = tail = 0] = X;
int u;
while (head <= tail){
u = q[head++];
for (res int i = 1; i <= M; i++)
if (p[i] > eps){
int to = ((u + i) % n + n) % n;
if (!vis[to]) vis[to] = true,q[++tail] = to;
}
}
for (int i = 0; i < n; i++) if (!vis[i]){
A[i][n] = INF,A[i][i] = 1;
}
return vis[Y] || vis[(n - Y) % n];
}
void pre(){
for (int i = 0; i < n; i++)
if (vis[i]){
A[i][i] = 1;
if (i == Y || i == (n - Y) % n) continue;
for (int j = 1; j <= M; j++){
int u = ((i + j) % n + n) % n;
A[i][u] += -p[j];
A[i][n] += p[j] * j;
}
}
}
bool gause(){
for (res int i = 0; i < n; i++){
int j = i;
for (res int k = i + 1; k < n; k++)
if (fabs(A[k][i]) > fabs(A[j][i])) j = k;
if (j != i) for (int k = i; k <= n; k++) swap(A[i][k],A[j][k]);
if (fabs(A[i][i]) < eps) return false;
for (res int j = i + 1; j < n; j++){
double t = A[j][i] / A[i][i];
for (res int k = i; k <= n; k++)
A[j][k] -= A[i][k] * t;
}
}
for (res int i = n - 1; ~i; i--){
for (res int j = i + 1; j < n; j++)
A[i][n] -= A[i][j] * ans[j];
if (fabs(A[i][i]) < eps) return false;
ans[i] = A[i][n] / A[i][i];
}
return true;
}
int main(){
int T = read();
while (T--){
N = read(); M = read(); Y = read(); X = read(); D = read();
n = 2 * (N - 1);
if (D == -1){
if (X == 0) D = 0;
else D = 1;
}
if (D >= 1) X = (n - X) % n;
for (int i = 1; i <= M; i++)
p[i] = read() / 100.0;
if (X == Y){puts("0.00"); continue;}
for (res int i = 0; i < n; i++)
for (res int j = 0; j <= n; j++)
A[i][j] = 0;
if (!bfs()) {puts("Impossible !"); continue;}
pre();
if (!gause() || ans[X] >= INF)
puts("Impossible !");
else printf("%.2lf\n",ans[X]);
}
return 0;
}

hdu4418 Time travel 【期望dp + 高斯消元】的更多相关文章

  1. HDU4418 Time travel(期望dp 高斯消元)

    题意 题目链接 Sol mdzz这题真的太恶心了.. 首先不难看出这就是个高斯消元解方程的板子题 \(f[x] = \sum_{i = 1}^n f[to(x + i)] * p[i] + ave\) ...

  2. HDU-4418 Time travel 概率DP,高斯消元

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:简单来说就是给你1个环(n - 1 , n - 2 …… 0 ,1 , 2 , 3 …… ...

  3. BZOJ_3143_[Hnoi2013]游走_期望DP+高斯消元

    BZOJ_3143_[Hnoi2013]游走_期望DP+高斯消元 题意: 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机 ...

  4. 期望dp+高斯消元+bfs——hdu4418

    高斯消元又弄了半天.. 注意只要能建立矩阵,那就必定有解,所以高斯消元里可以直接return 1 #include<bits/stdc++.h> using namespace std; ...

  5. HDU 2262 Where is the canteen 期望dp+高斯消元

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2262 Where is the canteen Time Limit: 10000/5000 MS ...

  6. 【noi2019集训题1】 脑部进食 期望dp+高斯消元

    题目大意:有n个点,m条有向边,每条边上有一个小写字母. 有一个人从1号点开始在这个图上随机游走,游走过程中他会按顺序记录下走过的边上的字符. 如果在某个时刻,他记录下的字符串中,存在一个子序列和S2 ...

  7. LightOJ 1151 Snakes and Ladders 期望dp+高斯消元

    题目传送门 题目大意:10*10的地图,不过可以直接看成1*100的,从1出发,要到达100,每次走的步数用一个大小为6的骰子决定.地图上有很多个通道 A可以直接到B,不过A和B大小不确定   而且 ...

  8. P4457-[BJOI2018]治疗之雨【期望dp,高斯消元】

    正题 题目链接:https://www.luogu.com.cn/problem/P4457 题目大意 开始一个人最大生命值为\(n\),剩余\(hp\)点生命,然后每个时刻如果生命值没有满那么有\( ...

  9. ZJUT 1423 地下迷宫(期望DP&高斯消元)

    地下迷宫 Time Limit:1000MS  Memory Limit:32768K Description: 由于山体滑坡,DK被困在了地下蜘蛛王国迷宫.为了抢在DH之前来到TFT,DK必须尽快走 ...

随机推荐

  1. jQuery(四)--HTTP请求

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. elasticsearch搜索引擎搭建

    在该路径下,运行elasticsearch.bat该命令,后面访问127.0.0.1:9200 出现如下界面说明启动成功 elasticsearch-head操作elasticsearch的图形界面, ...

  3. C语言常用关键语法精华总结

    1.关于typedef的用法总结 2.typedef struct的用法 3.typedef函数指针用法 4.数组指针(数组类型的指针)与指针数组 5.真正明白c语言二级指针 6.C语言for循环(及 ...

  4. linux文件操作篇 (四) 目录操作

    #include <sys/stat.h>#include <unistd.h>#include <dirent.h> //创建文件夹 路径 掩码 int mkdi ...

  5. C# 测量程序运行时间

    using System.Diagnostics; Stopwatch watch = new Stopwatch(); watch.Start(); /* 需要测量运行时间的程序 */ watch. ...

  6. 创龙DSP6748开发板驱动LCD屏

    1. DSP6748内部有2个LCD控制器,Raster Controller 光栅控制器和the LCD Interface Display Driver (LIDD) controller 控制器 ...

  7. asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文件接口

    FormItem类 public class FormItem { public string Name { get; set; } public ParamType ParamType { get; ...

  8. 从浏览器或者Webview 中唤醒APP

    本文来自网易云社区 作者:刘新奇 移动互联时代,很多互联网服务都会同时具备网站以及移动客户端,很多人认为APP的能帮助建立更稳固的用户关系,于是经常会接到各种从浏览器.webview中唤醒APP的需求 ...

  9. (干货分享)mac python+appium环境搭建

    因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...

  10. Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

    作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...