洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)
题意
Sol
设\(f[i]\)表示炸弹到达\(i\)这个点的概率,转移的时候考虑从哪个点转移而来
\(f[i] = \sum_{\frac{f(j) * (1 - \frac{p}{q})}{deg(j)}}\)
\(f[1]\)需要+1(炸弹一开始在1)
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 3001, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, vis[MAXN][MAXN], deg[MAXN];
double P, Q, a[MAXN][MAXN];
void Gauss() {
for(int i = 1; i <= N; i++) {
int mx = i;
for(int j = i + 1; j <= N; j++) if(a[j][i] > a[mx][i]) mx = j;
if(mx != i) swap(a[i], a[mx]);
for(int j = i + 1; j <= N + 1; j++) a[i][j] /= a[i][i]; a[i][i] = 1;
for(int j = 1; j <= N; j++) {
if(i == j) continue;
double p = a[j][i] / a[i][i];
for(int k = 1; k <= N + 1; k++) {
a[j][k] -= a[i][k] * p;
}
}
}
}
signed main() {
N = read(); M = read(); P = read(); Q = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read();
vis[x][y] = vis[y][x] = 1;
deg[x]++; deg[y]++;
}
for(int i = 1; i <= N; i++) {
a[i][i] = 1;
for(int j = 1; j <= N; j++)
if(vis[i][j])
a[i][j] = -(1.0 - P / Q) / deg[j];
}
a[1][N + 1] = 1;
Gauss();
for(int i = 1; i <= N; i++) printf("%.9lf\n", a[i][N + 1] * (P / Q));
return 0;
}
/*
5 4
1 2 2 1 3
1 3
*/
洛谷P2973 [USACO10HOL]赶小猪(高斯消元 期望)的更多相关文章
- 洛谷3317 SDOI2014重建(高斯消元+期望)
qwq 一开始想了个错的做法. 哎 直接开始说比较正确的做法吧. 首先我们考虑题目的\(ans\)该怎么去求 我们令\(x\)表示原图中的某一条边 \[ans = \sum \prod_{x\in t ...
- 洛谷P2973 [USACO10HOL]赶小猪
https://www.luogu.org/problemnew/show/P2973 dp一遍,\(f_i=\sum_{edge(i,j)}\frac{f_j\times(1-\frac{P}{Q} ...
- HDU2262;Where is the canteen(高斯消元+期望)
传送门 题意 给出一张图,LL从一个点等概率走到上下左右位置,询问LL从宿舍走到餐厅的步数期望 分析 该题是一道高斯消元+期望的题目 难点在于构造矩阵,我们发现以下结论 设某点走到餐厅的期望为Ek 1 ...
- 洛谷2973 [USACO10HOL]赶小猪Driving Out the Piggi… 概率 高斯消元
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 洛谷2973 题意概括 有N个城市,M条双向道路组成的地图,城市标号为1到N.“西瓜炸弹”放在1号城市,保证城 ...
- 洛谷P3232 [HNOI2013]游走(高斯消元+期望)
传送门 所以说我讨厌数学……期望不会高斯消元也不会……好不容易抄好了高斯消元板子被精度卡成琪露诺了…… 首先,我们先算出走每一条边的期望次数,那么为了最小化期望,就让大的期望次数乘上小编号 边的期望次 ...
- Codeforces 446D - DZY Loves Games(高斯消元+期望 DP+矩阵快速幂)
Codeforces 题目传送门 & 洛谷题目传送门 神仙题,%%% 首先考虑所有格子都是陷阱格的情况,那显然就是一个矩阵快速幂,具体来说,设 \(f_{i,j}\) 表示走了 \(i\) 步 ...
- BZOJ 3143 HNOI2013 游走 高斯消元 期望
这道题是我第一次使用高斯消元解决期望类的问题,首发A了,感觉爽爽的.... 不过笔者在做完后发现了一些问题,在原文的后面进行了说明. 中文题目,就不翻大意了,直接给原题: 一个无向连通图,顶点从1编号 ...
- BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...
- BZOJ 2337 XOR和路径 | 高斯消元 期望 位运算
BZOJ 2337 XOR和路径 题解 这道题和游走那道题很像,但又不是完全相同. 因为异或,所以我们考虑拆位,分别考虑每一位: 设x[u]是从点u出发.到达点n时这一位异或和是1的概率. 对于所有这 ...
随机推荐
- jQuery.cookie的使用指南
一个轻量级的cookie 插件,可以读取.写入.删除 cookie. 配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. <script type=&q ...
- iOS(Swift)-Runtime之关于页面跳转的捷径【Runtime获取当前ViewController,很常用】
写在前面 在我们操作页面跳转时,如果当前的类不是UIViewcontroller(下面用VC表示),你会不会写一个代理,或者block给VC传递信息,然后在VC里面进行 ///假如targetVc是将 ...
- 【mNOIP模拟赛Day 1】 T2 数颜色
题目传送门:https://www.luogu.org/problemnew/show/P3939 题外话:写完这题后本地跑了下极限数据,用时1.5s,于是马上用fread+fwrite优化至0.3s ...
- AngularJS入门之数据验证
AngularJS自带了对表单或控件的输入数据进行验证的功能,对于Html5的基础控件均有内建的验证器,以下列举了所有支持的验证类型: email max maxlength min minlengt ...
- 使用webpack打包js文件(隔行变色案例)
使用webpack打包js文件(隔行变色案例) 1.webpack安装的两种方式 运行npm i webpack -g全局安装webpack,这样就能在全局使用webpack的命令 在项目根目录中运行 ...
- Exception message: /bin/bash: line 0: fg: no job control
这个错误是 我本地idea 远程调试hadoop集群出现的 Diagnostics: Exception from container-launch. Container id: container_ ...
- 【树】Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- Oracle 相关查询
--创建用户 create user zzg identified by zzg123; --修改用户的密码 alter user zzg identified by unis; --所有用户所在的表 ...
- Redis的服务命令(实现开机自启动)
在Redis的安装目录下,有一个redis.windows-service.conf文件,即默认的配置文件, 如果需要修改端口号,或者设置密码就需要修改其中的内容: 默认端口号是6379,你可以随意修 ...
- 深入JVM学习心得
前言 相信很多人和我一样长期使用java编程,却很少关注过JVM底层实现,这很大程度上是因为JVM设计的很精巧,因此平时项目也很少遇到涉及JVM的问题.但是一方面出于对java底层技术的好奇,另一方面 ...