codevs 2488 绿豆蛙的归宿 题解报告

24 篇文章 0 订阅
10 篇文章 0 订阅

噫。
这里写图片描述

咳咳。。

题目描述 Description
  随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿。

  给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达终点。绿豆蛙从起点出发,走向终点。
  到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K 。
  现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少?

输入描述 Input Description
  第一行: 两个整数 N M,代表图中有N个点、M条边
  第二行到第 1+M 行: 每行3个整数 a b c,代表从a到b有一条长度为c的有向边

输出描述 Output Description
  从起点到终点路径总长度的期望值,四舍五入保留两位小数。

样例输入 Sample Input
4 4
1 2 1
1 3 2
2 3 3
3 4 4

样例输出 Sample Output
7.00

数据范围及提示 Data Size & Hint
  对于20%的数据 N<=100
  对于40%的数据 N<=1000
  对于60%的数据 N<=10000
  对于100%的数据 N<=100000,M<=2*N

嗯,,
好像挺容易的。,
。。
。。
。。。。

给出一张有向无环图。
跑 DFS 或 BFS
找出每个点的出度。
对于每条到达N的路径,沿途计算路程和 每条边 的 kk 值;
kk 为 每个点的出度分之一
最后求积 的 和 。
输出/。

(完全没有拓扑的想法好吧)
代码::

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,p,m,h[100011];
double ans;
stack<int>stac;
struct zqm
{
    int v,next,w;
}q[200001];
void add(int a,int b,int c)
{
    p++;
    q[p].v=b;
    q[p].w=c;
    q[p].next=h[a];
    h[a]=p;
}
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
void dfs(int x,int s,double kk)
{
    if(x==n)
    {
        ans+=(double)s*kk;
        return;
    }
    int pp=h[x],tot=0;
    while(pp)
    {
        tot++;
        pp=q[pp].next;
    }
    pp=h[x];
    while(pp)
    {
        dfs(q[pp].v,s+q[pp].w,kk/(double)tot);
        pp=q[pp].next;
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int q,w,e;
        cin>>q>>w>>e;
        add(q,w,e);
    }
    dfs(1,0,1);
    printf("%.2lf",ans);
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值