[Expected dp+ Gauss Elimination] BZOJ3143: [Hnoi2013] Walk __ Math-related

Source: Internet
Author: User
the

A undirected connected graph with vertices numbered from 1 to n, edges numbered from 1 to M.
The small z carries on the random walk on the graph, at the initial time the small z is at number 1th vertex.
Each step of small z randomly chooses to go out from the current vertex with equal probability, and gets the score equal to the number of the edge.
When little z reaches the n vertex, the total score is the sum of all the points obtained.
Now, you're going to number the M bar so that the small z has the lowest expected score.
n<=500

It's a bit tricky to directly calculate what the problem requires.
First, there is no relation between the assignment of the edge right and the small Z random walk,
If we can know the expected number of times each side passes, we will number 1~m from big to small as expected.
The question is, how do you know how many times each side is expected to go?
It takes a good step to transform, set E[i] is the expected number of visits to point I, d[i] is the degree of point I, E (x,y) indicates the expectation of the edge computes, then there are: E (x,y) =e[x]/d[x]+e[y]/d[y] e (x,y) = E[x]/d[x] + e[y]/ D[y]
This problem is transformed to seek e[i], establish the relationship between expectations, Gauss elimination can be.

Complexity O (n3) O (n^3)

#include <cstdio> #include <cmath> #include <algorithm> using namespace std;
const int MAXN=505,MAXE=MAXN*MAXN;
    struct edge{int x,y double w;
    BOOL operator < (const Edge &b) const{return w<b.w;
}} Es[maxe];
int N,M,D[MAXN];
Double ANS,E[MAXN],K[MAXN][MAXN];
    void Gauss () {n--;
        for (int i=1;i<=n;i++) {int where=0;
        for (int j=i;j<=n;j++) if (Fabs (k[j][i)) >fabs (k[where][i)) where=j;
        for (int j=0;j<=n;j++) swap (k[where][j],k[i][j]);
            for (int j=i+1;j<=n;j++) {double t=k[j][i]/k[i][i];
        for (int k=0;k<=n;k++) k[j][k]-=t*k[i][k];
        for (int i=n;i>=1;i--) {e[i]=-k[i][0];
        for (int j=i+1;j<=n;j++) E[I]-=K[I][J]*E[J];
    E[i]/=k[i][i];
} n++;
    int main () {freopen ("bzoj3143.in", "R", stdin);
    Freopen ("Bzoj3143.out", "w", stdout);
    scanf ("%d%d", &n,&m); for (int i=1;i<=m;i++) scanf ("%d%d", &es[i].x,&eS[I].Y), d[es[i].x]++, d[es[i].y]++;
        for (int i=1;i<=m;i++) if (es[i].x!=n&&es[i].y!=n) {K[ES[I].X][ES[I].Y]+=1.0/D[ES[I].Y];
    K[es[i].y][es[i].x]+=1.0/d[es[i].x];
    } k[1][0]=1;
    for (int i=1;i<=n-1;i++) k[i][i]=-1;
    Gauss ();
    for (int i=1;i<=m;i++) ES[I].W=E[ES[I].X]/D[ES[I].X]+E[ES[I].Y]/D[ES[I].Y];
    Sort (es+1,es+1+m);
    for (int i=1;i<=m;i++) ans+=es[i].w* (m-i+1);
    printf ("%.3lf", 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.