[Probability DP] HDU 5001 walk

Source: Internet
Author: User

Question: N points (1 ~ N), m edges walk K times, two-way edges, each random walk a path, the starting point is also random, and the probability of not going to each point is how much.

Ideas:

Probability DP [I] [J] [k] the probability that step I will walk to J less than K.

Then, state transfer is the point where J can reach. It transfers the upper probability of all DP [I] [J] [k] values.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "stack" # include "algorithm" # include "iostream" # include "vector" using namespace STD; # define n 300000 double DP [3] [55] [55]; // use the scrolling array int main () {int t; CIN> T due to memory problems; while (t --) {int n, m, D; scanf ("% d", & N, & M, & D ); vector <int> edge [55]; while (M --) {int A, B; scanf ("% d", & A, & B ); edge [A]. push_back (B); edge [B]. push_back (a);} memset (DP, 0, sizeof (DP); int I, J, K, L; for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {If (J! = I) DP [0] [I] [J] + = 1.0/N; // initialization, it is because the starting point is in I, so the probability of not going to J is all 1} for (I = 1; I <= D; I ++) {memset (DP [I % 2], 0, sizeof (DP [I % 2]); For (j = 1; j <= N; j ++) {for (k = 0; k <(INT) edge [J]. size (); k ++) {for (L = 1; L <= N; l ++) {If (J! = L & J! = Edge [J] [k]) DP [I % 2] [edge [J] [k] [l] + = DP [1-I % 2] [J] [l] * 1.0/edge [J]. size () ;}}} double ans [55]; memset (ANS, 0, sizeof (ANS); for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) if (I! = J) ans [I] + = DP [d % 2] [J] [I] ;}for (I = 1; I <= N; I ++) printf ("%. 10f \ n ", ANS [I]);} return 0 ;}


[Probability DP] HDU 5001 walk

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.