hdu 5036 Explosion bitset优化floyd
http://acm.hdu.edu.cn/showproblem.php?pid=5036
题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个。
如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏。问需要炸弹的期望。
比如一副图是1-->2-->3的。那么期望是11 / 6
假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3
假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后3号点能直接走到,但是需要炸开1号,贡献是2/3
假如从3号点开始,1/3概率选中3号点开始,那么需要炸开3号点,然后,可以选择炸开一号,2号直接到达,或者炸开2号再炸开1号。
总贡献是1/3 * (1/2 * 2 + 1/2 * 3) = 5 / 6
然后这样分类是做不了的,对于期望的题,一般都是靠期望的独立性。
就是算出每一个们的贡献,相加就是答案。而不是像上面那样,一个一个们地选择可能的方案。
对于这副图,首先自己肯定能到自己,就是选择炸开,然后求一个floyd,统计出有多少个点能够到达k,那么k这个点对答案的贡献
就是1.0 / cnt
因为在cnt种方法里面,只有一种是需要用炸弹的。
比如,我算2号顶点的贡献,就是,
①、炸开一号,一共用了1步,用了0个炸弹,这个炸弹不应该算做2号的消耗。是打开了1号,2号就无条件打开了
②、炸开2号,一共用了2步,用了1个炸弹。
所以贡献是1 / 2
累加即可。
这里的floyd,复杂度是n^3
需要用bitset优化到n^3 / 128
bitset大法好,学习了。
意思就是,
如果是1000位的bool[],你用锕a ^ b需要的时间是O(n)
但是如果用bitset直接做,复杂度是O(n / sizeof bitset)
bitset内存是,8bit是一字节,那么长度 / 8就是字节数。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e3 + ;
bitset<maxn>e[maxn];
void init(int n) {
for (int i = ; i <= n; ++i) {
e[i].reset();
e[i][i] = ;
}
}
void work() {
int n;
scanf("%d", &n);
init(n);
for (int i = ; i <= n; ++i) {
int k;
scanf("%d", &k);
while (k--) {
int x;
scanf("%d", &x);
e[i][x] = ;
}
}
// for (int k = 1; k <= n; ++k) {
// for (int j = 1; j <= n; ++j) {
// for (int i = 1; i <= n; ++i) {
// e[j][i] = e[j][i] || e[j][k] && e[k][i];
// }
// }
// }
// cout << e[1][2] << endl;
// for (int i = 1; i <= n; ++i) {
// cout << e[i] << endl;
// }
for (int k = ; k <= n; ++k) {
for (int i = ; i <= n; ++i) {
if (e[i][k]) {
e[i] |= e[k];
}
}
}
// cout << endl;
// for (int i = 1; i <= n; ++i) {
// cout << e[i] << endl;
// }
double ans = 0.0;
for (int i = ; i <= n; ++i) {
int cnt = ;
for (int j = ; j <= n; ++j) {
cnt += e[j][i];
}
ans += 1.0 / cnt;
}
static int f = ;
printf("Case #%d: %0.5f\n", ++f, ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
hdu 5036 Explosion bitset优化floyd的更多相关文章
- POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】
<题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...
- hdu 5036 Explosion(概率期望+bitset)
Problem Description Everyone knows Matt enjoys playing games very much. Now, he to N. Input The firs ...
- HDU - 5036 Explosion
Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a ga ...
- HDU 5036 Explosion (传递闭包+bitset优化)
<题目链接> 题目大意: 一个人要打开或者用炸弹砸开所有的门,每个门后面有一些钥匙,一个钥匙对应一个门,告诉每个门里面有哪些门的钥匙.如果要打开所有的门,问需要用的炸弹数量为多少. 解题分 ...
- bitset优化FLOYD HDU 3275
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...
- hdu 5036 概率+bitset
http://acm.hdu.edu.cn/showproblem.php?pid=5036 n个房间每个房间里面有一把或多把钥匙可以打开其他的门.如果手上没有钥匙可以选择等概率随机选择一个门炸开,求 ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- BZOJ2208 [Jsoi2010]连通数[缩点/Floyd传递闭包+bitset优化]
显然并不能直接dfs,因为$m$会非常大,复杂度就是$O(mn)$: 这题有三种做法,都用到了bitset的优化.第二种算是一个意外的收获,之前没想到竟然还有这种神仙操作.. 方法一:缩点+DAG上b ...
随机推荐
- 看懂JSP声明的格式。。。
在WebRoot下新建test3.jsp 改动body内容: <%! int a = 3; %> <% int b = 3; %> <%= a-- %& ...
- httpclient发送get请求
/** * 获取httpclient的请求url地址 */ public static String getUrl(){ String url = "http://"+map.ge ...
- Makefile详解 (转--不错就是有点长)
概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的程序员,make ...
- Cocos2d-x 3.2 Lua演示样例CurrentLanguageTest(当前语言环境)
Cocos2d-x 3.2 Lua演示样例CurrentLanguageTest(当前语言环境) 转载请注明:IT_xiao小巫 本篇博客介绍Cocos2d-x 3.2给我们提供的一个样例.获取当前程 ...
- 安装mint的问题集锦
1.修改DNS解析配置 刚刚安装完mint可能现无法连接源的问题,总是说dns解析错误,这个可能是dns配置文件造成的,因为官网下的mint很可能是配置了国外的dns解析,比如我刚安上时,就是默认配置 ...
- Spring如何实现IOC和AOP的,说出实现原理。
用过spring的朋友都知道spring的强大和高深,都觉得深不可测,其实当你真正花些时间读一读源码就知道它的一些技术实现其实是建立在一些最基本的技术之上而已:例如AOP(面向方面编程)的实现是建立在 ...
- 系统队列中的Windows错误报告
- C++不能在栈上申请动态内存,而只能依靠指针
以下三种情况皆错,都编译不过: int main(int argc, char* argv[]) { int a; int b[a]; } int main(int argc, char* argv[ ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- Android控件之HorizontalScrollView 去掉滚动条
在默认情况下,HorizontalScrollView控件里面的内容在滚动的情况下,会出现滚动条,为了去掉滚动条, 只需要在<HorizontalScrollView/>里面加一句 and ...