[Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…
题目描述
The Cows have constructed a randomized stink bomb for the purpose of driving away the Piggies. The Piggy civilization consists of N (2 <= N <= 300) Piggy cities conveniently numbered 1..N connected by M (1 <= M <= 44,850) bidirectional roads specified by their distinct endpoints A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). Piggy city 1 is always connected to at least one other city.
The stink bomb is deployed in Piggy city 1. Each hour (including the first one), it has a P/Q (1 <= P <= 1,000,000; 1 <= Q <=
1,000,000; P <= Q) chance of polluting the city it occupies. If it does not go off, it chooses a random road out of the city and follows it until it reaches a new city. All roads out of a city are equally likely to be chosen.
Because of the random nature of the stink bomb, the Cows are wondering which cities are most likely to be polluted. Given a map of the Piggy civilization and the probability that the stink bomb detonates in a given hour, compute for each city the probability that it will be polluted.
For example, suppose that the Piggie civilization consists of two cities connected together and that the stink bomb, which starts in city 1, has a probability of 1/2 of detonating each time it enters a city:
1--2 We have the following possible paths for the stink bomb (where the last entry is the ending city):
1: 1 2: 1-2 3: 1-2-1
4: 1-2-1-2
5: 1-2-1-2-1
etc. To find the probability that the stink bomb ends at city 1, we can add up the probabilities of taking the 1st, 3rd, 5th, ... paths above (specifically, every odd-numbered path in the above list). The probability of taking path number k is exactly (1/2)^k - the bomb must not remain in its city for k - 1 turns (each time with a probability of 1 - 1/2 = 1/2) and then land in the last city
(probability 1/2).
So our probability of ending in city 1 is represented by the sum 1/2 + (1/2)^3 + (1/2)^5 + ... . When we sum these terms infinitely, we will end up with exactly 2/3 as our probability, approximately 0.666666667. This means the probability of landing in city 2 is 1/3, approximately 0.333333333.
Partial feedback will be provided for your first 50 submissions.
一个无向图,节点1有一个炸弹,在每个单位时间内,有p/q的概率在这个节点炸掉,有1-p/q的概率随机选择一条出去的路到其他的节点上。问最终炸弹在每个节点上爆炸的概率。
输入输出格式
输入格式:
* Line 1: Four space separated integers: N, M, P, and Q
* Lines 2..M+1: Line i+1 describes a road with two space separated integers: A_j and B_j
输出格式:
* Lines 1..N: On line i, print the probability that city i
will be destroyed as a floating point number. An answer with an absolute
error of at most 10^-6 will be accepted (note that you should output at
least 6 decimal places for this to take effect).
输入输出样例
2 1 1 2
1 2
0.666666667
0.333333333
做完这道题,我感觉我离完全理解高斯消元又远了一步...
其实,这道题就是“游走”的简化版。
只要求出到每个点的期望次数,然后乘以p/q就是答案。
问题就是怎么样求期望次数。
设f[i]为到i点的期望次数, 于是f[i] = Σ(1/deg[v]) * f[v], v是i的所有相邻的点。
于是高斯消元解决,注意f[1]最后要加1,因为它一开始就经过。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
inline int read(){
int res=;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return res;
}
#define eps 1e-13
int n, m, p, q;
int deg[];
double a[][];
struct edge{
int nxt, to;
}ed[*];
int head[], cnt;
inline void add(int x, int y)
{
ed[++cnt] = (edge){head[x], y};
head[x] = cnt;
} inline void Gauss()
{
for (int i = ; i <= n ; i ++)
{
int pivot = i;
for (int j = i + ; j <= n ; j ++)
if (fabs(a[j][i] - a[pivot][i]) <= eps) pivot = j;
if (pivot != i)
for (int j = ; j <= n + ; j ++)
swap(a[i][j], a[pivot][j]);
for (int j = n + ; j >= i ; j --) a[i][j] /= a[i][i];
for (int j = ; j <= n ; j ++)
if (i != j)
for (int k = n + ; k >= i ; k --)
a[j][k] -= a[j][i] * a[i][k];
}
} int main()
{
n = read(), m = read(), p = read(), q = read();
double k = (double) p / (double) q;
for (int i = ; i <= m ; i ++)
{
int x = read(), y = read();
deg[x]++, deg[y]++;
add(x, y), add(y, x);
}
for (int x = ; x <= n ; x ++)
{
a[x][x] = ;
for (int i = head[x] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
a[x][to] = (- 1.0 / deg[to]) * (1.0 - k);
}
}
a[][n+] = ;
Gauss();
for (int i = ; i <= n ; i ++)
printf("%.9lf\n", k * a[i][n+]);
return ;
}
[Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…的更多相关文章
- 洛谷2973 [USACO10HOL]赶小猪Driving Out the Piggi… 概率 高斯消元
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 洛谷2973 题意概括 有N个城市,M条双向道路组成的地图,城市标号为1到N.“西瓜炸弹”放在1号城市,保证城 ...
- 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 ...
- [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\),为什么会输出 ...
- 洛谷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} ...
- P2973 [USACO10HOL]赶小猪
跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...
- [USACO10HOL]赶小猪
嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...
- luogu P2973 [USACO10HOL]Driving Out the Piggies G 驱逐猪猡
luogu LINK:驱逐猪猡 bzoj LINK:猪猪快跑 问题是在1时刻有个炸蛋在1号点 这个炸弹有p/q的概率爆炸 如果没有爆炸 那么会有1/di的概率选择一条边跳到另外一个点上重复这个过程. ...
随机推荐
- valueForKey与valueForKeyPath 区别
1.删除数组中重复的数据 2.valueForKeyPath:可以深层次取到子属性,不管隐藏的多深 valueForKey:无法取到深层次子属性 但是也有其相似的地方: 比如:快速找到字典数组中ke ...
- Ubuntu16.4安装Vivado Design Suite sdx2019.1
1:下载安装包.到Xilinx官网下载下面为网址: https://www.xilinx.com/support/download.html 2:进入安装包路径,打开终端 Ctrl+alt +t sh ...
- 新建web工程
1.选择新建Dynamic Web Project 2.选择服务器和版本(2.5) 3.WebContend目录下新建一个html文件 4.运行 工程的目录结构: WEB-INF目录时受保护的,不能 ...
- (七十二)c#Winform自定义控件-雷达图
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Mac 安装python 3.*新版本的详细步骤
Mac 系统自带python,不过自带的python版本都是2.*版本.虽然不影响老版本项目的运行, 但是python最新的3.*版本的一些语法与2.*版本并不相同,我们不论是学习还是使用,当然用最新 ...
- filebeat相关registry文件内容解析
filebeat的registry文件中存放的是被采集的所有日志的相关信息. linux中registry中一条日志记录的内容如下 {"source":"/var/log ...
- setInterval、setTimeout之遗忘的第三个参数
今天看阮一峰老师的ES6入门,在一个关于promise的小demo里,老师用到了setTimeout的第三个参数,惊了有没有,定时器还有第三个参数? 喏就是下面这个demo: function tim ...
- 基于Docker搭建大数据集群(一)Docker环境部署
本篇文章是基于Docker搭建大数据集群系列的开篇之作 主要内容 docker搭建 docker部署CentOS 容器免密钥通信 容器保存成镜像 docker镜像发布 环境 Linux 7.6 一.D ...
- 如何看破真假美猴王 ? --java中的Shadowing和Obscuring
故事背景 <西游记>第五十七回:唐僧因悟空又打死拦路强盗,再次把他撵走.六耳猕猴精趁机变作悟空模样,抢走行李关文,又把小妖变作唐僧.八戒.沙僧模样,欲上西天骗取真经.真假二悟空从天上杀到地 ...
- 读《深入理解Elasticsearch》点滴-对象类型、嵌套文档、父子关系
一.对象类型 1.mapping定义文件 "title":{ "type":"text" }, "edition":{ ...