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)
        A[j][k] -= A[i][k] * A[j][i] / A[i][i];
  }
  for (int i = n; i; --i) {
    A[i][n + 1] /= A[i][i];
    for (int j = i - 1; j; --j)
      A[j][n + 1] -= A[i][n + 1] * A[j][i];
  }
}
double p[M];
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &x[i], &y[i]);
    map[x[i]][y[i]] = map[y[i]][x[i]] = true;
    ++deg[x[i]]; ++deg[y[i]];
  }
  for (int i = 1; i <= n; ++i) {
    A[i][i] = 1.0;
    if (i != n)
      for (int j = 1; j <= n; ++j) if (i != j && map[i][j])
        A[i][j] = -1.0 / deg[j];
    if (i == 1)
      A[i][n + 1] = 1.0;
  }
  solve();
  for (int i = 0; i < m; ++i)
    p[i] = A[x[i]][n + 1] / deg[x[i]] + A[y[i]][n + 1] / deg[y[i]];
  std::sort(p, p + m);
  double ans = .0;
  for (int i = 0; i < m; ++i)
    ans += p[i] * (m - i);
  printf("%.3lf\n", ans);
  return 0;
}