If the simulation is pure, the loop will die, and as the expectation of each point of the loop approximates a value, the Gaussian elimination element will find the value by the column-founder group.
#include <cstdio> #include <cctype> #include <cstring> #include <algorithm>using namespace std; Const double Eps=1e-9;bool vis[503];d ouble f[503],a[503][503],ans[500*500];int n,m,cnt=0,du[503],a1[500*500],a2[500 *500];d ouble fabs (double x) {return x>0?x:-x;} int Getint () {char c;while (!isdigit (C=getchar ())), int a=c-' 0 '; while (IsDigit (C=getchar ())) a=a*10+c-' 0 '; return A; void Prepare () {for (int i=1;i<=m;++i) {a[a1[i]][a2[i]]+=1.0/du[a2[i]]; A[a2[i]][a1[i]]+=1.0/du[a1[i]]; } for (int i=1;i<=n;++i) a[n][i]=0; for (int i=1;i<n;++i) a[i][i]=-1.0; a[1][n+1]=-1.0;a[n][n]=1.0;} void Swapp (double &x,double &y) {double z=x;x=y;y=z;} void Gauss () {for (int i=1;i<=n;++i) {int now=i; for (int j=i+1;j<=n;++j) if (Fabs (A[j][i]) >fabs (A[now][i])) now=j; if (now!=i) for (int j=i;j<=n+1;++j) Swapp (A[now][j],a[i][j]); for (int j=i+1;j<=n;++j) {double t=a[j][i]/a[i][i]; for (int k=i;k<=n+1;++k)A[J][K]-=T*A[I][K]; }} for (int i=n;i>=1;--i) {for (int j=n;j>i;--j) {a[i][n+1]-=a[j][n+1]*a[i][j]; }a[i][n+1]/=a[i][i]; }}bool CMP (double a,double b) {return a>b;} int main () {memset (a,0,sizeof (a)); memset (du,0,sizeof (du)); N=getint (); M=getint (); for (int i=1;i<=m;++i) {a1[i]=getint (); A2[i]=getint (); du[a1[i]]++;d u[a2[i]]++; }prepare (); Gauss (); cnt=0; for (int i=1;i<=m;++i) {ans[++cnt]=a[a1[i]][n+1]/du[a1[i]]+a[a2[i]][n+1]/du[a2[i]]; } sort (ans+1,ans+m+1,cmp); Double sa=0; for (int i=1;i<=m;++i) sa+=ans[i]*i*1.0; printf ("%.3lf\n", SA); return 0;}
That's it.
"Bzoj 3143" "Hnoi2013" walk expectation + Gaussian elimination