The Preliminary Contest for ICPC Asia Nanjing 2019 南京网络赛 D题 Robots

题目链接:

点击前往


题目:

Given a directed graph with no loops which starts at node 111 and ends at node nnn.

There is a robot who starts at 111, 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 nnn.

It is guaranteed that there is only one node (node 111) whose in-degree is equal to 000, and there is only one node (node nnn) whose out-degree is equal to 000. And there are no multiple edges in the graph.

Input

The first line contains one integer T(1≤T≤10)T (1 \le T \le 10)T(1T10)

For each case,the first line contains two integers n(2≤n≤105)n (2 \le n \le 10^5)n(2n105) and m(1≤m≤2×105)m (1 \le m \le 2 \times 10^5)m(1m2×105), the number of nodes and the number of edges, respectively.

Each of the next mmm lines contains two integers uuu and vvv (1≤u,v≤n)(1 \le u, v \le n)(1u,vn) denoting a directed edge from uuu to vvv.

It is guarenteed that ∑n≤4×105\sum n \le 4\times 10^5n4×105, and ∑m≤5×105\sum m \le 5 \times 10^5m5×105.

Output

Output TTT lines.Each line have a number denoting the expected durability consumption when the robot arrives at node nnn.

Please keep two decimal places.


题目大意:

有一个 n n n个点, m m m条边的有向无环图,保证 1 1 1号节点的入度为 0 0 0 n n n号节点的出度为 0 0 0。有一个机器人要从 1 1 1走到 n n n,每天等概率的走到下一个节点(即任选一个出边)或着在原地不动。机器人每一天消耗的能量等于度过的天数。问机器人从 1 1 1走到 n n n的能量消耗的期望。


解题思路:

这种类型的题目一般都是概率DP。对于这个题,因为概率与后继节点的个数有关,所以我们需要从后向前进行DP
设状态如下
o u t i ] outi] outi] 代表编号为 i i i的点的出度。
d a y [ i ] day[i] day[i] 代表从编号为 i i i的点走到编号为 n n n的点花费的天数的期望。
c o s t [ i ] cost[i] cost[i] 代表从编号为 i i i的点走到编号为 n n n的点消耗的能量的期望。
那么,我们可以得到递推式如下:(设 v v v u u u的后继节点)
d a y [ u ] = 1 o u t [ i ] + 1 ∗ d a y [ u ] + ∑ d a y [ v ] o u t [ i ] + 1 + 1 day[u] = \cfrac{1}{out[i]+1}*day[u] + \sum\cfrac{day[v]}{out[i]+1} +1 day[u]=out[i]+11day[u]+out[i]+1day[v]+1
c o s t [ u ] = 1 o u t [ i ] + 1 ∗ c o s t [ u ] + ∑ c o s t [ v ] o u t [ i ] + 1 + d a y [ i ] cost[u] = \cfrac{1}{out[i]+1}*cost[u] + \sum\cfrac{cost[v]}{out[i]+1} +day[i] cost[u]=out[i]+11cost[u]+out[i]+1cost[v]+day[i]
移项化简后可得:
d a y [ u ] = ∑ d a y [ v ] o u t [ i ] + 1 + 1 o u t [ i ] day[u] = \sum\cfrac{day[v]}{out[i]} +1 + \cfrac{1}{out[i]} day[u]=out[i]day[v]+1+out[i]1
c o s t [ u ] = ∑ c o s t [ v ] o u t [ i ] + ( 1 + 1 o u t [ i ] ) ∗ d a y [ i ] cost[u] = \sum\cfrac{cost[v]}{out[i]} +(1 + \cfrac{1}{out[i]})*day[i] cost[u]=out[i]cost[v]+(1+out[i]1)day[i]
按照上述公式进行两次记忆化搜索即可得到答案(显然为 c o s t [ 1 ] cost[1] cost[1])。
注意处理 o u t [ i ] = 0 out[i] = 0 out[i]=0的情况(也就是当前节点是 n n n的情况)。


代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int MAXN = 1e5 + 100;
const double EPS = 1e-8;
vector<int> edge[MAXN];
int out[MAXN];
double days[MAXN], cost[MAXN];

void getDays(int u) {
    if (fabs(days[u]) > EPS) return;
    if (out[u] == 0) return;
    int vLen = edge[u].size(), v;
    for (int i = 0; i < vLen; i++) {
        v = edge[u][i];
        getDays(v);
        days[u] += days[v];
    }
    days[u] = days[u] / out[u] + 1.0 / out[u] + 1;
}

void getCost(int u) {
    if (fabs(cost[u]) > EPS) return;
    if (out[u] == 0) return;
    int vLen = edge[u].size(), v;
    for (int i = 0; i < vLen; i++) {
        v = edge[u][i];
        getCost(v);
        cost[u] += cost[v];
    }
    cost[u] = cost[u] / out[u] + (1.0 / out[u] + 1) * days[u];
}

int main() {
    int n, m, u, v, T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d", &n, &m);
        memset(out, 0, sizeof(int) * (n + 1));
        memset(days, 0, sizeof(double) * (n + 1));
        memset(cost, 0, sizeof(double) * (n + 1));
        for (int i = 1; i <= n; i++) edge[i].clear();
        for (int i = 0; i < m; i++) {
            scanf("%d %d", &u, &v);
            out[u]++;
            edge[u].push_back(v);
        }
        getDays(1);
        getCost(1);
        printf("%.2f\n", cost[1]);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值