zoukankan      html  css  js  c++  java
  • HDU 4405

    简单的概率DP,求期望,逆推更好。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define N 100005
    using namespace std;
    
    double p[N];
    
    struct e{
    	int u,v;
    	int next;
    }edge[1010];
    int head[N],tot;
    
    void addedge(int u,int v){
    	edge[tot].u=u;
    	edge[tot].v=v;
    	edge[tot].next=head[u];
    	head[u]=tot++;
    }
    
    int findx(int p){
    	int ret=p;
    	for(int ei=head[p];ei!=-1;ei=edge[ei].next){
    		ret=edge[ei].v;
    	}
    	return ret;
    }
    
    int main(){
    	int n,m,u,v,pos;
    	while(scanf("%d%d",&n,&m),n||m){
    		tot=0;
    		memset(head,-1,sizeof(head));
    		for(int i=1;i<=m;i++){
    			scanf("%d%d",&u,&v);
    			addedge(u,v);
    		}
    		p[n]=0;
    		for(int i=n-1;i>=0;i--){
    			p[i]=0;
    			if(head[i]!=-1){
    				p[i]=p[findx(i)];
    			}
    			else{
    				for(int k=1;k<=6;k++){
    					pos=i+k>n?n:i+k;
    					if(head[pos]!=-1){
    						p[i]+=p[findx(pos)]/6.0;
    					}
    					else{
    						p[i]+=p[pos]/6.0;
    					}
    				}
    				p[i]+=1;
    			}
    		}
    		printf("%.4lf
    ",p[0]);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Tomcat安装与配置
    模板方法模式
    观察者模式
    访问者模式
    策略模式
    迭代器模式
    状态模式
    访问者模式
    备忘录模式
    解释器模式
  • 原文地址:https://www.cnblogs.com/jie-dcai/p/4101097.html
Copyright © 2011-2022 走看看