2019 ICPC Nanjing网络赛 D题 Robots 【期望dp】


传送门:Robots


Robots

Given a directed graph with no loops which starts at node 11 and ends at node nn.
There is a robot who starts at 11, and will go to one of adjacent nodes or stand still with equal probability every day.
Every day the robot will have durability consumption which equals to the number of passed days.
Please calculate the expected durability consumption when the robot arrives at node nn.
It is guaranteed that there is only one node (node 1) whose in-degree is equal to 0,
and there is only one node (node n) whose out-degree is equal to 0. And there are no multiple edges in the graph.

Input
The first line contains one integer T (1≤T≤10)
For each case,the first line contains two integers n (2≤n≤10^5) and m (1≤m≤2×10^5), the number of nodes and the number of edges, respectively.
Each of the next mm lines contains two integers u and v (1≤u,v≤n) denoting a directed edge from u to v.
It is guarenteed that ∑n≤4×10^5, and ∑m≤5×10^5.

Output
Output T lines.Each line have a number denoting the expected durability consumption when the robot arrives at node n.
Please keep two decimal places.

样例输入
1
5 6
1 2
2 5
1 5
1 3
3 4
4 5
样例输出
9.78


比赛的时候看出来是期望dp了,但是看了半天看不出来连样例怎么算出 9.78 的都不知道,真的是难受。

赛后看官方题解:
在这里插入图片描述
看完题解又用了半个小时才把花费价值的方程列出来,菜的真实。

d p [ i ] : dp[i]: dp[i]: 表示从第 i 天到 N 的天数期望;
f [ i ] : f[i]: f[i]: 表示从第 i 天到 N 的花费代价的期望;
o u t [ i ] : out[i]: out[i]: 表示点的出度;

天数期望方程:
d p [ i ] = dp[i] = dp[i]= 1 o u t [ i ] + 1 ∗ \frac{1}{out[i]+1}* out[i]+11 d p [ i ] + dp[i] + dp[i]+ 1 o u t [ i ] + 1 ∗ \frac{1}{out[i]+1}* out[i]+11 ∑ d p [ j ] + 1 \sum{dp[j]}+1 dp[j]+1

移项化简:
d p [ i ] = dp[i]= dp[i]= 1 o u t [ i ] ∗ \frac{1}{out[i]}* out[i]1 ∑ d p [ j ] + 1 + \sum{dp[j]}+1+ dp[j]+1+ 1 o u t [ i ] \frac{1}{out[i]} out[i]1


花费代价方程:
f [ i ] = f[i] = f[i]= 1 o u t [ i ] + 1 ∗ \frac{1}{out[i]+1}* out[i]+11 f [ i ] + f[i] + f[i]+ 1 o u t [ i ] + 1 ∗ \frac{1}{out[i]+1}* out[i]+11 ∑ f [ j ] + d p [ i ] \sum{f[j]}+dp[i] f[j]+dp[i]

移项化简:
f [ i ] = f[i]= f[i]= 1 o u t [ i ] ∗ \frac{1}{out[i]}* out[i]1 ∑ f [ j ] + \sum{f[j]}+ f[j]+ o u t [ i ] + 1 o u t [ i ] ∗ d p [ i ] \frac{out[i]+1}{out[i]}*dp[i] out[i]out[i]+1dp[i]


没有用拓扑跑,用的vector存的边,递归求的解,一开始超时了,然后队友让我剪个枝试试,本来以为没有用,没想到真的过了,真玄学


AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int t,n,m;
int out[2*N];
double dp[2*N],f[2*N];
vector<int> v[2*N];
void init()
{
	for(int i=0; i<=n; i++)
	{
		v[i].clear();
		dp[i]=0;
		f[i]=0;
		out[i]=0;
	}
}
double dfsdp(int x)
{
	if(dp[x]!=0) return dp[x];
	if(out[x]==0) return dp[x];
	int len=v[x].size();
	double s1=0;
	for(int i=0; i<len; i++)
	{
		int e=v[x][i];
		dfsdp(e);
		s1+=dp[e];
	}
	dp[x]=s1/out[x]*1.0+1+1.0/out[x];
	return dp[x];
}
double dfsf(int x)
{
	if(f[x]!=0) return f[x];
	if(out[x]==0) return f[x];
	int len=v[x].size();
	double s1=0;
	for(int i=0; i<len; i++)
	{
		int e=v[x][i];
		dfsf(e);
		s1+=f[e];
	}
	f[x]=s1/out[x]*1.0+(1+1.0/out[x]*1.0)*dp[x];
	return f[x];
}
int main()
{
	int a,b;
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=1; i<=m; i++)
		{
			scanf("%d%d",&a,&b);
			out[a]++;
			v[a].push_back(b);
		}

		dfsdp(1);
		/*for(int i=1;i<=n;i++)
		   {
		   printf("%.2f%c",dp[i],i==n?'\n':' ');
		   }*/

		dfsf(1);
		/*for(int i=1;i<=n;i++)
		   {
		   printf("%.2f%c",f[i],i==n?'\n':' ');
		   }*/
		printf("%.2f\n",f[1]);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值