poj-2096-expect/dp

http://poj.org/problem?id=2096

    There are n kinds of viruses and s servers, and a certain virus is found on a certain server with equal probability every day. Ask the expected number of days to find all kinds of viruses and cover all servers.

Using the full expectation formula, the expectation can be decomposed into the weighted sum of the sub-expectations, and the weight is the probability Pi of the sub-expectation occurrence. Note that SUM{ Pi }=1; 

If we have completed (i,j) and the goal is (n,s), then the next day may be (i,j), (i+1,j), (i,j+1), (i+1) ,j+1) The corresponding Pi is p1=(i/n)*(j/s) p2=(1-i/n)*(j/s) p3=(i/n)*(1-j /s) p4=(1-i/n)*(1-j/s)

Let f(i,j) be the expectation of completing the (i,j) distance target, then there is f(i,j)=p1*(f(i,j)+1)+p2*(f(i+1 ,j)+1)+p3*(f(i,j+1)+1)+p4*(f(i+1,j+1)+1) | f(n,s)=0 , shift term Just solve f(i,j).

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL long long 
17 #define pii pair<int,int>
18 #define eps 1e-12
19 
20 double f[1100][1100];
21 int main()
22 {
23     int n,m,i,j,k,t;
24     cin>>n>>m;
25     for(i=n;i>=0;--i){
26         for(j=m;j>=0;--j){
27             if(i==n&&j==m) continue;
28             double p1=(double)i*j,p2=(double)j*(n-i),
29             p3=(double)i*(m-j),p4=(double)(n-i)*(m-j);
30             p1/=(1.0*n*m);p2/=(1.0*n*m);p3/=(1.0*n*m);p4/=(1.0*n*m);
31             f[i][j]=p1+p2*(f[i+1][j]+1)+p3*(f[i][j+1]+1)+
32             p4*(f[i+1][j+1]+1);
33             f[i][j]/=(1.0-p1);
34         }
35     }
36     printf("%.4f\n",f[0][0]);
37     return 0; 
38 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133254&siteId=291194637