当前位置: 代码迷 >> 综合 >> HDU P5001 Walk
  详细解决方案

HDU P5001 Walk

热度:63   发布时间:2023-12-21 07:44:47.0

2014区域网络赛鞍山校区上的第五题:


Walk

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 183    Accepted Submission(s): 127
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

题意大致为一张双向图相同概率一个点进入,任意走,问走完d步所有点没走过的概率。要是你欲先求出所有点能走过的概率,然后1-它输出的话,你就走远了。。。

正解应该是刚开始每个点1/n的初始概率,然后删除I点,之后求出其余点能走到的概率,能走到的概率总和即I点不能走到的概率,然后注意到任意两点直接走到的概率可以形成一个矩阵,走了d步相当于初始概率形成的矩阵*概率矩阵^d。 故可以用矩阵快速幂来做。


由于时限较宽,其余方法还有很多,若暂时看不懂本题含义,可以先去看这道题的概率DP题解,其实思想是一样的。


代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
int n,m,d;
vector<int> eg[100];
struct matrix{double a[100][100];
}pan,st,npan;
matrix mul(matrix a1,matrix a2,int i1,int i2){int i,j,k;matrix c;rep(i,i1)rep(j,i2){c.a[i][j]=0;rep(k,i2){c.a[i][j]+=a1.a[i][k]*1.0*a2.a[k][j];}}return c;
}
void qmi(int nn){int j,k;while(nn){if(nn%2==1) st=mul(st,npan,1,n);npan=mul(npan,npan,n,n);nn=(nn>>1);	}
}
int main()
{int i,j,k,T;scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&d);rep(i,n) eg[i].clear();MM(pan.a,0);rep(i,m){int s,e;scanf("%d%d",&s,&e);eg[s].push_back(e);eg[e].push_back(s);}rep(i,n){int sz=eg[i].size();for(j=0;j<sz;j++) pan.a[i][eg[i][j]]=1.0/sz;	}rep(i,n){double res=0;rep(j,n) st.a[1][j]=1.0/n;MM(npan.a,0);rep(j,n)rep(k,n)if(j!=i && k!=i) npan.a[j][k]=pan.a[j][k];qmi(d);rep(j,n) res+=st.a[1][j];printf("%.8lf\n",res);}}return 0;
}