草庐IT

洛谷 P2973 [USACO10HOL]Driving Out the Piggies G ,概率论+高斯消元

qingmuhe 2023-03-28 原文

洛谷 P2973 [USACO10HOL]Driving Out the Piggies G

题目描述

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

样例输入 #1

2 1 1 2 
1 2

样例输出 #1

0.666666667 
0.333333333


题意

给出一张\(n\)个点,\(m\)条边的无向图,节点\(1\)有一个炸弹,在每个单位时间内,有\(\dfrac {p}{ q}\)的概率在这个节点炸掉,有\(1-\dfrac{p}q\)的概率随机选择一条出去的路到其他的节点上,去每个节点的概率相等。问最终炸弹在每个节点上爆炸的概率。

题解

由于只要一直不爆炸,炸弹就可以在节点间反复横跳,不断对其爆炸概率产生贡献,故通过模拟顺推的方式求概率不太现实(除非可以归纳出无限次数下的表达式)。

这里考虑概率最终稳态下,通过相邻点之间的概率关系来解出每个点的爆炸概率

​ 爆炸概率\(P=\dfrac {p}{ q}\)

\(f(i)\)为炸弹在\(i\)节点爆炸的概率,

\(arrve(i)\)为炸弹到达i节点的概率

\(deg(i)\)为与\(i\)相连节点个数

则: $$f(i)=arrve(i)*P $$

​ $$\quad\quad\quad\quad =P*\sum_{i,j相连}arrve(j)*(1-P)/deg(j) $$

​ $$\quad\quad\quad\quad =P*\sum_{i,j相连}(f(j)/P)*(1-P)/deg(j) $$

​ $$ =\sum_{i,j相连}f(j)*(1-P)/deg(j) $$

而对于\(f(1)\),除加上与之相连的节点为其贡献的概率外,由于炸弹初始于点\(1\),故还应加上第一轮爆炸概率\(P\)

