hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021
http://acm.hdu.edu.cn/showproblem.php?pid=4418
读了一遍题后大体明白意思,但有些细节不太确定。就是当它处在i点处,它有1~m步可以走,但他走的方向不确定呢。后来想想这个方向是确定的,就是他走到i点的方向,它会继续朝着这个方向走,直到转向回头。
首先要解决的一个问题是处在i点处,它下一步该到哪个点。为了解决方向不确定的问题,将n个点转化为2*(n-1)个点。例如当n=4时由原来的0123变为012321,它对应的编号为012345,这样就不用管它哪个方向,统一处理了,当向前走k步时即(i+k)%n。
然后设出dp[i]表示在i点处时到达终点的期望步数,那么可列出转移方程dp[i] = ∑( pk * (dp[ (x+k)%n ] +k) )。但是有些点是永远无法到达的,因此先bfs出所有到达的点,然后列出方程组解方程。
有许多注意的点,判断p[i]是否为0等。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
//#define LL __int64
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ; double p[maxn];
double a[maxn][maxn];//增广矩阵
double X[maxn];//解集
int num[maxn];//给每个能给到达的point离散化
int n,m,x,y,d;
int equ,var,cnt; bool Gauss()
{
int row,col,max_r,i,j;
row = ;
col = ;
while(row < equ && col < var)
{
max_r = row;
for(i = row+; i < equ; i++)
{
if(fabs(a[i][col]) > fabs(a[max_r][col]))
max_r = i;
}
if(max_r != row)
{
for(j = col; j <= var; j++)
swap(a[row][j], a[max_r][j]);
}
if(fabs(a[row][col]) < eps)
{
col++;
continue;
}
for(i = row+; i < equ; i++)
{
if(fabs(a[i][col]) < eps) continue;
double t = a[i][col]/a[row][col];
a[i][col] = 0.0;
for(j = col+; j <= var; j++)
a[i][j] -= a[row][j]*t;
}
row++;
col++;
} for(i = row; i < equ; i++)
if(fabs(a[i][var]) > eps)
return false; //无解
for(i = equ-; i >= ; i--)
{
if(fabs(a[i][i]) < eps) continue;
double t = a[i][var];
for(j = i+; j < var; j++)
t -= a[i][j]*X[j];
X[i] = t/a[i][i];
}
return true;
} void bfs(int s) //bfs找出所有能够到达的点并离散化
{
queue <int> que;
que.push(s);
num[s] = cnt++;
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i = ; i <= m; i++)
{
if(fabs(p[i]) < eps)
continue;
int v = (u+i)%n;
if(num[v] == -)
{
num[v] = cnt++;
que.push(v);
}
}
}
} int main()
{
int test;
scanf("%d",&test);
for(int item = ; item <= test; item++)
{
scanf("%d %d %d %d %d",&n,&m,&y,&x,&d);
for(int i = ; i <= m; i++)
{
scanf("%lf",&p[i]);
p[i] /= ;
}
if(x == y)
{
printf("0.00\n");
continue;
}
n = *(n-);
if(d == )
x = n-x;
memset(num,-,sizeof(num));
cnt = ;
bfs(x);
if(num[y] == - && num[n-y] == -) //注意这里是 &&,只有当两个方向都走不到才算走不到
{
printf("Impossible !\n");
continue;
} memset(a,,sizeof(a));
memset(X,,sizeof(X));
equ = var = cnt; for(int i = ; i < n; i++)
{
if(num[i] != -)
{
if(i == y || i == n-y) //注意特判终点
{
a[num[i]][num[i]] = ;
a[num[i]][cnt] = ;
continue;
}
a[num[i]][num[i]] = ;
for(int j = ; j <= m; j++)
{
int t = (i+j)%n;
if(num[t] != -)
a[num[i]][num[t]] -= p[j];
a[num[i]][cnt] += j*p[j];
}
}
}
if(Gauss())
printf("%.2lf\n",X[num[x]]);
else printf("Impossible !\n");
}
return ;
}
hdu 4481 Time travel(高斯求期望)(转)的更多相关文章
- [ACM] hdu 4418 Time travel (高斯消元求期望)
Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...
- HDU 5245 Joyful(概率题求期望)
D - Joyful Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- ZJUT 地下迷宫 (高斯求期望)
ShowID=1423">http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID=1423 设dp[i]表示在i点时到达终点要走的期望步数,那么d ...
- HDU 3853 LOOP (概率DP求期望)
D - LOOPS Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDU 4418 Time travel 期望dp+dfs+高斯消元
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4418 Time travel Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- HDU 5159 Card (概率求期望)
B - Card Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- hdu 4418 Time travel 概率DP
高斯消元求期望!! 将n时间点构成2*(n-1)的环,每一点的期望值为dp[i]=dp[i+1]*p1+dp[i+2]*p2+……+dp[i+m]*pm+1. 这样就可以多个方程,利用高斯消元求解. ...
随机推荐
- java日志组件介绍(common-logging,log4j,slf4j,logback )
转自:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging是apache提供的一个通用的日志 ...
- PHP加密技术
一.MD5加密 直接干,这里以一个登录页面为例: <?php require_once 'config/database.config.php'; $act=$_REQUEST['act']; ...
- There is no getter for property named 'useName' in 'class cn.itcast.mybatis.pojo.User'
org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: org.apache.i ...
- ESLint规则配置说明
"no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- css一些进入条
http://www.jq22.com/jquery-info5309 http://www.jq22.com/jquery-info10964 http://www.jq22.com/jquery- ...
- 详解C#中的反射
反射(Reflection) 2008年01月02日 星期三 11:21 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B ...
- Java_jvisualvm使用JMX连接远程机器(实践)
https://my.oschina.net/heroShane/blog/196227 一.启动普通的jar程序 1.执行foo.jar启动命令 java -Dcom.sun.management. ...
- OpenStack从入门到放弃
OpenStack从入门到放弃 目录: 为何选择云计算/云计算之前遇到的问题 什么是云计算 云服务模式 云应用形式 传统应用与云感知应用 openstack及其相关组件介绍 flat/vlan/gre ...
- django 缓存、中间件、信号、CSRF 详解
中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...