• 【洛谷P2973】Driving Out the Piggies


    题目

    题目链接:https://www.luogu.com.cn/problem/P2973
    给定一张 \(n\) 个点的无向图,一开始 \(WYCdalao\) 在 1 号节点,每次这个点有 \(\frac{p}{q}\) 的概率停止,\((1-\frac{p}{q})\) 的概率随机走一条与该点相邻的边。求 \(WYCdalao\) 最终在每一个节点停止的期望。
    \(2\leq n\leq 300\)

    思路:

    这种数据小的期望题很有可能考的就是高斯消元。
    首先如果我们经过了一个点 \(k\) 次,那么在这个点停止的概率就是 \(\frac{kp}{q}\)
    \(f[x]\) 表示到达点 \(x\) 的期望次数,那么有

    \[f[x]=\sum_{(x,y)}\frac{(1-\frac{p}{q})f[y]}{deg[y]} \]

    其中 \(deg[x]\) 表示 \(x\) 的度数。初始化 \(f[x]=1\)
    这个方程有后效性,所以把等号右边移项到左边,就是高斯消元板子了。
    时间复杂度 \(O(n^3+m)\)

    代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long double ld;
    
    const int N=310,M=45000;
    const ld zero=1e-14;
    int n,m,deg[N];
    ld p,q,a[N][N];
    
    struct edge
    {
    	int u,v;
    }e[M];
    
    void gauss()
    {
    	for (int i=1;i<=n;i++)
    	{
    		for (int j=i+1;j<=n;j++)
    			if (a[i][j]>zero)
    			{
    				for (int k=1;k<=n;k++)
    					swap(a[j][k],a[i][k]);
    				break;
    			}
    		for (int j=i+1;j<=n;j++)
    		{
    			ld rate=a[j][i]/a[i][i];
    			for (int k=0;k<=n;k++)
    				a[j][k]-=a[i][k]*rate;
    		}
    	}
    	for (int i=n;i>=1;i--)
    	{
    		for (int j=i+1;j<=n;j++)
    			a[i][0]-=a[j][0]*a[i][j];
    		a[i][0]/=a[i][i];
    	}
    }
    
    int main()
    {
    	scanf("%d%d",&n,&m);
    	scanf("%Lf%Lf",&p,&q);
    	p/=q;
    	for (int i=1,x,y;i<=m;i++)
    	{
    		scanf("%d%d",&x,&y);
    		e[i].u=x; e[i].v=y;
    		deg[x]++; deg[y]++;
    	}
    	for (int i=1;i<=m;i++)
    	{
    		int x=e[i].u,y=e[i].v;
    		a[x][y]=-(1.0-p)/deg[y];
    		a[y][x]=-(1.0-p)/deg[x];
    	}
    	for (int i=1;i<=n;i++)
    		a[i][i]=1;
    	a[1][0]=1;
    	gauss();
    	for (int i=1;i<=n;i++)
    		printf("%0.9Lf\n",a[i][0]*p);
    	return 0;
    }
    
  • 相关阅读:
    Find all Windows Phone Application Bar Icon
    DELL PowerConnect 5548[初步]配置
    删除SD的分区
    Import/Export a database into Oracle
    索爱U100i排线更换笔记
    forrtl: severe (174): SIGSEGV, segmentation fault occurred
    下载功能和上传功能的实现
    自我小结:手动给GridView添加数据源
    删除指定目录内的文件
    GridView数据源中没有数据的时候显示表头
  • 原文地址:https://www.cnblogs.com/stoorz/p/12384250.html
Copyright © 2020-2023  润新知