\(n\)个点,可写出\(n\)个方程,然后用高斯消元模板求解即可(需注意控制精度

//https://www.luogu.com.cn/problem/P2973
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const db eps=1e-14,one=1.00000000000;
#define rep(i_,from_,to_) for(register int i_=(from_);i_<=(to_);++i_)
#define nep(i,from,to) for(register int i=from;i>=to;--i)
int n,m,p,q,deg[310];
double P,a[310][310];
vector<int> e[310];
inline void addE(int u,int v){e[u].push_back(v);deg[u]++;}
int gauss(int n) {
	int c, r;
	for (c = 0, r = 0; c < n; c ++ ) {
		int t = r;
		for (int i = r; i < n; i ++ ) // 找到绝对值最大的行
			if (fabs(a[i][c]) > fabs(a[t][c]))
				t = i;
		if (fabs(a[t][c]) < eps) continue;
		for (int i = c; i <= n; i ++ ) swap(a[t][i], a[r][i]); // 将绝对值最大的行换到最顶端
		for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // 将当前上的首位变成1
		for (int i = r + 1; i < n; i ++ ) // 用当前行将下面所有的列消成0
			if (fabs(a[i][c]) > eps)
				for (int j = n; j >= c; j -- )
					a[i][j] -= a[r][j] * a[i][c];
		r ++ ;
	}
	if (r < n) {
		for (int i = r; i < n; i ++ )
			if (fabs(a[i][n]) > eps)
				return 2; // 无解
		return 1; // 有无穷多组解
	}
	for (int i = n - 1; i >= 0; i -- )
		for (int j = i + 1; j < n; j ++ )
			a[i][n] -= a[i][j] * a[j][n];
	return 0; // 有唯一解
}
void solve() {
	cin>>n>>m>>p>>q;
	P=((double)p)/q;
	rep(i,1,m){
		int u,v;
		scanf("%d%d",&u,&v);
		addE(u,v);addE(v,u);
	}
	a[0][n]=P;
	rep(i,1,n){
		a[i-1][i-1]=1;
		for(auto j:e[i]){
			a[i-1][j-1]=-(one-P)/deg[j];
		}
	}
	int ret=gauss(n);
	if(ret==0){
		rep(i,1,n){
			printf("%.10lf\n",a[i-1][n]);
		}
	}
}
int main() {
	solve();
	return 0;
}

有关洛谷 P2973 [USACO10HOL]Driving Out the Piggies G ,概率论+高斯消元的更多相关文章

  1. 由于 libgmp.10.dylib 的问题,Ruby 2.2.0 无法运行 - 2

    我刚刚安装了带有RVM的Ruby2.2.0,并尝试使用它得到了这个:$rvmuse2.2.0--defaultUsing/Users/brandon/.rvm/gems/ruby-2.2.0dyld:Librarynotloaded:/usr/local/lib/libgmp.10.dylibReferencedfrom:/Users/brandon/.rvm/rubies/ruby-2.2.0/bin/rubyReason:Incompatiblelibraryversion:rubyrequiresversion13.0.0orlater,butlibgmp.10.dylibpro

  2. ruby - ri 有空文件 – Ubuntu 11.10, Ruby 1.9 - 2

    我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da

  3. ruby-on-rails - gem install rmagick -v 2.13.1 错误 Failed to build gem native extension on Mac OS 10.9.1 - 2

    我已经通过提供MagickWand.h的路径尝试了一切,我安装了命令工具。谁能帮帮我?$geminstallrmagick-v2.13.1Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/ghazanfarali/.rvm/rubies/ruby-1.8.7-p357/bin/rubyextconf.rbcheckingforRubyversion>=1.8.5...yescheckingfor/

  4. ruby - 安装 tiny_tds 在 mac os 10.10.5 上出现错误 - 2

    我正在使用macos,我想使用ruby​​驱动程序连接到sqlserver。我想使用tiny_tds,但它给出了缺少free_tds的错误,但它已经安装了。怎么能过这个?~brewinstallfreetdsWarning:freetds-0.91.112alreadyinstalled~sudogeminstalltiny_tdsBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtiny_tds:ERROR:Failedtobuildgemnativeextension.完整日志如下:/System

  5. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

  6. ruby-on-rails - 在 osx 10.9.3 上使用 RVM 安装 ruby​​-1.9.3-p547 时出错 - 2

    如何解决这个错误:$rvminstall1.9.3Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.9/x86_64/ruby-1.9.3-p547.Continuingwithcompilation.Pleaseread'rvmhelpmount'togetmoreinformationonbinaryrubies.Checkingrequirementsforosx.Certificatesin'/usr/local/etc/openssl/cert.pem'arealr

  7. u盘安装系统(win10为例) - 2

    下载微PE工具箱进入官网下载微PE工具箱-下载 安装好后,打开微PE工具箱客户端,选择安装PE到U盘 PE壁纸可选择自己喜欢的壁纸,勾选上包含DOS工具箱,个性化盘符图标 下载原版系统进入网站下载镜像NEXT,ITELLYOU如果没有账号,注册一下就好进入选择开始使用选择win10 这里我们选择消费者版,用迅雷把BT种子下载下来 下面的两个盘符,是PE工具箱安装进U盘后,分成的盘符,注意EFI的盘符,这里面不能删东西,也不能添东西,另一个盘符可以当做正常的U盘空间使用,我们现在需要把下载下来的景象文件复制到正常的U盘空间中去 这个时候我们的系统U盘就只做好了 安装系统我们将U盘插入电脑,开机,

  8. ruby-on-rails - OSX 10.7.5 - Ruby on Rails LoadError : Could not open library 'sodium' : dlopen(sodium, 5) - 2

    输入rakedb:create后我得到:LoadError:Couldnotopenlibrary'sodium':dlopen(sodium,5):imagenotfound.Couldnotopenlibrary'libsodium.dylib':dlopen(libsodium.dylib,5):imagenotfound这里还有一些输出。/Users/Mao/.rvm/gems/ruby-2.0.0-p451/gems/ffi-1.9.3/lib/ffi/library.rb:133:in`blockinffi_lib'/Users/Mao/.rvm/gems/ruby-2.0

  9. ruby-on-rails - 如何使用 Xcode 4.5.1 在 OSX Lion 10.8.2 上编译 EventMachine gem - 2

    我找遍了所有我能找到的地方,但似乎找不到解决这个问题的办法。我在Lion10.8.2上使用Xcode4.5.1,并尝试为Rails项目运行bundle,但它一直卡在这上面。我正在为Heroku使用Thingem。Bolanos@Jeremys-Mac-mini⦿-1.9.3fishfarm$sudogeminstalleventmachinePassword:Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingeventmachine:ERROR:Failedtobuildgemnativeextens

  10. ruby - Ruby 中允许 "p *1..10"打印出数字 1-10 的功能是什么? - 2

    require'pp'p*1..10这会打印出1-10。为什么这么简洁?您还可以用它做什么? 最佳答案 它是“splat”运算符。它可用于分解数组和范围并在赋值期间收集值。这里收集赋值中的值:a,*b=1,2,3,4=>a=1b=[2,3,4]在此示例中,内部数组([3,4])中的值被分解并收集到包含数组中:a=[1,2,*[3,4]]=>a=[1,2,3,4]您可以定义将参数收集到数组中的函数:deffoo(*args)pargsendfoo(1,2,"three",4)=>[1,2,"three",4]

随机推荐