Luogu2973:[USACO10HOL]赶小猪
题面
Sol
设\(f[i]\)表示炸弹到\(i\)不爆炸的期望
高斯消元即可
另外,题目中的概率\(p/q\)实际上为\(1-p/q\)
还有,谁能告诉我不加\(EPS\),为什么会输出\(-0.00000\)
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(505);
const int __(100005);
IL ll Input(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, fst[_], nxt[__], cnt, to[__], dg[_];
double ans, f[_], a[_][_], p, q;
IL void Add(RG int u, RG int v){
to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++; ++dg[v];
}
IL void Gauss(){
for(RG int i = 1; i < n; ++i)
for(RG int j = i + 1; j <= n; ++j){
RG double div = a[j][i] / a[i][i];
for(RG int k = 1; k <= n + 1; ++k) a[j][k] -= a[i][k] * div;
}
for(RG int i = n; i; --i){
f[i] = a[i][n + 1] / a[i][i];
for(RG int j = i - 1; j; --j) a[j][n + 1] -= f[i] * a[j][i];
}
}
int main(RG int argc, RG char* argv[]){
n = Input(); m = Input(); Fill(fst, -1);
q = Input(); q /= Input(); p = 1.0 - q;
for(RG int i = 1, u, v; i <= m; ++i)
u = Input(), v = Input(), Add(u, v), Add(v, u);
for(RG int u = 1; u <= n; u++){
a[u][u] = -1.0;
for(RG int e = fst[u]; e != -1; e = nxt[e]) a[to[e]][u] = p / dg[u];
}
a[1][n + 1] = -1.0; Gauss();
for(RG int i = 1; i <= n; ++i){
f[i] *= q;
if(fabs(f[i]) < 1e-9) f[i] = 0;
}
for(RG int i = 1; i <= n; ++i) printf("%.9lf\n", f[i]);
return 0;
}
Luogu2973:[USACO10HOL]赶小猪的更多相关文章
- [Luogu2973][USACO10HOL]赶小猪
Luogu sol 首先解释一波这道题无重边无自环 设\(f_i\)表示\(i\)点上面的答案. 方程 \[f_u=\sum_{v,(u,v)\in E}(1-\frac PQ)\frac{f_v}{ ...
- [Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…
题目描述 The Cows have constructed a randomized stink bomb for the purpose of driving away the Piggies. ...
- 洛谷2973 [USACO10HOL]赶小猪Driving Out the Piggi… 概率 高斯消元
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 洛谷2973 题意概括 有N个城市,M条双向道路组成的地图,城市标号为1到N.“西瓜炸弹”放在1号城市,保证城 ...
- 洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)
题意 题目链接 Sol 设\(f[i]\)表示炸弹到达\(i\)这个点的概率,转移的时候考虑从哪个点转移而来 \(f[i] = \sum_{\frac{f(j) * (1 - \frac{p}{q}) ...
- 洛谷P2973 [USACO10HOL]赶小猪
https://www.luogu.org/problemnew/show/P2973 dp一遍,\(f_i=\sum_{edge(i,j)}\frac{f_j\times(1-\frac{P}{Q} ...
- Luogu P2973 [USACO10HOL]赶小猪Driving Out the Piggi 后效性DP
有后效性的DP:$f[u]$表示到$u$的期望次数,$f[u]=\Sigma_{(u,v)} (1-\frac{p}{q})*f[v]*deg[v]$,最后答案就是$f[u]*p/q$ 刚开始$f[1 ...
- P2973 [USACO10HOL]赶小猪
跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...
- [USACO10HOL]赶小猪
嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...
- 小猪cms微信二次开发之怎样分页
$db=D('Classify'); $zid=$db->where(array('id'=>$this->_GET('fid'),'token'=>$this->tok ...
随机推荐
- ConcurrenHashMap源码分析(二)
本篇博客的目录: 一:put方法源码 二:get方法源码 三:rehash的过程 四:总结 一:put方法的源码 首先,我们来看一下segment内部类中put方法的源码,这个方法它是segment片 ...
- Ubuntu系统下crontab的使用
最近一个项目,需要用到一个定时任务,先说crontab的常用命令. crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 crontab -l //列出 ...
- Spring Data与elasticsearch版本对应关系
- springmvc+mybatis+mysql 数据库插入中文是乱码
java web项目,前台页面的表单数据,插入到数据库时,结果出现乱码"???"的问题,断断续续折腾了一天时间,废话不说,步骤如下: 一:在web.xml中配置:编码格式拦截器 & ...
- 登录功能(MD5加密)
登录这个功能,是不管哪个项目都会用到的,登录做的好坏,安全性的保障将直接影响到整个系统的成败,尤其是一些安全性要求比较严格的项目 1.首先需要对密码进行加密,这里用到的是md5加密,需要在login. ...
- 震撼功能:逐浪CMS全面支持PWA移动生成意指未来
Progressive Web App, 简称 PWA,是提升 Web App 的体验的一种新方法,能给用户原生应用的体验. PWA 能做到原生应用的体验不是靠特指某一项技术,而是经过应用一些新技术进 ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
- 内置函数--bin() oct() int() hex()
英文文档: bin(x) Convert an integer number to a binary string. The result is a valid Python expression. ...
- Luogu P1092 虫食算
题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045 +8468#6633 44445509678 其中# ...
- Spark SQL1.2与HDP2.2结合
1.hbase相同的rowkey里存在多条记录问题的调研解决方案 VERSIONS => 3,Hbase version 最多插入三条记录 将一个集群hbase中表 "Vertical ...