BZOJ3143 [HNOI2013] 游走
2018 年 03 月 14 日发布.
Description
一个$n$个点的图,从$1$开始每次随机选择相邻的边走过去直到走到$n$为止。
现在你要对$m$条边重新从$1$到$m$标号,使得路径上期望边权和最小。问最小值。
Solution
首先高消求出$f_i$表示从$i$点期望出发的次数。那么一条边$(u,v)$经过的次数期望为$\frac{f_u}{deg_u} + \frac{f_v}{deg_v}$($deg$是度数)。然后显然我要给期望经过次数小的边分配大的编号。没了。
Code
#include <algorithm>
#include <cstdio>
const int N = 505;
const int M = 250050;
int n, deg[N], x[M], y[M];
bool map[N][N];
double A[N][N];
double abs(double x) { return x < 0 ? -x : x; }
void solve() {
for (int i = 1; i <= n; ++i) {
int j = i;
for (int k = i + 1; k <= n; ++k) if (abs(A[k][i]) > abs(A[j][i])) j = k;
for (int k = i; k <= n + 1; ++k) std::swap(A[i][k], A[j][k]);
for (int j = i + 1; j <= n; ++j)
for (int k = n + 1; k >= i; --k)
[j][k] -= A[i][k] * A[j][i] / A[i][i];
A}
for (int i = n; i; --i) {
[i][n + 1] /= A[i][i];
Afor (int j = i - 1; j; --j)
[j][n + 1] -= A[i][n + 1] * A[j][i];
A}
}
double p[M];
int main() {
int m;
("%d%d", &n, &m);
scanffor (int i = 0; i < m; ++i) {
("%d%d", &x[i], &y[i]);
scanf[x[i]][y[i]] = map[y[i]][x[i]] = true;
map++deg[x[i]]; ++deg[y[i]];
}
for (int i = 1; i <= n; ++i) {
[i][i] = 1.0;
Aif (i != n)
for (int j = 1; j <= n; ++j) if (i != j && map[i][j])
[i][j] = -1.0 / deg[j];
Aif (i == 1)
[i][n + 1] = 1.0;
A}
();
solvefor (int i = 0; i < m; ++i)
[i] = A[x[i]][n + 1] / deg[x[i]] + A[y[i]][n + 1] / deg[y[i]];
pstd::sort(p, p + m);
double ans = .0;
for (int i = 0; i < m; ++i)
+= p[i] * (m - i);
ans ("%.3lf\n", ans);
printfreturn 0;
}