D title Robots [expectations]

Robots

Given a directed graph with no loops which starts at node 11 and ends at node nn.
There is a robot who starts at 11, and will go to one of adjacent nodes or stand still with equal probability every day.
Every day the robot will have durability consumption which equals to the number of passed days.
Please calculate the expected durability consumption when the robot arrives at node nn.
It is guaranteed that there is only one node (node 1) whose in-degree is equal to 0,
and there is only one node (node n) whose out-degree is equal to 0. And there are no multiple edges in the graph.

Input
The first line contains one integer T (1≤T≤10)
For each case,the first line contains two integers n (2≤n≤10^5) and m (1≤m≤2×10^5), the number of nodes and the number of edges, respectively.
Each of the next mm lines contains two integers u and v (1≤u,v≤n) denoting a directed edge from u to v.
It is guarenteed that ∑n≤4×10^5, and ∑m≤5×10^5.

Output
Output T lines.Each line have a number denoting the expected durability consumption when the robot arrives at node n.
Please keep two decimal places.


Sample input
. 1
. 5. 6
. 1 2
2. 5
. 1. 5
. 1. 3
. 3. 4
. 4. 5
Sample Output
9.78

translation:

Given a directed graph without loop, which starts from node 11, end node nn.
There is a robot from the age of 11, have reached a neighboring node with the same probability every day, or standing still.
Robot day durability expenditure is equivalent to the number of days past.
Calculate the robot to reach the expected durability consumption when the node nn.
Ensure that only one node (node 1) into 0 degree,
and only the degree of a node (node n) is equal to zero. FIG no more than one edge
input
of the first row contains an integer T (1≤T≤10)
In each case, the first line contains two integers n (2≤n≤10 ^ 5) and m (1≤m≤ 2 × 10 ^ 5), respectively, the number of nodes and edges.
The next mm lines each comprising two integers u and v (1≤u, v≤n), said directed edge from u to v.
Σn≤4 × 10 ^ 5, Σm≤5 × 10 ^ 5.

Output
the output line. Each row has a number indicating the durability of the robot to reach the expected consumption when the node n.
Keep two decimal places.

 

 

Official Solution:

dp [i]: 1 to the number of days of the desired N;

f [i]: the cost of a desired cost;

out [i] represents the degree point;

Days expectations equation

dp[i]  =   1/( out[i] + 1 )*dp[i]  + 1/( out[i] + 1  )*∑dp[j]  + 1;

Portable after simplifying:

dp[i]  = 1/out[i]*∑dp[j]  + 1 +  1/out[i];

Expect to spend:

f[i]=1/(   out[i] +  1 )*f[i]  +  1/( out[i] + 1)*∑f[j]  +  dp[i];

After the shift of the inter formula:

f[i]  =  1/out[i]*∑f[j] + (out[i] + 1) / out[i] * dp [ i ];

 

ac Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 110000;
int t,n,m;
int out[2*N];
double dp[2*N],f[2*N];
vector<int> v[2*N];
void init(){
    for(int i=0;i<=n;i++){
        v[i].clear();
        dp[i] = 0;
        f[i] = 0;
        out[i] = 0;
    }
}
double dfsdp(int x){
    if(dp[x]!=0) return dp[x];
    if(out[x] == 0)  return dp[x];
    int len = v[x].size();
    double s1=0;
    for(int i=0;i<len;i++){
        int e=v[x][i];
        dfsdp(e);
        s1+=dp[e];
    }
    dp[x]=s1/out[x]*1.0+1+1.0/out[x];
    return dp[x];
}


double dfsf(int x){
    if(f[x]!=0) return f[x];
    if(out[x] == 0)  return f[x];
    int len = v[x].size();
    double s1=0;
    for(int i=0;i<len;i++){
        int e=v[x][i];
        dfsf(e);
        s1+=f[e];
    }
    f[x]=s1/out[x]*1.0+(1+1.0/out[x]*1.0)*dp[x];
    return f[x];
}

int main(){

 int a,b;
    int t;
    cin>>t;
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            out[a]++;
            v[a].push_back(b);
        }
        dfsdp(1);
        dfsf(1);
        printf("%.2f\n",f[1]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lusiqi/p/11491056.html