1. Title Description: Click to open the link
2. Problem-Solving ideas: The problem is solved by using matrix fast Power + probability DP. According to the test instructions can draw a state transfer diagram, according to the state transfer diagram is not difficult to get one-step transfer probability matrix, the next question is: How to solve the D-step (including D) can not go from other points to the node U probability.
First, since you cannot reach the node U, you cannot choose that point when you start . Secondly, in order to make the other nodes unable to reach the junction U, the probability of the one-step transfer probability matrix with the node U is all set to 0 . That is, the U node can not reach the other nodes, the other nodes can not reach the U node, which is equivalent to the U node from the state transfer diagram is temporarily deleted . Then, according to the knowledge of Markov chain, if the one-step transfer probability matrix is a, then the D-Step transfer probability matrix is a^d. Considering that the selection starting point also constitutes a matrix B, then the probability matrix of the final D step is b=b* (a^d). The b[i][j in the resulting B-matrix] is the probability that I will go through the D-step to J.
Here is a small trick, in order to facilitate the calculation of the B matrix, can be initialized at the time only the No. 0 row of each node is set to 1/n (node I is still 0), indicating that the node J is selected as the starting point of the probability, so that the final calculated B-matrix only in the No. 0 row has the result, the other points to the probability Only these probabilities can be summed to get the probability of reaching the other nodes beyond the node I, that is, the probability of not reaching node I.
The time complexity of the subject is O (N^4LOGD).
3. Code:
#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <cctype> #include <functional>using namespace std; #define ME (s) Memset (s,0,sizeof (s)) #define PB push_backtypedef Long long ll;typedef unsigned int uint;typedef unsigned long long Ull;typ Edef pair <int, int> p;const int n = 10000 + 10;int N, m, d;struct node{double v[55][55];void init () {memset (V, 0, si Zeof (v));} Node operator* (node R) {node ans;ans.init (); for (int i = 0, i < n; i++) for (int j = 0; J < N; j + +) for (int k = 0; K & Lt N k++) ans.v[i][j] + = v[i][k] * R.v[k][j];return ans;}} A Node operator^ (node m, int d) {node ans; Ans.init (); for (int i=0;i<n;i++) ans.v[i][i]=1; while (d>0) { if (d&1) ans=ans*m; M=m*m; d>>=1; } return ans; int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &n, &m, &d); int u, v;vector<int>g[n] ; for (int i = 0; i<m; i++) {scanf ("%d%d", &u, &v); U--;v--;g[u].push_back (v); G[v].push_back (u);} A.init (); for (int i = 0; i < n; i++) if (!g[i].empty ()) {int len = G[i].size (), for (int j = 0; j<len; j + +) {int id = g[ I][j];a.v[i][id] = (double) 1.0/len;//one-step transfer probability matrix}}for (int i=0;i<n;i++) {Node tmp=a; for (int j=0;j<n;j++) tmp.v[i][j]=tmp.v[j][i]=0;//the probability associated with node I is temporarily set at 0 node ans; Ans.init (); for (int j=0;j<n;j++) ans.v[0][j]=1.0/n; ans.v[0][i]=0; Do not depart from node I ans=ans* (tmp^d); Double res=0; for (int j=0;j<n;j++) res+=ans.v[0][j];//sum, the probability of the node I is not passed through the D step printf ("%.10lf\n", res); }}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU5001 Walk (2014 Anshan Division online game e)