HNOI2013 遊走
阿新 • • 發佈:2018-12-22
這道題好好玩啊……我也想自♂由的遊走……
首先我們肯定是把期望越大的邊權值設定的越小。邊的期望顯然是由點的期望決定的,就是兩個點的期望除以其點度的和。
所以我們轉化為求點的期望。點的期望是可以由周圍的點更新的。我們設\(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; }