HNOI2013 游走

传送门

这道题好好玩啊……我也想自♂由的游走……

首先我们肯定是把期望越大的边权值设置的越小。边的期望显然是由点的期望决定的,就是两个点的期望除以其点度的和。

所以我们转化为求点的期望。点的期望是可以由周围的点更新的。我们设\(x_1,x_2……x_k\)都是与当前点x相连的点,那么就有:

\[ans[x] = \sum_{i=1}^k\frac{ans[i]}{deg[i]}\]

然后我们只要把所有的ans看成一组方程组的未知量用高斯消元解一下就好了。

注意的问题是,第n个点因为到达了就不能继续游走,所以期望不计。然后因为一开始就在1点,所以,特殊的,1点的期望是\(\sum_{i=1}^k\frac{ans[i]}{deg[i]} + 1\),这个要特别注意。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define fr friend inline
#define y1 poj
#define mp make_pair
#define pr pair<int,int>
#define fi first
#define sc second
#define pb push_back

using namespace std;
typedef long long ll;
const int M = 500005;
const int N = 1005;
const int INF = 1000000009;
const double eps = 1e-7;

int read()
{
   int ans = 0,op = 1;char ch = getchar();
   while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
   while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
   return ans * op;
}

struct edge
{
   int next,to,from,v;
}e[M<<1];

int n,head[N],ecnt,m,x,y;
double a[N][N],ans[N],deg[N],ex[M<<1],tot;

void add(int x,int y)
{
   e[++ecnt].to = y;
   e[ecnt].from = x;
   e[ecnt].next = head[x];
   head[x] = ecnt;
}

void build(int k)
{
   a[k][k] = -1.0;
   for(int i = head[k];i;i = e[i].next)
   {
      int t = e[i].to;
      if(t != n) a[k][t] = 1.0 / deg[t];
   }
}

void gauss()
{
   rep(i,1,n-1)
   {
      int r = i;
      rep(j,i+1,n-1) if(fabs(a[r][i]) < fabs(a[j][i])) r = j;
      if(r != i) swap(a[i],a[r]);
      double div = a[i][i];
      rep(j,i,n) a[i][j] /= div;
      rep(j,i+1,n-1)
      {
     div = a[j][i];
     rep(k,i,n) a[j][k] -= a[i][k] * div;
      }
      per(i,n-1,1)
      {
     ans[i] = a[i][n];
     rep(j,i+1,n-1) ans[i] -= a[i][j] * ans[j];
      }
   }
}

int main()
{
   n = read(),m = read();
   rep(i,1,m) x = read(),y = read(),add(x,y),add(y,x),deg[x]++,deg[y]++;
   rep(i,1,n-1) build(i);
   a[1][n] = -1.0;
   gauss();
   rep(i,1,m)
   {
      int kx = e[i<<1].from,ky = e[i<<1].to;
      ex[i] = ans[kx] / deg[kx] + ans[ky] / deg[ky];
   }
   sort(ex+1,ex+1+m);
   rep(i,1,m) tot += ex[m-i+1] * (double)i;
   printf("%.3lf\n",tot);
   return 0;
}

猜你喜欢

转载自www.cnblogs.com/captain1/p/10161187.html