Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1778
Bombs starting from 1, there is a probability explosion of \ (\frac{p}{q}\), if not explode, and so on the probability of moving to connected points. The probability of an explosion at each point.
Analysis
We construct a \ (n\) row \ (n\) column of matrix \ (f\), where \ (f[i][j]\) represents the probability of moving from \ (i\) to \ (j\).
Then \ (f^2\) \ (f^2[i][j]\) is \ (f[i][k]\times{f[k][j]}\) obtained, that is, the probability of \ (i\to{k}\to{j}\), that is, the probability of moving two times to arrive.
In this case \ (f^n[i][j]\) is the probability that \ (i\) moves n times to \ (j\).
We construct a line vector \ (s={1,0,0,..., 0}\).
Then \ (s\times{f^i}\) The result is the first line of \ (f^i\), that is, from \ (1\), moving \ (i\) times the probability of reaching each point.
Then from \ (1\), moving \ (i\) times to a point and then the probability of explosion is \ (S\times{f^i}\times{p\over q}\).
So the answer line vector
$ $ans =\sum_{i=0}^{\infty}s\times{f^i}\times{p\over q}$$
Then, according to the geometric series summation formula, get:
$ $ans \times (i-f) =\frac{p}{q}\times{s}$$
where \ (i\) is the unit matrix, the size is \ (n\times{n}\), the diagonal is \ (1\), other locations are \ (0\).
Only \ (ans\) is unknown, and this is a linear equation group.
The problem, however, is that the coefficients of the i\ equation are \ (f\) (i\) columns instead of rows. So we invert the ranks of \ (f\), or write an awkward Gaussian elimination, not a big problem.
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= -+5;5 intn,m,p,q;6 intD[MAXN];7 DoubleRate ;8 DoubleF[MAXN][MAXN];9 voidGause () {Ten for(intI=1; i<=n;i++){ One intt=i; A for(intj=i+1; j<=n;j++)if(Fabs (F[j][i]) >fabs (F[t][i]) t=J; - if(i!=t) for(intj=i;j<=n+1; j + +) Swap (f[t][j],f[i][j]); - for(intj=i+1; j<=n;j++){ the Doublex=f[j][i]/F[i][i]; - for(intk=i;k<=n+1; k++) f[j][k]-=f[i][k]*x; - } - } + for(inti=n;i;i--){ - for(intj=i+1; j<=n;j++) f[i][n+1]-=f[i][j]*f[j][n+1]; +f[i][n+1]/=F[i][i]; A } at } - intMain () { -scanf"%d%d%d%d",&n,&m,&p,&q); - if(P>Q) p=Q; -Rate= (Double) p/Q; - for(intI=1; i<=m;i++){ in intx, y; scanf"%d%d",&x,&y); -d[x]++; d[y]++; tof[x][y]+=1.0; f[y][x]+=1.0; + } - for(intI=1; i<=n;i++){ the for(intj=1; j<=n;j++){ * if(D[j]) f[i][j]/=D[j]; $f[i][j]*=rate-1;Panax Notoginseng } -f[i][i]+=1.0; the } +f[1][n+1]=Rate ; A Gause (); the for(intI=1; i<=n;i++) printf ("%.9lf\n", f[i][n+1]); + return 0; -}View Code
1778: [Usaco2010 HOL]DOTP expelled pigs time limit:10 sec memory Li mit:64 MB
submit:439 solved:168
[submit][status][ Discuss] Description cows have built a random stench bomb to expel pigs. The civilization of the pigs consists of 1 to n (2 <= n <= 300) total n Pig City. These cities are two-way road connections represented by the M (1 <= m <= 44,850) strip by two different endpoints A_j and B_j (1 <= a_j<= n; 1 <= b_j <= N). Make sure the city is 1 connected to at least one other city. At first the stink bomb will be placed in the City 1. Each hour (including the first one hours), it has the probability of p/q (1 <= P <=1,000,000; 1 <= Q <= 1,000,000) to pollute the city in which it resides. If it does not contaminate the city it is in this hour, it randomly chooses a path to walk along this road to a new city within this hour. All roads that can leave the city are chosen in equal probability. Because of the random nature of the stench, cows are confused about which city is most likely to be contaminated. Given the probability that a swine civilization map and stink bombs explode within an hour. Calculate the probability that each city will eventually be contaminated. The following example assumes that the swine civilization has two connected cities. The stench bomb departs from City 1, and every city, it has a 1/2 probability of exploding. 1--2 The following paths are the paths that the bombs might pass through (the last city was the city where the stink bombs exploded): 1:12:1-23:1-2-14:1-2-1-25:1-2-1-2-1 ... to get the bomb in the city 1 the probability of terminating, we can put the above 1th, 3rd, 5th ... ... The probability of the path is added (that is, the path to the odd numbered table above). The probability of the K-path in the table above is exactly the ^k, which means that the city must be left in the first k-1 round (with a probability of 1-1/2 = 1/2 each time) and remain in the last city (probability is 1/2). So the probability of ending in City 1 can be expressed as 1/2 + (1/2) ^3 + (1/2) ^5 + .... When we calculate the infinite number of items together, we end up with exactly 2/3, which is the probability we demand, about 0.666666667. This means that the probability of eventually staying in the City 2 is 1/3, about 0.333333333. input* line 1th: Four integers separated by spaces: N, M, P, and q* 2nd to m+1 rows: The i+1 line uses two integers separated by spaces A_j and B_j to represent a road. output* 1th to nth: In line I, the probability of city I being destroyed with a floating-point number is output. An answer that doesn't exceed 10^-6 will be accepted (note that means you need at least 6 outputA valid number makes the answer effective). Sample Input2 1 1 2
1 2
Sample Output
0.666666667
0.333333333
Hintsource
Gold
Bzoj_1778_[usaco2010_hol]_dotp_ Expelling Pigs _ (expected dynamic programming + Gaussian elimination + matrix)