码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5001 概率dp

时间:2015-09-04 21:01:38      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

                                                     Walk

                                                            Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Special Judge

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Source

2014 ACM/ICPC Asia Regional Anshan Online
 
题意:给你一个无向图,可以从任意点出发问你走d步的特定下,这n个点分别不能经过的概率
 
上代码:
技术分享
///1085422276
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b) scanf("%d%d",&a,&b)
#define mod 1000000007
#define inf 100000
#define maxn 300000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<0||ch>9)
    {
        if(ch==-)f=-1;
        ch=getchar();
    }
    while(ch>=0&&ch<=9)
    {
        x=x*10+ch-0;
        ch=getchar();
    }
    return x*f;
}
//******************************************************************
double dp[51][10005];
int n,m,D,vis[10005],jh;
vector<int >G[500];
void dfs(int x,int d)
{
    if(x==jh)return;
    int tmp=G[x].size();
   for(int i=0;i<tmp;i++)
   {
    dp[G[x][i]][d]+=dp[x][d-1]/(tmp);
   }
}
int main()
{

    int T=read();
    while(T--)
    {
        n=read(),m=read(),D=read();
        mem(dp);
        int a,b;
        FOR(i,1,n)G[i].clear();
        FOR(i,1,m)
        {
            READ(a,b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        FOR(i,1,n){
        mem(dp);   jh=i;
        FOR(j,1,n) dp[j][0]=1.0/n;
        FOR(j,1,D) FOR(k,1,n)dfs(k,j);
        double ans=0.0;
        FOR(j,0,D)ans+=dp[i][j];
        printf("%.10f\n",1-ans);
        }
    }
    return 0;
}
代码

 

HDU 5001 概率dp

标签:

原文地址:http://www.cnblogs.com/zxhl/p/4782249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!