【YBTOJ】路径长度 &&【Luogu】绿豆蛙的归宿

96 篇文章 0 订阅
11 篇文章 0 订阅

在这里插入图片描述

思路:

容易想到设i到n的期望长度为 d i d_i di,考虑一条j向i的边w,则 d i d_i di d j d_j dj的贡献为 d i + w k i \frac{d_i+w}{k_i} kidi+w
有了转移方程,只需要拓扑DP就行了(

c o d e code code

#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

int n, m, du[100100], du1[100100];
int tot, head[100100];
struct node
{
	int to, next, w;
}b[200100];
double EXD[100100];

void add(int x, int y, int w)
{
	b[++tot]=(node){y, head[x], w};
	head[x]=tot; 
}

void topu()
{
	queue<int> q;
	q.push(n);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		for(int i=head[x]; i; i=b[i].next)
		{
			int y=b[i].to;
			du[y]--;
			if(!du[y])
				q.push(y);
			EXD[y]+=(EXD[x]*1.0+b[i].w*1.0)*1.0/(du1[y]*1.0);
		}
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i=1; i<=m; i++)
	{
		int x, y, w;
		scanf("%d%d%d", &x, &y, &w);
		du[x]++, du1[x]++;
		add(y, x, w);
	}
	topu();
	printf("%.2lf", EXD[1]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值