0
点赞
收藏
分享

微信扫一扫

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

米小格儿 2022-02-07 阅读 42

在这里插入图片描述

容易想到设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;
}
举报

相关推荐

0 条评论