P3232 [HNOI2013]游走(概率期望,高斯消元解决流量关系,随机函数变形)
作者:互联网
题意:一个人初始化时在1号结点,给出一张双向联通图,每次这个人等概率的走到相邻的一个点,走到n结点游戏结束。 走过一条边时获得这条边的编号的分数。现在要求你给这些边编号使得最后他的期望值最低。
分析与总结:显然要求每条边经过次数的期望,设边(u->x)的期望是
e
x
p
(
u
−
>
x
)
exp(u->x)
exp(u−>x),那么
e
x
p
(
u
−
>
x
)
=
e
x
p
(
u
)
/
d
[
u
]
+
e
x
p
[
x
]
/
d
[
x
]
exp(u->x)=exp(u)/d[u]+exp[x]/d[x]
exp(u−>x)=exp(u)/d[u]+exp[x]/d[x],意思很明显,就是边的期望就是他的两个端点期望经过次数/这两个点的度。ok,问题转化成求点的期望经过次数,那么这道题是不是很像经典题—随机函数呢,是的,列一个n元1次方程组用高斯消元求下每一个点期望经过多少次。注意特别考虑1号结点和n号结点的情况。
#include<bits/stdc++.h>
#define f(i,a,b) for( int i=a;i<=b;++i)
#define ff(i,a,b) for( int i=a;i>=b;--i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const int inf = 0x3f3f3f3f;
const double tiaohe = 0.57721566490153286060651209;
ll oula(ll x) { ll res = x;f(i, 2, x / i) { if (x % i == 0) { res = res / i * (i - 1);while (x % i == 0) x /= i; } }if (x > 1) res = res / x * (x - 1);return res; }
ll quickmod(ll a, ll n, ll m) { ll s = 1;while (n) { if (n & 1) { s = s * a % m; }a = (a*a) % m;n = n / 2; }return s; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { if (!b) { d = a, x = 1, y = 0; } else { ex_gcd(b, a % b, y, x, d);y -= x * (a / b); } }
ll inv(ll t, ll p) { ll d, x, y;ex_gcd(t, p, x, y, d);return d == 1 ? (x % p + p) % p : -1; }
bool isPrime(ll x) { if (x == 2)return true;if (x % 2 == 0)return false;for (ll i = 2;i*i <= x;i++) if (x % i == 0)return false; return true; }
inline int in() { char ch = getchar();int x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; }
//double a = log(n) +tiaohe + 1.0 / (2 * n);
double eqa = (1 + sqrt(5.0)) / 2.0;
double E = 2.7182818284;
const double eps = 1e-6;
const int N = 505;
int d[N];
vector<int> G[N];
int n, m;
double f[N][N];
void gauss_jordan()
{
f(i, 0, n - 1)
{
int r = i;
f(j, i + 1, n - 1)
if (fabs(f[j][i]) > fabs(f[r][i]))r = j;
if (fabs(f[r][i]) < eps)continue;
if (r != i)f(j, 0, n)swap(f[r][j], f[i][j]);
f(k, 0, n - 1)
if (k != i)ff(j, n, i)f[k][j] -= f[k][i] / f[i][i] * f[i][j];
}
}
double mp[N][N];
double g[N*N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
cin >> n >> m;
f(i, 1, m)
{
int x = in(), y = in();
x--, y--;
G[x].emplace_back(y);
G[y].emplace_back(x);
d[x]++, d[y]++;
}
f[0][n - 1] = 1;
f(i, 0, n - 2)
{
f[i][i] = 1;
for (auto I : G[i])
if (I != n - 1)
f[i][I] = -1.0 / d[I];
}
gauss_jordan();
f(i, 0, n - 2)
for (auto I : G[i])
mp[i][I] += (f[i][n - 1] / f[i][i]) / d[i];
int id = 0;
f(i, 0, n - 1)
{
f(j, i, n - 1) {
if (mp[i][j] > eps)
{
g[++id] = mp[i][j];
g[id] += mp[j][i];
}
}
}
double res = 0.0;
sort(g + 1, g + 1 + id, greater<double>());
f(i, 1, m)res += i * g[i];
printf("%.3lf\n", res);
return 0;
}
标签:ch,return,int,res,ll,HNOI2013,P3232,double,高斯消 来源: https://blog.csdn.net/qq_43543086/article/details/110766545