---
id: 2191
lang: en
translation_of: index.md
source_sha256: 0ca69173cd377c9283f3a5b2d703ae124fc8be57c58d3df5cdbebbed99ffb3c8
title: "BZOJ3470: Freda’s Walk — Solution"
description: "A DAG DP gives the probability of reaching each vertex, a reverse pass gives expected distances, then each edge is tried using the kept arrays."
tags: ["OI","Probability DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "判断的机会 rendered literally as “chance to make a judgment”."
---

## Main Idea

When I first started thinking about it, I thought I could directly set up $dp[u][0/1]$ to indicate whether the one chance to make a judgment had already been used up. The intended solution is actually pretty close to this; it just uses a more formulaic approach.

<!--more-->

We do a DAG DP starting from $1$ to find the probability of reaching each vertex, then go in reverse once to compute the expected distances. When computing these probabilities and expectations, be sure to keep the arrays used as auxiliaries in the computation, so that later, when enumerating each edge, we can compute things quickly.

Enumerate an edge $u \to v, w$, recompute the probability for $u$, and then add the expected value from $1 \to u$. This thing can be obtained directly by subtracting the current vertex’s downward expected distance from the full expectation.

## Code

```cpp
// BZ3470.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e4 + 200, MAX_E = 1e5 + 200;

int n, m, head[MAX_N], current, deg[MAX_N], idx[MAX_N], itot;
double g[MAX_N], acc[MAX_N], f[MAX_N], acc_w[MAX_N];

struct edge
{
    int to, nxt, weight;
} edges[MAX_E];

void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

void toposort()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (deg[i] == 0)
            q.push(i);
    while (!q.empty())
    {
        int u = q.front();
        q.pop(), idx[++itot] = u;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (--deg[edges[i].to] == 0)
                q.push(edges[i].to);
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v, w; i <= m; i++)
        scanf("%d%d%d", &u, &v, &w), u++, v++, addpath(u, v, w), deg[v]++;
    toposort(), g[1] = 1;
    for (int i = 1; i <= n; i++)
    {
        int u = idx[i], sum = 0;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            sum += edges[i].weight;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            g[edges[i].to] += 1.0 * g[u] * edges[i].weight / sum;
    }
    memset(deg, 0, sizeof(deg));
    for (int i = n; i >= 1; i--)
    {
        int u = idx[i];
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            acc[u] += edges[i].weight * f[edges[i].to], acc_w[u] += edges[i].weight, deg[u]++;
        if (acc_w[u] > 0)
            f[u] = acc[u] / acc_w[u] + 1;
    }
    double ans = f[1];
    for (int i = n; i >= 1; i--)
    {
        int u = idx[i];
        if (deg[u] <= 1)
            continue;
        double curt = f[1] - g[u] * f[u];
        for (int i = head[u]; i != -1; i = edges[i].nxt)
        {
            double A = acc[u] - edges[i].weight * f[edges[i].to], B = acc_w[u] - edges[i].weight;
            ans = max(ans, curt + g[u] * (A / B + 1));
        }
    }
    printf("%.6lf\n", ans);
    return 0;
}
```
