[ACM] HDU 5001 walk (probability DP)

Source: Internet
Author: User

Walk
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
0.00000000000.00000000000.00000000000.00000000000.00000000000.69933179670.58642849520.44408608210.22758969910.42940745910.48510487420.48960188420.45250442500.34065674830.6421630037
 
Source2014 ACM/ICPC Asia Regional Anshan online


Solution:

The graph contains N vertices ranging from 1 to n, m bidirectional edges, and step d. At the beginning, a random vertex is selected as the start point, then, Step D is taken in the graph. A vertex is likely to walk to each of its adjacent points. A vertex can arrive multiple times, the probability (1 <= I <= N) that does not pass through vertex I in all paths is required ).

BFS was used during the competition, and DFS was written for a long time. It was not clear how the State was saved, whether to add or multiply it, and how to record each path... Struct is not expected to use the DP [] [] array to do it, ah...

The status is saved using DP [I] [J], and the DP [I] [J] is defined as the probability that step J will reach point I.

The requirement does not go through the probability of point I, that is, the probability of reaching other points. The "delete point" discussed by the experts in the QQ group (t is understood now ). How can we ensure that it does not pass through the I point? First, the starting point cannot start from the I point. Second, when a point is transferred to its adjacent point, it cannot reach the I point. Finally, DP [J] [d], (J! = I. After careful consideration, the second line has no effect. When J is transferred to its adjacent point, the probability of reaching each point is 1.0/g [J]. size () (vector <int> G [52], connected table), even if it reaches the I point, it does not affect the probability of reaching other adjacent points, in addition, it does not matter with DP [I] [] during the final addition.


Code:

# Include <iostream> # include <stdio. h> # include <vector> # include <string. h >#include <algorithm> # include <iomanip> using namespace STD; double DP [51] [10010]; // DP [I] [J] indicates the probability vector from step J to point I <int> G [51]; // The adjacent table double ans; int N, m, D; void CLR () {for (INT I = 1; I <= N; I ++) g [I]. clear () ;}int main () {int t; scanf ("% d", & T); While (t --) {CLR (); scanf ("% d", & N, & M, & D); int from, to; For (INT I = 1; I <= m; I ++) {scanf ("% d", & fr Om, & to); G [from]. push_back (to); G [to]. push_back (from) ;}for (INT I = 1; I <= N; I ++) // enumerate each vertex {ans = 0; memset (DP, 0, sizeof (DP); For (Int J = 1; j <= N; j ++) DP [J] [0] = 1.0/N; for (Int J = 1; j <= D; j ++) // enumeration steps {for (from = 1; from <= N; from ++) // Where to start? {If (from = I) continue; For (int K = 0; k <G [from]. size (); k ++) // where to go if (G [from] [k]! = I) // In fact, this sentence is not affected because even if I exists in the next vertex, the probability of reaching other vertices is also 1.0/g [from]. size () DP [G [from] [k] [J] + = DP [from] [J-1] * (1.0/g [from]. size () ;}for (Int J = 1; j <= N; j ++) if (J! = I) ans + = DP [J] [d]; // ans indicates the probability of passing through a vertex other than vertex I, that is, the probability cout <setiosflags (IOs: fixed) <setprecision (10) <ans <Endl ;}} return 0;} does not pass through the I point ;}

A piece of code written at the beginning is to calculate the probability of passing through the I point. The last 1 minus it is the request, but the sample can not pass .. For guidance.

Code:

# Include <iostream> # include <vector> # include <string. h> # include <iomanip> # include <stdio. h> using namespace STD; const int maxn = 51; double DP [maxn] [10002]; vector <int> G [maxn]; int t, n, m, D; int main () {scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M, & D); For (INT I = 0; I <= N; I ++) g [I]. clear (); int from, to; For (INT I = 1; I <= m; I ++) {scanf ("% d", & from, & to); G [from]. push_back (to); G [to]. push_back (from) ;}for (INT I = 1; I <= N; I ++) g [0]. push_back (I); For (INT I = 1; I <= N; I ++) // enumerate each vertex {memset (DP, 0, sizeof (DP )); double ans = 0.0; For (Int J = 1; j <= N; j ++) DP [J] [0] = 1.0/N; For (Int J = 1; j <= D; j ++) // number of steps taken {for (from = 0; from <= N; from ++) // where to start from {If (from = I) // do not start from the current enumeration point to continue; For (to = 0; to <G [from]. size (); To ++) // Where to go {DP [G [from] [to] [J] + = DP [from] [J-1] * 1.0/g [from]. size () ;}}ans + = DP [I] [J];} cout <setiosflags (IOs: fixed) <setprecision (10) <1-ans <Endl ;}} return 0 ;}



[ACM] HDU 5001 walk (probability DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.