HDU5001 -- Walk, mongoman
Problem DescriptionI used to think I cocould 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.
InputThe 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.
OutputFor 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
25 10 1001 22 33 44 51 52 43 52 51 41 310 10 101 22 33 44 55 66 77 88 99 104 9
Sample Output
Idea: enumeration paths do not include vertex I. Then, the transfer matrices a [I] [j] And a [j] [I] are both set to 0, and then the matrix is saved as a power.# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <string> # include <queue> # include <cmath> # include <map> # include <set> using namespace std; # define maxn 10080 double dp [maxn] [52]; int first [58]; int vv [maxn], nxt [maxn], du [maxn]; int e; void init () {e = 0; memset (first,-1, sizeof (first); memset (dp, 0, sizeof (dp); memset (du, 0, sizeof (du);} void addedge (int u, in T v) {vv [e] = v; nxt [e] = first [u]; first [u] = e ++; vv [e] = u; nxt [e] = first [v]; first [v] = e ++; du [u] ++; du [v] ++ ;} struct Matrix {double a [52] [52]; Matrix () {memset (a, 0, sizeof (a) ;}; Matrix MultiMul (Matrix a, Matrix B, int size) {Matrix ans; for (int I = 1; I <= size; I ++) {for (int j = 1; j <= size; j ++) {for (int k = 1; k <= size; k ++) {ans. a [I] [j] + =. a [I] [k] * B. a [k] [j] ;}} return ans;} Matrix P Ow (Matrix a, Matrix B, int size, int n) {Matrix ans = a; while (n) {if (n & 1) ans = MultiMul (ans, B, size); n> = 1; B = MultiMul (B, B, size);} return ans;} int main () {// freopen ("in.txt ", "r", stdin); int t; scanf ("% d", & t); while (t --) {int n, m, d; scanf ("% d", & n, & m, & d); init (); for (int I = 1; I <= m; I ++) {int u, v; scanf ("% d", & u, & v); addedge (u, v );} for (int I = 1; I <= n; I ++) // the probability that the vertex I is not included {Matrix ans; For (int j = 1; j <= n; j ++) if (j! = I) ans. a [1] [j] = 1. /n; for (int j = 1; j <= n; j ++) {if (j = I) continue; for (int k = first [j]; k! =-1; k = nxt [k]) {int v = vv [k]; if (v = I) continue;. a [j] [v] + = 1.0/du [j];} ans = Pow (ans, a, n, d); double fuckyou = 0; for (int j = 1; j <= n; j ++) if (j! = I) fuckyou + = ans. a [1] [j]; printf ("%. 10lf \ n", fuckyou) ;}} return 0 ;}