P4316 绿豆蛙的归宿

因为非要用bfs所以稍微麻烦一点qwq(大家用的都是dfs)

其实问题让我们求的就是经过每条边的概率*边权之和

我们可以用bfs把图遍历一遍处理概率,顺便把每条边的概率*边权存到这条边的终点

最后把每个点的答案累加起来,答案就出来了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cctype>
using namespace std;
inline int Int(){
char c=getchar(); int x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
return x;
}
const int maxn=;
int n,m,cnt,siz[maxn],hd[maxn],ed[maxn],nxt[maxn<<],to[maxn<<],in[maxn];
double ans,dis[maxn<<],f[maxn],p[maxn];
inline void add_edge(int x,int y,int v){
nxt[ed[x]]=++cnt, ed[x]=cnt, hd[x]= hd[x] ?hd[x]:cnt;
++siz[x], ++in[y], to[cnt]=y, dis[cnt]=(double)v;
}
void bfs(int st){//bfs处理
queue <int> h;
h.push(st); p[st]=1.0; //初始概率为1
while(!h.empty()){
int x=h.front(); h.pop();
double Q=p[x]/(double)siz[x]; //到从该点经过每条边的概率
for(int i=hd[x];i;i=nxt[i]){
p[to[i]]+=Q, f[to[i]]+=dis[i]*Q; --in[to[i]]; //p数组存储概率,f数组存储所求答案
if(in[to[i]]<=) h.push(to[i]);
}
}
}
int main(){
n=Int(); m=Int();
int q1,q2,q3;
for(int i=;i<=m;++i){
q1=Int(); q2=Int(); q3=Int();
add_edge(q1,q2,q3);
}
bfs();
for(int i=;i<=n;++i) ans+=f[i]; //累加答案
printf("%.2lf",ans);
return ;
}

P4316 绿豆蛙的归宿(期望)的更多相关文章

  1. P4316 绿豆蛙的归宿 期望DP

    P4316 绿豆蛙的归宿 期望DP DAG上,每条边有边权,走向相连每条路的概率相等,问从起点到终点所经过的路径总长度期望 因为发现终点走到终点期望为0,定义\(f[i]\)从终点走到\(i\)所经过 ...

  2. 洛谷P4316 绿豆蛙的归宿(期望)

    题意翻译 「Poetize3」 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出 ...

  3. 洛谷P4316绿豆蛙的归宿——期望

    题目:https://www.luogu.org/problemnew/show/P4316 期望水题,从终点向起点推,因为是DAG,所以拓扑序推过去即可. 代码如下: #include<ios ...

  4. 洛谷$P4316$ 绿豆蛙的归宿 期望

    正解:期望 解题报告: 传送门! 看懂题目还是挺水的$(bushi$ 三个方法,但因为题目太水了懒得一一介绍了,,,反正都是期望,,,$so$随便港个最简单的趴$QwQ$ 直接考虑每条边的贡献,就会是 ...

  5. [luogu]P4316 绿豆蛙的归宿(拓扑排序,期望)

    P4316 绿豆蛙的归宿 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够 ...

  6. Luogu P4316 绿豆蛙的归宿

    P4316 绿豆蛙的归宿 题意翻译 「Poetize3」 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边 ...

  7. 洛谷 P4316 绿豆蛙的归宿

    洛谷 P4316 绿豆蛙的归宿 洛谷传送门 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度, ...

  8. BZOJ 3036: 绿豆蛙的归宿( 期望dp )

    从终点往起点倒推 . 在一个图 考虑点 u , 出度为 s : s = 0 , d[ u ] = 0 ; s ≠ 0 , 则 d( u ) = ( ∑ d( v ) ) / s ( ( u , v ) ...

  9. Bzoj 3036: 绿豆蛙的归宿(期望)

    3036: 绿豆蛙的归宿 Time Limit: 2 Sec Memory Limit: 128 MB Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归 ...

随机推荐

  1. 添加了unimportant 之后,需要调整的参数

    count = count1+count2-count_unimportant_union*10 这个10需要调参.因为我们TOPN取的是10,所以如果两个词完全相同,正常情况下会有22个非0值.( ...

  2. 通过socket实现http通讯代码理解

    1.首先构造http协议报头: String dd = "GET http://www.baidu.com HTTP/1.1" + "\r\n" + " ...

  3. 【LeetCode每天一题】Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  4. 16-Python3 条件控制

    2018-11-20 11:41:15 print('狗狗的年龄兑换*********************************************************') age = ...

  5. koa2的文件上传

    使用koa2搭建文件上传服务,后端代码 const os = require('os'); const path = require('path'); const koaBody = require( ...

  6. ida pro 使用

    交互式反汇编器专业版(Interactive Disassembler Professional),人们常称其为IDA Pro,或简称为IDA.是目前最棒的一个静态反编译软件,为众多0day世界的成员 ...

  7. python regularexpress

    这个正则表达式,我还真没有接触过,在python首次学习 //test.py 1 import re 2 3 print (re.match('www', 'www.myweb.com').span( ...

  8. Dapper基础入门

    Dapper是一个轻量级的ORM.之前最常用的ORM是EF,其实EF底层是Ado.net实现的. 现在基本上已经远离SqlHelper时代了. Dapper是开源的  https://github.c ...

  9. servlet 的servletconfig

  10. c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0。。。。

    c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an ...