"HNOI2013" walk

Portal

Description

Random walk, the cost of each edge is the edge number, from Q \ (1 \) to the \ (n-\) a minimum desired distance

You need to label edge

Solution

The answer is
\ [\ sum_ {i = 1
} ^ m E [i] \ times id [i] \] so the desired number of passes of each edge row count out a sequence like

Side of the desired number of passes may be converted to point
\ [E [<u, v >] = \ frac {F [u]} {deg [u]} + \ frac {F [v]} {deg [v]} \]
so long as the number of times each point is calculated through just fine

So that
\ [F [i] = [
i = 1] + \ sum _ {<i, j>, j \ ne n} \ frac {F [j]} {deg [j]} \] Therefore Gaussian elimination on All right


Code 

#include<bits/stdc++.h>
#define ll long long
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
#define dbg3(x) cerr<<#x<<"\n"
using namespace std;
#define reg register
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
const int MN=505;
int deg[MN],n,m,u[MN*MN],v[MN*MN];
#define db double
db E[MN*MN],F[MN],b[MN][MN],ans=0.;
std::vector<int> G[MN];
void Gauss()
{
    reg int i,j,k;
    for(i=1;i<n;++i)
    {
        int p=i;
        for(j=i+1;j<n;++j)if(fabs(b[j][i])>fabs(b[p][i]))p=j;
        if(i!=p)swap(b[i],b[p]);
        for(j=i+1;j<n;++j)
        {
            db d=b[j][i]/b[i][i];
            for(k=i;k<=n;++k) b[j][k]-=d*b[i][k];
        }
    }
    for(i=n-1;i;--i)
    {
        for(j=i+1;j<n;++j)b[i][n]-=F[j]*b[i][j];
        F[i]=b[i][n]/b[i][i];
    }
}
int main()
{
    n=read();m=read();
    reg int i,j,x,y;
    for(i=1;i<=m;++i)
    {
        u[i]=x=read();v[i]=y=read();
        G[x].push_back(y);G[y].push_back(x);
        ++deg[x];++deg[y];
    }
    for(b[1][n]=i=1;i<n;++i)
    {
        b[i][i]=1.;
        for(j=0;j<G[i].size();++j)
        if(G[i][j]!=n)
        b[i][G[i][j]]=-1./deg[G[i][j]];
    }
    Gauss();
    for(i=1;i<=m;++i)
    {
        if(u[i]!=n)E[i]+=F[u[i]]/(db)deg[u[i]];
        if(v[i]!=n)E[i]+=F[v[i]]/(db)deg[v[i]];
    }
    std::sort(E+1,E+m+1);
    for(i=1;i<=m;++i)ans+=(db)i*E[m-i+1];
    return 0*printf("%.3lf\n",ans);
}



Blog from PaperCloud , without permission, please do not reprint, TKS!

Guess you like

Origin www.cnblogs.com/PaperCloud/p/11872955.html