把N个点先转化为2*N-2个点。

比方说把012345转化成0123454321。

这样,就能够找出随意两两个点之间的关系。

然后依据关系能够得出来一个一元多项式的矩阵。

然后就用高斯消元求出矩阵就可以。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
#define eps 1e-6
#define zero(x) ((fabs(x)<eps?0:x))
#define maxn 220
double a[maxn][maxn];
int g[maxn],cnt;
int n,m,st,ed;
double p[maxn];
int guss(int n)
{
int r;
for(int i=0;i<n;i++)
{
r=i;
for(int j=i+1;j<n;j++)
{
if(fabs(a[j][i])>fabs(a[r][i]))r=j;
}
if(!zero(a[r][i]))return 0;
if(r!=i){
for(int j=0;j<=n;j++)
swap(a[i][j],a[r][j]);
}
for(int j=i+1;j<=n;j++)a[i][j]/=a[i][i];
a[i][i]=1.0;
for(int j=0;j<n;j++)
{
if(j==i)continue;
for(int k=i+1;k<=n;k++)
{
a[j][k]-=a[i][k]*a[j][i];
}
a[j][i]=0;
}
}
return 1;
}
void bfs()
{
queue<int>que;
while(!que.empty())que.pop();
que.push(st);
memset(g,-1,sizeof(g));
cnt=0;
g[st]=cnt++;
while(!que.empty())
{
int x=que.front();
que.pop();
for(int i=1;i<=m;i++)
{
if(!zero(p[i]))continue;
int y=(i+x)%n;
if(g[y]==-1)
{
g[y]=cnt++;
que.push(y);
}
}
}
}
int main()
{
int T,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&n,&m,&ed,&st,&d);
for(int i=1;i<=m;i++)
{
scanf("%lf",&p[i]);
p[i]=1.0*p[i]/100.0;
}
if(ed==st)
{
puts("0.00");
continue;
}
n=2*n-2;
if(d==1)st=n-st;
bfs();
if(g[ed]==-1&&g[n-ed]==-1){
puts("Impossible !");
continue;
}
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
if(g[i]==-1)continue;
if(i==ed||i==n-ed)
{
a[g[i]][g[i]]=1;
a[g[i]][cnt]=0;
continue;
}
a[g[i]][g[i]]=1.0;
for(int j=1;j<=m;j++)
{
int y=(i+j)%n;
if(g[y]==-1)continue;
a[g[i]][g[y]]-=p[j];
a[g[i]][cnt]+=1.0*j*p[j];
}
}
if(!guss(cnt))puts("Impossible !");
else printf("%.2lf\n",a[g[st]][cnt]);
}
return 0;
}

hdu-4418-Time travel-高斯+概率dp的更多相关文章

  1. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...

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

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

  3. HDU 5781 ATM Mechine (概率DP)

    ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  4. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu 3076 ssworld VS DDD (概率dp)

    ///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次 ...

  7. HDU 4336——Card Collector——————【概率dp】

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. hdu 4418 Time travel 概率DP

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

  9. HDU 4418 Time travel

    Time travel http://acm.hdu.edu.cn/showproblem.php?pid=4418 分析: 因为走到最后在折返,可以将区间复制一份,就变成了只往右走,01234321 ...

  10. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. c语言,结构体里面的函数

    以linux-3.2内核代码为例,结构体里面的函数的用法: 例,在某驱动文件中,定义了一个平台设备驱动: static struct platform_driver s3c24xx_led_drive ...

  2. 简单区分`:before`与`::before`的区别

    简单区分:before与::before的区别 :hover我们都知道,称作伪类,英文名pseudo-class,而我们此处提到的:before以及:after也是伪类,属于css2的内容,在ie8下 ...

  3. Android漫游记(4)---.so文件动态调试一例

    Android平台的动态调试一直以来是个困扰我等Coder的头疼问题,特别是对于本地的动态调试支持.能够说是"弱智"级别的,不知道Google的新版NDK和新出的Android S ...

  4. 在Ubuntu下编译Assimp库

    在Ubuntu下编译Assimp库 怎样在Ubuntu下编译Assimp库?这是我曾经编译成功后做的笔记,供參考. 1.去以下的站点去下载Assimp库: http://assimp.sourcefo ...

  5. UVA 322 ships (POJ 1138)

    题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. linux命令:find

    先上例子: find ./*_src -type f | xargs grep -ils "date" 在指定的那些文件夹下面,递归寻找包含“date” 字符串的普通文件. fin ...

  7. Windows Azure入门教学系列 (五):使用Queue Storage

    本文是Windows Azure入门教学的第五篇文章. 本文将会介绍如何使用Queue Storage.Queue Storage提供给我们一个云端的队列.我们可以用Queue Storage来进行进 ...

  8. JSP:JAVA Bean在JSP中的运用

    目录结构,如图: index.jsp <%@ page language="java" import="java.util.*" pageEncoding ...

  9. HTML语言简单回顾

    简单复习一下html语言. html的基本结构如下: <html> <head> <title></title> </head> <b ...

  10. 简易的sniffer程序

    真的非常简易,这个程序不过抓一些发送到本机的数据包,然后显示出来它们的一些信息罢了.      程序很easy!       #include <WinSock2.h> #include ...