Luo Valley P4316 frog destination (probability DP / DP + desired topological sort)

Topic background

With the launch of the new version of Baidu Space, Blog pet frog to complete its mission, to find it a new home.

Title Description

Zhang gives NN n-point mm m edges of the directed acyclic graph, the starting point is . 11 1 end of NN n-, each edge has a length, and starting from the starting point to reach all the points, all the points are also We are able to reach the end.

Frog from the starting point, towards the end. Each vertex is reached, if the node has KK K the article edges, frog can select any one point away from the edge, and to each side of the probability 1K \ FRAC. 1} {K} { K . 1 . Now frog would like to know, from the beginning to an end of a path through which the total length of expectations is how much?

Input Format

The first line of input two integers, representing the number of points of FIG NN n-number of sides and mm m.

Of 22 is 2 to (m +. 1) (m +. 1) ( m + . 1 ) rows, each row having three integers U, V, Wu, V, W U , V , W, indicates the presence of a from Uu U pointing vv v length WW W directed edge.

Output Format

Output a real number line represents the answer, rounded to two decimal places.

Sample input and output

Input # 1
4 4 
1 2 1 
1 3 2 
2 3 3 
3 4 4
Output # 1
7.00 

and before AtCoder that question is very similar, but the problem is already point number descending order according to the topology row, so you can traverse directly from largest to smallest, and this problem had to sort side edge dp. DP [i] n the representative path from i to a desired length, the transfer equation: dp [i] = (Σdp [k] + edge [i, k]) / degree [i], where i k is an edge end, edge [i, k] is a k-edge length to i, degree [i] is an i in degrees. Starting from the end point to the need for anti FIG recursive, while two of the memory arrays, out array as with the transfer equation, OUT1 of the array as a topological sort (not seemingly two arrays QuQ).
#include <bits / STDC ++ H.>
 #define N 100005
 #define M 200005
 the using  namespace STD;
 int n-, m, head [N], Ver [M], Edge [M], the Next [M], TOT = 0 ;
 Double DP [N];
 int  oUT [N] = { 0 }, OUT1 of [N] = { 0 }; // oUT [i] i represents the point of the (original, not the anti FIG) 
void the Add ( int X, int Y, int Z)
{
    ver [ ++ tot] = y; edge [tot] = z;
    Next[tot]=head[x],head[x]=tot;
}
void process()
{
    queue<int>q;
    q.push(n);
    DP [n-] = 0 ; 
     the while (q.size ()) // topological sort 
    {
         int pre = q.front ();
        q.pop();
        int i;
        for(i=head[pre];i;i=Next[i])
        {
            int y=ver[i],z=edge[i];
            DP [Y] + = (DP [pre] + Z) * 1.0 / OUT [Y];
             IF (--out1 [Y] == 0 ) // Topological Sort 
            {
                q.push(y);
            }
        }
    }
}
int main ()
{
    cin>>n>>m;
    int i;
    for(i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        the Add (Y, X, Z); // backstepping built trans FIG 
        OUT [X] ++ ;
        out1[x]++;
    }
    process();
    printf("%.2lf",dp[1]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12548153.html