洛谷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的概率. 对于所有这 ...
随机推荐
- 微信小程序 base64图片在canvas上画图
上代码 wxml <canvas canvas-id="myCanvas" style="width:400px;height:400px;">&l ...
- Java之集合(二十五)ConcurrentHashMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7520808.html 1.前言 本章介绍使用的最频繁的并发集合类之一ConcurrentHashMap,之前介绍 ...
- Java之集合(二)ArrayDeque
转载请注明源出处:http://www.cnblogs.com/lighten/p/7283928.html 1.前言 上章讲解了Java中的集合接口和相关实现抽象类,本章开始介绍一些具体的实现类,第 ...
- Eclipse调用hadoop2运行MR程序(转)
hadoop:hadoop2.2 ,windows myeclipse环境: Eclipse调用hadoop运行MR程序其实就是普通的java程序可以提交MR任务到集群执行而已.在Hadoop1中,只 ...
- 【Java并发编程】:使用synchronized获取互斥锁
在并发编程中,多线程同时并发访问的资源叫做临界资源,当多个线程同时访问对象并要求操作相同资源时,分割了原子操作就有可能出现数据的不一致或数据不完整的情况,为避免这种情况的发生,我们会采取同步机制,以确 ...
- 弱网测试--使用fiddler进行弱网测试
弱网测试原理以及方法(一) 一.为什么要进行弱网测试? 按照移动特性,各种网络连接协议不同,导致通信的信号不同,速率也不同,影响应用的加载时间.可用性.稳定性 二.什么样的网络属于弱网? 低于2g速率 ...
- android 使用lint + studio ,排查客户端无用string,drawable,layout资源
在项目中点击右键(或者菜单中的Analyze),在出现的右键菜单中有“Analyze” --> “run inspaction by Name ...”.在弹出的搜索窗口中输入想执行的检查类型, ...
- C/C++程序调试和内存检测
程序出现错误很正常,一个优秀的程序员必须学会调试,发现错误并改正.减少程序错误最有效的方法是:在敲代码之前,多花点时间思考,如何构造程序,数据结构和算法,尽量把细节提前写下来,可以尝试着在纸上写出核心 ...
- CentOS install搭建SVN服务器
安装步骤如下: 1.命令:yum install subversion 之后会打印很多消息,显示 Complete! 则完成. 2.查看 svn 版本:svnserve --version 3.创建S ...
- [uva] 1671 History of Languages
题目描述 输入两个DFA,判断是否等价. https://uva.onlinejudge.org/external/16/1671.pdf 输入 第一行T 可以接受的字母表 第二行N 状态数 接下去N ...