题目: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
3
1 2
1 3
1 1
3
0
0
0
Sample Output
Case #1: 1.00000
Case #2: 3.00000
Source
Recommend
题意:N扇门,每次可以炸开一道门,并获得这扇门里的钥匙,求炸开门的期望。
题解:
我们要求总期望,可以将每扇门的期望值相加。
然后我们可以把这道题转化为一个有向图,要求解的为有多少个结点可以到结点i(1<=i<=n)。则要炸开第i个门的期望值为 1/能到达i的结点个数 。
最后统计和即可。
统计有多少个结点,可以用bitset优化传递闭包。
#include<bits/stdc++.h>
using namespace std;
bitset<> a[];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int main()
{
int T,N,i,j,k,kk,ans,case1=;
double sum;
T=read();
while(T--)
{
N=read();
for(i=;i<=N;i++){a[i].reset();a[i][i]=;}
for(i=;i<=N;i++)
{
k=read();
for(j=;j<=k;j++)
{
kk=read();
a[i][kk]=;
}
}
for(i=;i<=N;i++)
{
for(j=;j<=N;j++)
{
if(a[j][i])a[j]|=a[i];
}
}
sum=0.0;
for(i=;i<=N;i++)
{
ans=;
for(j=;j<=N;j++)
{
if(a[j][i]==)ans++;
}
sum+=(1.0/(double)ans);
}
printf("Case #%d: %.5lf\n",++case1,sum);
}
fclose(stdin);
fclose(stdout);
return ;
}