[Day2 for provinces in Sichuan 04-flood] State compression DP

Source: Internet
Author: User

Question:

CountryN(N<= 8) small village, usedMTwo-way roads are connected, and each two village can be directly connected by one road. In the recent village flood disaster recovery, the flood has caused these roads to be flooded with a certain probability, and whether these roads are drowned is mutual.Independent. Your task is to calculate the probability that all villages can be connected together (that is, from any village to all other villages can go through an unflooded road.

Analysis: n = 8. Obviously, it is neither bare search nor status compression DP. It is also very difficult to perform bare search, so the direction of this question will come out.

Algorithm:

Note that f [I] indicates the probability of all vertex connections in the I state. If you directly divide the vertex to be selected into two sets, it will calculate the weight.

To avoid weighting, We need to specify something like this: first isolate the first vertex in the selected set and then enumerate the connectivity of other vertices, the connected blocks formed by other points cannot be directly connected. They can only be connected to the isolated point,

We need to set up a DFS to search for connectivity between other points. We are surprised to find that the big process is a small process, and the small process can also call the big process, in addition, the local variables of the large process are full variables of the small process. However, the local variables cannot be monitored during debugging, which makes me very depressed for a long time. After the examination, I came up with a solution ..

Most probability calculations are far behind this question. It took me more than an hour to know how the sample was made...

The data size of this question is really small. I am O (2 ^ N * n! * N ^ 2) actually all 0 ms, haha.

Program syj; {DP} <br/> var n, m, all, I, J, K, K1, K2: longint; <br/> A: array [1 .. 8, 1 .. 8] of real; <br/> F: array [0 .. 1 <8] of real; <br/> BB: array [0 .. 8] of real; <br/> G: array [1 .. 8, 1 .. 8] of Boolean; <br/> procedure DFS (I: longint); <br/> var Jj: longint; <br/> B: array [0 .. 8] of longint; <br/> procedure find (J, K: longint); <br/> var l, ll, LJ, lk: longint; T1, T2: real; <br/> begin <br/> If J = 0 then begin <br/> T1: = 1; <br/> for L: = 1 to B [0] Do BB [l]: = B [l]; <br/> for L: = 1 to B [0] Do begin <br/> DFS (B [l]); <br/> J: = B [l]; LL: = 1; t2: = 1; <br/> while j> 0 do begin <br/> If Odd (j) then T2: = T2 * A [JJ, ll]; <br/> J: = j> 1; Inc (LL); <br/> end; <br/> T1: = T1 * (1-t2) * f [B [l]; <br/> end; <br/> for L: = 1 to B [0]-1 do <br/> for LL: = L + 1 to B [0] Do begin <br/> J: = B [l]; LJ: = 1; t2: = 1; <br/> while j> 0 do begin <br/> If Odd (j) then begin <br/> K: = B [ll]; LK: = 1; <br/> while K> 0 do begin <br/> If Odd (k) then T2: = T2 * A [LJ, lk]; <br/> K: = k> 1; Inc (lk); <br/> end; <br/> J: = j> 1; Inc (LJ ); <br/> end; <br/> T1: = T1 * t2; <br/> end; <br/> F [I]: = f [I] + T1; <br/> exit; <br/> end; <br/> while not odd (j) Do begin <br/> J: = j> 1; INC (k); <br/> end; <br/> for L: = 1 to B [0] Do begin <br/> LL: = B [l]; B [l]: = B [l] Or (1 <(k-1); <br/> Find (j> 1, k + 1 ); <br/> B [l]: = ll; <br/> end; <br/> Inc (B [0]); B [B [0]: = 1 <(k-1); <br/> Find (j> 1, k + 1); <br/> B [B [0]: = 0; dec (B [0]); <br/> end; <br/> begin <br/> if f [I]>-1 then exit; F [I]: = 0; <br/> for JJ: = 1 to n do if odd (I> (jj-1) Then break; <br/> B [0]: = 0; <br/> Find (I XOR (1 <(jj-1), 1); <br/> end; <br/> begin <br/> assign (input, 'flood. in '); reset (input); <br/> assign (output, 'flood. out'); rewrite (output); <br/> readln (n, m); All: = (1 <n)-1; <br/> for I: = 1 to n do <br/> for J: = 1 to n do a [I, j]: = 1; <br/> for I: = 1 to M do begin <br/> readln (J, K, a [j, k]); A [K, J]: = A [j, k]; <br/> // A [j, k]: = 1-A [j, k]; A [K, J]: = 1-A [K, J]; <br/> end; <br/> for I: = 1 to all do f [I]: =-2; <br/> for I: = 1 to n do f [1 <(I-1)]: = 1; <br/> DFS (all); <br/> {for I: = 1 to n do <br/> for J: = I + 1 to n do if a [I, j]> 1e-8 then <br/> for K1: = 1 to all-1 do if odd (K1> (I-1) Then <br/> for K2: = 1 to all-1 do if odd (k2> (J-1) and (K1 and K2 = 0) Then <br/> F [K1 or k2]: = f [K1 or k2] + F [k1] * f [k2] * A [I, j] ;}< br/> writeln (F [all]: 0: 3); <br/> close (input); <br/> close (output); <br/> end. <br/> 

 

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.