题目描述

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).

输入输出样例

输入样例#1:

2 1 1 2
1 2
输出样例#1:

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…的更多相关文章

  1. 洛谷2973 [USACO10HOL]赶小猪Driving Out the Piggi… 概率 高斯消元

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 洛谷2973 题意概括 有N个城市,M条双向道路组成的地图,城市标号为1到N.“西瓜炸弹”放在1号城市,保证城 ...

  2. 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 ...

  3. [Luogu2973][USACO10HOL]赶小猪

    Luogu sol 首先解释一波这道题无重边无自环 设\(f_i\)表示\(i\)点上面的答案. 方程 \[f_u=\sum_{v,(u,v)\in E}(1-\frac PQ)\frac{f_v}{ ...

  4. Luogu2973:[USACO10HOL]赶小猪

    题面 Luogu Sol 设\(f[i]\)表示炸弹到\(i\)不爆炸的期望 高斯消元即可 另外,题目中的概率\(p/q\)实际上为\(1-p/q\) 还有,谁能告诉我不加\(EPS\),为什么会输出 ...

  5. 洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)

    题意 题目链接 Sol 设\(f[i]\)表示炸弹到达\(i\)这个点的概率,转移的时候考虑从哪个点转移而来 \(f[i] = \sum_{\frac{f(j) * (1 - \frac{p}{q}) ...

  6. 洛谷P2973 [USACO10HOL]赶小猪

    https://www.luogu.org/problemnew/show/P2973 dp一遍,\(f_i=\sum_{edge(i,j)}\frac{f_j\times(1-\frac{P}{Q} ...

  7. P2973 [USACO10HOL]赶小猪

    跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...

  8. [USACO10HOL]赶小猪

    嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...

  9. luogu P2973 [USACO10HOL]Driving Out the Piggies G 驱逐猪猡

    luogu LINK:驱逐猪猡 bzoj LINK:猪猪快跑 问题是在1时刻有个炸蛋在1号点 这个炸弹有p/q的概率爆炸 如果没有爆炸 那么会有1/di的概率选择一条边跳到另外一个点上重复这个过程. ...

随机推荐

  1. JSP实例:彩色验证码

    本例使用一个JavaBean,名为Image.java,包com.zempty.bean下; 三个JSP文件,分别为image.jsp.login.jsp.check.jsp.其中login.jsp是 ...

  2. charles DNS欺骗

    本文参考:charles DNS欺骗 DNS欺骗/DNS Spoofing 功能:通过将您自己的主机名指定给远程地址映射来欺骗DNS查找 一般的开发流程中,在上线之前都需要在测试环境中先行进行验证,而 ...

  3. prometheus告警模块alertmanager注意事项(QQ邮箱发送告警)

    配置alertmanager的时候,都是根据网上的教程来配置的. 因为我是用QQ邮箱来发送告警的,所以alertmanager.yml的邮箱配置如下: global: resolve_timeout: ...

  4. 003:CSS三大重点之一:盒子模型

    目录 1:盒子模型 2:边框: 2.1:合写 2.2:适用于:table系元素.边框合并 3:内边距 4:外边距: 4.1:盒子居中三大条件 4.2:外边距合并.外边距塌陷(父子嵌套)解决方法三种 前 ...

  5. Android静态注册广播无法接收的问题(8.0+版本)

    如果你静态注册的广播无法接收到消息,请先检查下:你的安卓版本是不是8.0+ * 前言** Google官方声明:Beginning with Android 8.0 (API level 26), t ...

  6. aclocal-1.14: 未找到命令

    WARNING: 'aclocal-1.14' is missing on your system. 一条命令解决 autoreconf -ivf

  7. [Boost库] noncopyable——禁止拷贝的类

    1.noncopyable允许程序轻松地实现一个禁止拷贝的类,在头文件<boost/noncopyable.hpp>中   2.实现原理很简单:noncopyable的实现就是用了C++中 ...

  8. WordPress 文章目录插件 Easy Table of Contents 配置教程

    今天介绍一款目录插件,本站已经使用一段时间,感觉不错,实现效果可以看文章页右侧边栏有目录,点击目录内容会快速定位.推荐给大家. 一.安装Easy Table of Contents 插件主页搜索Eas ...

  9. 新手学习FFmpeg - 调用API完成视频的读取和输出

    在写了几个avfilter之后,原本以为对ffmpeg应该算是入门了. 结果今天想对一个视频文件进行转码操作,才发现基本的视频读取,输出都搞不定. 痛定思痛,仔细研究了一下ffmpeg提供的examp ...

  10. SpringBoot系列——ElasticSearch

    前言 本文记录安装配置ES环境,在SpringBoot项目中使用SpringData-ElasticSearch对ES进行增删改查通用操作 ElasticSearch官网:https://www.el ...