hdu4405(概率dp+并查集)

这题反而不难啊。。就是设个d[i]为i到走完的期望步数。。

然后d[i]从后6步转移过来就可以了。。

然后对跳跃情况就是用并查集维护,只能在跳转终点停留,所以只有对跳转终点i才有意义,所以在转移的时候得转移终点的d。。

发现挺多概率题都是逆向转移的,以后做概率题的时候得多尝试尝试。。。






/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include
#include
#include
#include
#include
#include
#include
#include
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<>1)
#define NM 100050
#define nm 7000000
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 



int n,m,_x,_y,f[NM];
double d[NM];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
	freopen("data.in","r",stdin);
	while(~scanf("%d%d",&n,&m)&&(n||m)){
		mem(d);
		inc(i,1,n+5)f[i]=i;
		inc(i,1,m){_x=read();_y=read();f[find(_x)]=find(_y);}
		dec(i,n-1,0)if(find(i)==i)
			inc(j,1,6)if(i+j






Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5168    Accepted Submission(s): 3253


Problem Description
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0
Please help Hzz calculate the expected dice throwing times to finish the game.
 

Input
There are multiple test cases.
Each test case contains several lines.
The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).
Then M lines follow, each line contains two integers Xi,Yi(1≤Xi The input end with N=0, M=0.
 

Output
For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.
 

Sample Input
 
   
2 0 8 3 2 4 4 5 7 8 0 0
 

Sample Output
 
   
1.1667 2.3441
 

Source
2012 ACM/ICPC Asia Regional Jinhua Online
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   6263  6262  6261  6260  6259 
 

Statistic |  Submit |  Discuss | Note

你可能感兴趣的:(DP,并查集)