poj 5001 Walk &&2014 ACM/ICPC Asia Regional Anshan Online 1005(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=5001

思路:dp计算出途径每个点的总概率,1-x即为所求解。

dp题,先介绍下dp[i][j]为第j步走在第i个点的概率,那么dp[i][j]=dp[x1][j-1]+dp[x2][j-1]+...,x1,x2为i 的相邻节点。上一步在相邻节点这一步才能走到该点嘛。

每个点概率要一个一个的算,当算到第ii个点时(没打错,ii个点与代码对应),从起点推起,起点嘛,算是第0步吧,每个点被选中几率1.0/n,直接计入ans,并将该状态记零,因为此部分概率已计入总概率,不用产生后继状态,防止重复计数。然后以每一步为一层,更新每个节点的概率,碰到所求的点ii 时将概率计入总数并该状态记零,同上,避免状态重复计算。如此即可。

代码(g++ %f不过,c++ %f %lf 都过,知道的同学路过请求告知):

#include<iostream>
#include<map>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=55;
const int maxm=3000;

int first[maxn],v[maxm],nex[maxm],out[maxn];
double dp[55][10005];
int ecnt;

void add_(int a,int b)
{
    out[a]++;
    v[ecnt]=b;
    nex[ecnt]=first[a];
    first[a]=ecnt++;
}
int main()
{
    int T,n,m,k,a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        ecnt=0;
        memset(out,0,sizeof out);
        memset(first,-1,sizeof first);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            add_(a,b);add_(b,a);
        }
        for(int j=1;j<=n;j++)dp[j][0]=1.0/n;
        for(int ii=1;ii<=n;ii++)
        {
            double ans=1.0/n;
            dp[ii][0]=0;
            for(int i=1;i<=k;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    dp[j][i]=0;
                    for(int e=first[j];~e;e=nex[e])
                        dp[j][i]+=(dp[v[e]][i-1]/out[v[e]]);
                    if(j==ii)ans+=dp[j][i],dp[j][i]=0;
                }
            }
            dp[ii][0]=1.0/n;
            printf("%.10f\n",1-ans);
        }
    }
    return 0;
}

Walk

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 154    Accepted Submission(s): 106
Special Judge


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

The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel to an adjacent node with the same probability in the next step. I will pick up the start node randomly (each node in the graph has the same probability.), and travel for d steps, noting that I may go through some nodes multiple times.

If I miss some sights at a node, it will make me unhappy. So I wonder for each node, what is the probability that my path doesn't contain it.
 

Input
The first line contains an integer T, denoting the number of the test cases.

For each test case, the first line contains 3 integers n, m and d, denoting the number of vertices, the number of edges and the number of steps respectively. Then m lines follows, each containing two integers a and b, denoting there is an edge between node a and node b.

T<=20, n<=50, n-1<=m<=n*(n-1)/2, 1<=d<=10000. There is no self-loops or multiple edges in the graph, and the graph is connected. The nodes are indexed from 1.
 

Output
For each test cases, output n lines, the i-th line containing the desired probability for the i-th node.

Your answer will be accepted if its absolute error doesn't exceed 1e-5.
 

Sample Input
      
      
2 5 10 100 1 2 2 3 3 4 4 5 1 5 2 4 3 5 2 5 1 4 1 3 10 10 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 4 9
 

Sample Output
      
      
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.6993317967 0.5864284952 0.4440860821 0.2275896991 0.4294074591 0.4851048742 0.4896018842 0.4525044250 0.3406567483 0.6421630037
 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值