题目链接:
题目:
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(1≤T≤10)
For each case,the first line contains two integers n(2≤n≤105)n (2 \le n \le 10^5)n(2≤n≤105) and m(1≤m≤2×105)m (1 \le m \le 2 \times 10^5)m(1≤m≤2×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)(1≤u,v≤n) denoting a directed edge from uuu to vvv.
It is guarenteed that ∑n≤4×105\sum n \le 4\times 10^5∑n≤4×105, and ∑m≤5×105\sum m \le 5 \times 10^5∑m≤5×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]+11∗day[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]+11∗cost[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;
}