[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的概率选择一条边跳到另外一个点上重复这个过程. ...
随机推荐
- STL中的unique和unique_copy函数
一.unique函数 这个函数的功能就是删除相邻的重复元素,然后重新排列输入范围内的元素,并返回一个最后一个无重复值的迭代器(并不改变容器长度). 例如: vector<); ; i < ...
- CSS——边框设置
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 创建型模式总结(2.x)
顾名思义,创建型模式的聚焦点在如何创建对象能够将对象的创建与使用最大化的分离从而降低系统的耦合度. 创建型模式可分为: 单例模式:一个类只能有一个实例对象 工厂模式: 简单工厂模式:聚焦单个产品种类的 ...
- Hadoop入门 之 Hadoop的安装
1.安装Hadoop的三大步骤 答:1.Linux环境,2.JDK环境,3.配置Hadoop. 2.安装Linux 答:利用阿里云,腾讯云等公有云.选择Ubuntu进行安装,然后利用小putty进行操 ...
- [Code] 大蛇之数据工程
作为“所谓码农”的首篇,本章内容理应涵盖基本编程技法. Python这个东西.简洁,作为载体,是个不错的选择呦. 链接资源: Python文档:https://docs.python.org/3/ 教 ...
- React学习笔记(持续更新)
2.2页面加载过程 1.资源加载过程:URL->DNS查询->资源请求->浏览器解析 ①URL结构:http://www.hhh.com:80/getdata?pid=1#title ...
- 使用回车键执行input框事件
html: <input type="text" class="search-data-input" placeholder="请输入关键词&q ...
- centos使用android studio遇到的一些问题
1.下载完成后进入bin目录启动 ./studio 2. 由于google被墙,SDK 下载不了, 照此教程添加下载源 http://jingyan.baidu.com/album/adc815137 ...
- docker运行jexus+mono爬坑记
新的.net core都已经支持docker.手头有一些原来开发的asp.net旧项目,用的asp.net mvc开发的,跑在.net formwork 4.6上. 在docker的公共仓库searc ...
- json入门初体验
json是JavaScript对象表示法,也是轻量级的文本数据交互格式,独立于语言,能够自我描述 json文本格式在语法上与创建JavaScript对象代码相同,多以json不需要解析器,js程序能够 ...