Hdu 5036-Explosion 传递闭包,bitset,期望/概率

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5036

Explosion

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 879    Accepted Submission(s): 309


Problem Description
Everyone knows Matt enjoys playing games very much. Now, he is playing such a game. There are N rooms, each with one door. There are some keys(could be none) in each room corresponding to some doors among these N doors. Every key can open only one door. Matt has some bombs, each of which can destroy a door. He will uniformly choose a door that can not be opened with the keys in his hand to destroy when there are no doors that can be opened with keys in his hand. Now, he wants to ask you, what is the expected number of bombs he will use to open or destroy all the doors. Rooms are numbered from 1 to N.
 
Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

In the first line of each test case, there is an integer N (N<=1000) indicating the number of rooms. 

The following N lines corresponde to the rooms from 1 to N. Each line begins with an integer k (0<=k<=N) indicating the number of keys behind the door. Then k integers follow corresponding to the rooms these keys can open.
 
Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1), y is the answer which should be rounded to 5 decimal places.
 
Sample Input
2 3 1 2 1 3 1 1 3 0 0 0
 
Sample Output
Case #1: 1.00000
Case #2: 3.00000
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5644 5643 5642 5641 5640
 
题意:N扇门,每次可以炸开一道门,并获得这扇门里的钥匙,求炸开门的期望。
题解:
我们要求总期望,可以将每扇门的期望值相加。
然后我们可以把这道题转化为一个有向图,要求解的为有多少个结点可以到结点i(1<=i<=n)。则要炸开第i个门的期望值为 1/能到达i的结点个数 。
最后统计和即可。
统计有多少个结点,可以用bitset优化传递闭包。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 bitset<1010> a[1010];
 4 int read()
 5 {
 6     int s=0,fh=1;char ch=getchar();
 7     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
 8     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
 9     return s*fh;
10 }
11 int main()
12 {
13     int T,N,i,j,k,kk,ans,case1=0;
14     double sum;
15     T=read();
16     while(T--)
17     {
18         N=read();
19         for(i=1;i<=N;i++){a[i].reset();a[i][i]=1;}
20         for(i=1;i<=N;i++)
21         {
22             k=read();
23             for(j=1;j<=k;j++)
24             {
25                 kk=read();
26                 a[i][kk]=1;
27             }
28         }
29         for(i=1;i<=N;i++)
30         {
31             for(j=1;j<=N;j++)
32             {
33                 if(a[j][i])a[j]|=a[i];
34             }
35         }
36         sum=0.0;
37         for(i=1;i<=N;i++)
38         {
39             ans=0;
40             for(j=1;j<=N;j++)
41             {
42                 if(a[j][i]==1)ans++;
43             }
44             sum+=(1.0/(double)ans);
45         }
46         printf("Case #%d: %.5lf
",++case1,sum);
47     }
48     fclose(stdin);
49     fclose(stdout);
50     return 0;
51 }
View Code
原文地址:https://www.cnblogs.com/Var123/p/5281723.html