把两种状态化成2*n-2的一条线上的一种状态即可。很容易想到。

高斯列主元法,不知为什么WA。要上课了,不玩了。。。逃了一次课呢。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
double const eps=1e-8;
double G[210][210]; int n,m,s,e,d; double sum;
double pk[110];
double ans[210];
bool vis[210];
bool find(int i){
int k=i;
for(int j=i+1;j<n;j++){
if(fabs(G[j][i])>fabs(G[k][i]))
k=j;
}
if(fabs(G[k][i])<eps) return false;
for(int p=i;p<=n;p++)
swap(G[i][p],G[k][p]);
return true;
} bool Guass(){
for(int i=0;i<n;i++){
if(find(i)){
for(int j=i+1;j<n;j++){
double k=G[j][i]/G[i][i];
for(int p=i;p<=n;p++)
G[j][p]=G[j][p]-k*G[i][p];
}
}
else return false;
} ans[n-1]=G[n-1][n]/G[n-1][n-1];
for(int i=n-2;i>=0;i--){
double sum=0;
for(int j=n-1;j>i;j--){
sum+=(G[i][j]*ans[j]);
}
ans[i]=(G[i][n]-sum)/G[i][i];
}
return true;
} bool BFS(int s){
queue<int>q;
q.push(s);
memset(vis,false,sizeof(vis));
vis[s]=true;
while(!q.empty()){
int te=q.front();
q.pop();
for(int i=1;i<=m;i++){
if(pk[i]<eps)
continue;
int u=(te+i)%n;
if(!vis[u]){
vis[u]=true;
q.push(u);
if(u==e||u==n-e) return true;
}
}
}
return false;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d",&n,&m,&e,&s,&d);
sum=0;
n=2*n-2;
for(int i=1;i<=m;i++){
scanf("%lf",&pk[i]);
pk[i]/=100;
sum+=(i*pk[i]);
}
if (s == e){
puts ("0.00");
continue;
}
if(d==0) s=s;
else if(d==1) s=(n-s)%n;
memset(G,0,sizeof(G));
for(int i=0;i<n;i++){
G[i][i]=-1;
if(i==e||i==n-e) continue;
for(int j=1;j<=m;j++){
if(pk[j]<eps)
G[i][(j+i)%n]=pk[j];
}
G[i][n]=-sum;
}
if(!BFS(s)){
puts("Impossible !");
continue;
}
if(!Guass()){
puts("Impossible !");
}
else{
printf("%.2f\n",ans[s]);
}
}
return 0;
}

  

  这个是别人的

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.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;
}

  

HDU 4418 高斯消元法求概率DP的更多相关文章

  1. hdu 4418 Time travel 概率DP

    高斯消元求期望!! 将n时间点构成2*(n-1)的环,每一点的期望值为dp[i]=dp[i+1]*p1+dp[i+2]*p2+……+dp[i+m]*pm+1. 这样就可以多个方程,利用高斯消元求解. ...

  2. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

  3. HDU 5378 树上的概率DP Leader in Tree Land

    官方题解: 可以用求概率的思想来解决这个问题.令以i号节点为根的子树为第i棵子树,设这颗子树恰好有sz[i]个点.那么第i个点是第i棵子树最大值的概率为1/sz[i],不是最大值的概率为(sz[i]- ...

  4. hdu 4625 Dice(概率DP)

    Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  5. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  6. HDU - 5001 Walk(概率dp+记忆化搜索)

    Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...

  7. HDU 4418 Time travel 期望dp+dfs+高斯消元

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4418 Time travel Time Limit: 2000/1000 MS (Java/Othe ...

  8. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  9. HDU 4035Maze(树状+概率dp,绝对经典)

    题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-e ...

随机推荐

  1. CF899A Splitting in Teams

    CF899A Splitting in Teams 题意翻译 n个数,只有1,2,把它们任意分组,和为3的组最多多少 题目描述 There were nn groups of students whi ...

  2. Ubuntu14.04.1安装搜狗拼音输入法

    之前在Ubuntu16.04下安装过搜狗,在印象中与这次遇到的问题不一样,因此先说明一下这次Ubuntu的版本号: 参考博客http://blog.csdn.net/tao_627/article/d ...

  3. HDU 4196

    很容易由算术基本定理知道,完全平方数就是所有质因子指数为偶数的数.而求得N以下的质因子,可由前两篇的公式知,由N!与p的关系求得.对于指数为p的,用N!除去就可以,因为p必定属于N以内,且无重复. 至 ...

  4. Java关键字整理之一

    变量.函数.类的前面都可能会用到关键字,最常见的 private.public.protected.default 这四个修饰符的访问权限如下表: -------------------------- ...

  5. JavaScript 获取小数任一小数点后的位数的小数

    用Javascript取float型小数点后两位,例22.127456取成22.13,怎样做? 1.这样的方法最不推荐: function get(){ var s = 22.127456 + &qu ...

  6. android 虚拟按键是通过哪种机制上报的?

    1.在normal mode下,tp button也是和其他触摸事件一样,以坐标形式的input_event进行上报.在初始化时会通过tpd_button_setting()函数依据定义在tpd_cu ...

  7. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  8. listView 多个item布局

    package kds.szkingdom.wo.android.adapter; import java.util.List; import android.content.Context; imp ...

  9. BZOJ:3441 乌鸦喝水

    bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...

  10. js 回调函数小例子

    js 回调函数小例子 <script> //将函数作为另一个函数的参数 function test1(){ alert("我是test1"); } function t ...