题目描述

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. ubuntu httpie使用方法

    HTTPie 是用 Python 写的,所以你可以在几乎所有地方(Linux,MacOSX,Windows)安装它.而且,在大多数的 Linux 发行版中都有编译好的安装包. Debian,Ubunt ...

  2. JSP官方文档(JavaServer Pages Specification)下载

    找了好久,终于找到官网的下载地址了.这样,就可以用一手的文档资料了! 下载地址:http://download.oracle.com/otndocs/jcp/jsp-2_3-mrel2-spec/ s ...

  3. 第二场周赛(递归递推个人Rank赛)——题解

    很高兴给大家出题,本次难度低于上一场,新生的六个题都可以直接裸递归式或者裸递推式解决,对于老生的汉诺塔3,需要找出一般式,后两题分别为裸ST算法(或线段树)/线性DP. 正确的难度顺序为 种花 角谷定 ...

  4. 自定义View入门-绘制基础(1)

    ### 前言 说道自定义View,我们一定会想到,自定义View的绘制流程 - 测量阶段(measure) - 布局阶段(layout) - 绘制阶段(draw) 我们看到的一些炫酷的view效果,都 ...

  5. charles 反向代理

    本文参考:charles 反向代理 这个比较有逼格了: 正向代理和反向代理的区别: 正向代理:是代理客户端,为客户端收发请求,使真实客户端对服务器不可见:在客户这一端的,替客户收发请求(类似现在正常使 ...

  6. Nginx缓存原理及机制

    文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 上篇文章介绍了Nginx一个较为重要的知识点:Nginx实现接口限流.本篇文章将介绍Nginx另一个重要知识点 ...

  7. 将maven项目到入到idea中

    一,前言 在文章将maven项目导入到eclipse中中我将新建的项目到入到了eclipse中了,因为最近也在尝试idea,那么就顺便也到入idea中. maven项目的话,我就使用在文章使用命令行创 ...

  8. 了解一下Java SPI的原理

    了解一下Java SPI的原理 1 为什么写这篇文章? 近期,本人在学习dubbo相关的知识,但是在dubbo官网中有提到Java的 SPI,这个名词之前未接触过,所以就去看了看,感觉还是有很多地方有 ...

  9. web前端开发面试题(附答案)-2

    1.label是什么标签,有什么作用?和for属性使用的作用? label标签来定义表单控制间的关系,当用户选择该标签时,浏览器会自动将焦点转到和标签相关的表单控件上. label 元素不会向用户呈现 ...

  10. Android Studio [相对布局RelativeLayout]

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...