zoukankan      html  css  js  c++  java
  • hdu 5036 Explosion(概率期望+bitset)

    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
     
    题意:
    一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问需要用的炸弹为多少。
     
    思路:
    考虑每个点需要用炸弹打开的概率,那么所有点的概率之和就是解。 每个点 v 需要用炸弹打开的概率是 1/S, S是u(u->v联通)的数量, 然后就变成求这个图的传递闭包了,用bitset优化。为什么是1/S呢,因为这S个门里面必定要用一个炸弹砸开,而砸v的概率为1/S。
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<stdlib.h>
     7 #include<bitset>
     8 using namespace std;
     9 #define N 1006
    10 int n;
    11 bitset<N> bs[N];
    12 int main()
    13 {
    14    int t;
    15    int ac=0;
    16    scanf("%d",&t);
    17    while(t--){
    18       scanf("%d",&n);
    19       for(int i=0;i<N;i++){
    20          bs[i].reset();
    21          bs[i][i]=true;
    22       }
    23       for(int i=0;i<n;i++){
    24          int k;
    25          scanf("%d",&k);
    26          for(int j=0;j<k;j++){
    27             int x;
    28             scanf("%d",&x);
    29             x--;
    30             bs[i][x]=true;
    31          }
    32       }
    33       for(int j=0;j<n;j++){
    34          for(int i=0;i<n;i++){
    35             if(bs[i][j]){
    36                bs[i] |= bs[j];
    37             }
    38          }
    39       }
    40       double ans=0;
    41       for(int j=0;j<n;j++){
    42          int cnt=0;
    43          for(int i=0;i<n;i++){
    44             if(bs[i][j]){
    45                cnt++;
    46             }
    47          }
    48          ans+=1.0/cnt;
    49       }
    50       printf("Case #%d: %.5lf
    ",++ac,ans);
    51    }
    52    return 0;
    53 }
    View Code
    Recommend
  • 相关阅读:
    elastic-job详解(二):作业的调度
    elastic-job详解(一):数据分片
    定时任务的分布式调度
    HBase多条件及分页查询的一些方法
    TP6多应用模式配置
    Swoole WebSocket 服务端如何主动推送消息?
    mysql(多级分销)无限极数据库设计方法
    django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
    Centos7安装并配置Python3环境
    short url短链接原理
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4790034.html
Copyright © 2011-2022 走看看