[USACO10HOL]赶小猪
嘟嘟嘟
这题和某一类概率题一样,大体思路都是高斯消元解方程。
不过关键还是状态得想明白。刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\frac{p}{q}\)?),于是就考虑换状态。
一个非常好的状态是炸弹传到点\(i\)的概率,这样答案再乘以一个\(\frac{p}{q}\)就好了。转移也特别好办:\(f[i] = \sum (1 - \frac{p}{q}) * \frac{1}{du[v]}*f[v]\)。
别忘了结点1的概率要加1。
于是这题就做完了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 305;
const int maxe = 1e6 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
}
int n, m, p, q;
db P;
struct Edge
{
int nxt, to;
}e[maxe];
int head[maxn], ecnt = -1, du[maxn];
In void addEdge(int x, int y)
{
++du[x];
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
db f[maxn][maxn], ans[maxn];
In void init()
{
f[1][n + 1] = 1;
for(int i = 1; i <= n; ++i)
{
f[i][i] = 1;
for(int j = head[i], v; ~j; j = e[j].nxt)
f[v = e[j].to][i] -= (1 - P) * 1.0 / du[i];
}
}
In void Gauss()
{
for(int i = 1; i <= n; ++i)
{
int pos = i;
for(int j = i + 1; j <= n; ++j)
if(fabs(f[j][i]) > fabs(f[pos][i])) pos = j;
if(pos ^ i) swap(f[pos], f[i]);
if(fabs(f[i][i]) < eps) continue;
db tp = f[i][i];
for(int j = i; j <= n + 1; ++j) f[i][j] /= tp;
for(int j = i + 1; j <= n; ++j)
{
db tp = f[j][i];
for(int k = i; k <= n + 1; ++k) f[j][k] -= tp * f[i][k];
}
}
for(int i = n; i; --i)
{
ans[i] = f[i][n + 1];
for(int j = i - 1; j; --j) f[j][n + 1] -= f[j][i] * ans[i];
}
}
int main()
{
//MYFILE();
Mem(head, -1);
n = read(), m = read(), p = read(), q = read();
P = 1.0 * p / q;
for(int i = 1; i <= m; ++i)
{
int x = read(), y = read();
addEdge(x, y), addEdge(y, x);
}
init(), Gauss();
for(int i = 1; i <= n; ++i) printf("%.9lf\n", ans[i] * P);
return 0;
}
[USACO10HOL]赶小猪的更多相关文章
- [Luogu2973][USACO10HOL]赶小猪
Luogu sol 首先解释一波这道题无重边无自环 设\(f_i\)表示\(i\)点上面的答案. 方程 \[f_u=\sum_{v,(u,v)\in E}(1-\frac PQ)\frac{f_v}{ ...
- Luogu2973:[USACO10HOL]赶小猪
题面 Luogu Sol 设\(f[i]\)表示炸弹到\(i\)不爆炸的期望 高斯消元即可 另外,题目中的概率\(p/q\)实际上为\(1-p/q\) 还有,谁能告诉我不加\(EPS\),为什么会输出 ...
- 洛谷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 ...
- [Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…
题目描述 The Cows have constructed a randomized stink bomb for the purpose of driving away the Piggies. ...
- 小猪cms微信二次开发之怎样分页
$db=D('Classify'); $zid=$db->where(array('id'=>$this->_GET('fid'),'token'=>$this->tok ...
随机推荐
- Python中下划线的5种含义
目录 单前导下划线 _var 当涉及到变量和方法名称时,单个下划线前缀有一个约定俗成的含义. 它是对程序员的一个提示 - 意味着Python社区一致认为它应该是什么意思,但程序的行为不受影响. 单末尾 ...
- 去除空格函数trim
实际查询中,经常存在多个tables,需要统一查询比如segments总大小或者索引或者主键等,我们得到大量的tables表名称,但是SQL查询,每次需要手工添加双引号,去除空格很麻烦. 可以通过文本 ...
- 使用jdk8 stream简化集合操作
使用stream的前提是对lambda表达式和函数式接口有一定的了解,同时对方法引用和普通传参的区别有一定的认识. stream的三大特性:1.不存储数据2.不改变源数据3.延时执行. stream优 ...
- C# Combox控件绑定自定义数据
DataTable dt = new DataTable(); dt.Columns.Add("name"); dt.Columns.A ...
- What's new in C# 7.0
1.数字分隔符(Digit Separators ) 数字分隔符使代码更具可读性.在声明变量时,可以将_添加到单独的数字中.编译器只删除_.以下代码片段在C#7中看起来更具可读性: In C# 6 l ...
- Error:Unable to start the daemon process. This problem might be caused by incorrect configuration of
我试了修改或者配置gradle文件没有成功解决的 ,所以试了这个解决方案 试了下这个是可以解决的. 变量名 _JAVA_OPTIONS 变量值 -Djava.net.preferIPv4Sta ...
- SmartEvent with kbmMW #1
前言 前面的文章,我写了有关SmartBinding框架方面的内容.SmartBinding的目的是将数据容器绑定到一起,通常情况下,数据容器可以是显示数据或与数据交互的控件(Edit,ListVie ...
- IE浏览器下载后台返回的Excel文件,报错400
问题描述(见下图): 问题分析: 400是后端没有接收到请求 原因是后端高版本的tomcat不会自动对字符串进行转义 所以,前端把参数值进行转义,即encodeURI(string) 问题处理前代码( ...
- 【shell】shell基础
一.数据类型 1.shell变量 运行shell时,会同时存在三种变量: 1) 局部变量 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量. 2) 环 ...
- 不常见的sql错误处理方法(1)
select @@global.sql_mode, mysql5.7 导致 group查询出错 set @@global.sql_mode='' # mysql重启就失效了: phpstudy -&g ...