Bzoj 3143 [Hnoi2013] travel higher than the yuan

Source: Internet
Author: User

Topic: Given an n-point M edge of the undirected connected graph. A random walk on the graph, starting at the 1th vertex, each step randomly selects an edge of the current vertex at an equal probability, walks along this edge to the next vertex, obtains the number equal to the edge, and ends the walk at the N vertex, the sum of all the scores obtained. The M-bar is numbered to minimize the expected total score.

Each side of the random number, it is natural to make the number of the side of a lot of walking as small as possible. The problem turns to the desired number of each edge, and the number of passes is shifted by the point, the first requirement is the expected number of times of each point. The expected number of starting points must be 1, and the expected number of endpoints cannot be transferred to other points. In addition to the starting and ending points, each point may be transferred from the neighboring point, so the desired number of points of a point F (i) =sigma{j and I have a connecting edge | f (j) *p (J)}, where I is not equal to the starting point or end point, p (j) represents p through other probabilities, the solution equation can be.

#include <cstdio> #include <algorithm> #include <cmath> #define N 505 using namespace std;
const double EPS=1E-10;
    struct Edge {int u,v;
    Double Val; BOOL operator < (const edge& RHS) const {return val>rhs.val;}}
E[n*n];
int n,m,d[n];
Double P[n],a[n][n];
        void Gauss () {for (int i=1;i<=n;i++) {int J;
        for (j=i;j<=n;j++) if (Fabs (A[j][i]) >eps) break;
        if (j>n) continue;
        if (j!=i) for (int k=1;k<=n+1;k++) swap (a[i][k],a[j][k]);
            for (j=i+1;j<=n;j++) {if (Fabs (a[j][i)) <eps) continue;
            Double Tmp=a[j][i]/a[i][i];
        for (int k=i;k<=n+1;k++) a[j][k]-=a[i][k]*tmp;
        }} for (int i=n;i;i--) {for (int j=i+1;j<=n;j++) a[i][n+1]-=a[i][j]*a[j][n+1];
    A[i][n+1]/=a[i][i];
} return;
    } int main () {scanf ("%d%d", &n,&m); for (int i=1;i<=m;i++) {scanf ("%d%d ", &AMP;E[I].U,&AMP;E[I].V);
    d[e[i].u]++, d[e[i].v]++;
    } for (int i=1;i<=n;i++) p[i]=1.0/(double) d[i];
    for (int i=1;i<=m;i++) A[E[I].U][E[I].V]+=P[E[I].V], a[e[i].v][e[i].u]+=p[e[i].u];
    for (int i=1;i<=n;i++) a[n][i]=0, a[i][i]=-1;
    A[1][n+1]=-1;
    Gauss ();
    for (int i=1;i<=m;i++) e[i].val=p[e[i].u]*a[e[i].u][n+1]+p[e[i].v]*a[e[i].v][n+1];
    Sort (e+1,e+1+m);
    Double ans=0;
    for (int i=1;i<=m;i++) ans+=e[i].val* (double) I;
    printf ("%.3f\n", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.