[codevs2488]绿豆蛙的归宿

题目←

思路:
求距终点距离的递推式:
dis[f] = dis[t] + l[i].v
加上期望
E[f] = E[t] +l[i].v
这是对于f只有t一条出边的情况
多条出边时,设G为f所有出边的集合

E[f]=eGE[e.t]+e.vout[f]

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double E[200000];
int n,m;
struct edge{
    int f,t;
    double v;
}l[200000 << 1];
int head[200000],next[400000],tot;
void init(int n){
    for(int i = 1;i <= n;i ++){
        head[i] = -1;
    }
}
void build(int f,int t,double v){
    l[++tot] = (edge){f,t,v};
    next[tot] = head[f];
    head[f] = tot;
}
int a,b;
double c;
int ru[200000],topo[200000],chu[200000];
int hd,tl;
int main(){
    scanf("%d%d",&n,&m);
    init(n);
    for(int i = 1;i <= m;i ++){
        scanf("%d%d%lf",&a,&b,&c);
        build(a,b,c);
        ru[b] ++;
        chu[a] ++;
    }
    for(int i = 1;i <= n;i ++){
        if(!ru[i]){
            topo[tl ++] = i;
        }
    }
    while(hd != tl){
        int u = topo[hd];
        hd ++;
        for(int i = head[u];i != -1;i = next[i]){
            int t = l[i].t;
            ru[t] --;
            if(!ru[t]){
                topo[tl ++] = t;
            }
        }
    }
    for(int i = n - 1;i >= 0;i --){
        int u = topo[i];
        if(head[u] == -1)continue;
        for(int j = head[u];j != -1;j = next[j]){
            int t = l[j].t;
            E[u] += (E[t] + l[j].v)/chu[u];
        }
    }
    printf("%.2lf",E[1]);
}

注意最后不会到达n的边也是对答案有贡献的(可能被走过)


又看了某大佬的解法……分别计算每条边对答案的贡献
dp[f]->f可能被经过的次数
eGf ,则e对答案贡献为

dp[f]e.vout[f]

dp[t]可由 dp[f] 推来

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double E[200000];
int n,m;
struct edge{
    int f,t;
    double v;
}l[200000 << 1];
int head[200000],next[400000],tot;
void init(int n){
    for(int i = 1;i <= n;i ++){
        head[i] = -1;
    }
}
void build(int f,int t,double v){
    l[++tot] = (edge){f,t,v};
    next[tot] = head[f];
    head[f] = tot;
}
int a,b;
double c;
int ru[200000],topo[200000],chu[200000];
int hd,tl;
double ans;
int main(){
    scanf("%d%d",&n,&m);
    init(n);
    for(int i = 1;i <= m;i ++){
        scanf("%d%d%lf",&a,&b,&c);
        build(a,b,c);
        ru[b] ++;
        chu[a] ++;
    }
    for(int i = 1;i <= n;i ++){
        if(!ru[i]){
            topo[tl ++] = i;
        }
    }
    E[1] = 1.00;
    while(hd != tl){
        int u = topo[hd];
        hd ++;
        for(int i = head[u];i != -1;i = next[i]){
            int t = l[i].t;
            ru[t] --;
            E[t] += E[u]/chu[u];
            ans += E[u]*l[i].v/chu[u];
            if(!ru[t]){
                topo[tl ++] = t;
            }
        }
    }
    printf("%.2lf",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值