P4284 [SHOI2014] Probability Charger

 

P4284 [SHOI2014] Probability Charger

Link: https://www.luogu.org/problemnew/show/P4284

Topic description

The famous electronic product brand SHOI has just released the next generation of electronic products leading the world trend - the probability charger:

"Using a new nano-level processing technology, whether components and wires can be energized is completely determined by true random numbers! SHOI probability charger, an indispensable necessities in your life! Can you charge it? Try it now!"

The SHOI probabilistic charger connects n charging elements by n-1 wires. When charging, whether each wire can conduct electricity is determined by probability, and whether each charging element itself is directly charged is also determined by probability. Electrical energy can then be passed from the directly charged element through the energized wires to enable indirect charging of other charging elements.

As a loyal customer of SHOI Company, you cannot restrain yourself from purchasing SHOI products. After waiting in a long line for a week, I finally got the latest model of the SHOI Probability Charger. You can't wait to plug the SHOI Probabilistic Charger into the power source - at which point you suddenly wonder, what is the expected number of components going into a state of charge?

Input and output format

Input format:

 

The first line contains an integer: n. The number of charging elements of the probabilistic charger. The charging elements are numbered 1-n.

The next n-1 lines, each with three integers a, b, p, describe a wire connected to the charging elements numbered a and b, with a energization probability of p%.

Line n+2 n integers: qi. Indicates that the probability of direct charging of component i is qi%.

 

Output format:

 

Output a line with a real number, which is the expected number of components that can enter the charging state, rounded to 6 decimal places.

 

Input and output example

Input Example #1:  Copy
3
1 2 50
1 3 50
50 0 0
Output Sample #1:  Copy
1.000000
Input Example #2:  Copy
5
1 2 90
1 3 80
1 4 70
1 5 60
100 10 20 30 40
Output Sample #2:  Copy
4.300000

illustrate

For 30% of the data, n≤5000.

For 100% data, n≤500000, 0≤p, qi≤100.

Problem solution: probability dp, it is difficult to find the inverse, and the probability that it cannot be filled

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-6
#define maxn 500005
double f[maxn],g[maxn],h[maxn];
int tot,head[maxn];
struct edge{
    int nxt,to;double w;
}G[maxn * 2 + 10];
void add(int u, int v, double w){
    G[ ++tot].to = v;
    G [tot] .w = w;
    G[tot].nxt = head[u];
    head[u] = tot;
}
void dfs1( int u, int fa){ // son's contribution
    
    for(int i = head[u]; i; i = G[i].nxt){
        int v = G[i].to;
        if(v == fa)continue;
        dfs1(v, u);        
        h[v] = f[v] + ( 1 - f[v]) * ( 1 - G[i].w); // probability of the son not being able to charge the power + probability of the son being charged * the wire is not powered probability 
        f[u] *= h[v];
    }
}
void dfs2( int u, int fa){     // Father's contribution    
    
    for ( int i = head[u]; i; i = G[i].nxt){
         int v = G[i].to;
         if (v == fa) continue ;            
         double t = (f[v] < eps) ? 0 : g[u] * f[u]/h[v]; // The probability that the father can't charge the battery, if he wants to go out, his own contribution to the father 
        g[v] = ( 1 - t)*( 1 - G[i].w) + t;
        dfs2(v, u);
    }
}
/*
    t = g [fa] * f [fa] / f [u]
    g [u] = t + (1 - t)*(1 - w [fa] [u])

*/ 
int main(){
     int n;
    double years = 0 ;
    scanf("%d",&n);
    for(int i = 1; i < n; i++){
        int u, v, c;
        scanf("%d%d%d",&u,&v,&c);
        add(u, v, c/100.0);add(v, u, c/100.0);
    }
    for(int i = 1; i <= n; i++){
        int c;scanf("%d",&c);
        f[i] = 1 - c/100.0;
    }
    dfs1 ( 1 , 0 );
    f[0] = 1; g[1] = 1;
    dfs2(1, 0);
    for(int i = 1; i <= n; i++)
        ans += ( 1 - f[i]*g[i]); // Probability of charging = (1-father and son can't be charged) 
    printf( " %.6lf\n " ,ans);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325321635&siteId=291194637