---
id: 1219
lang: en
translation_of: index.md
source_sha256: 820e590b241ff8230ec62d1ba072d87fd220e69e7870424d806e1cc51ad25633
title: "[Codeforces 113D] Museum — Solution"
description: "This problem is pretty brilliant: the formula is fairly easy to derive, but the Gaussian elimination part is a bit of a pain."
tags: ["OI","DP","Gaussian Elimination","Probability"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "人在 i,j 时 (literally “when the person is at i,j”) rendered as “the people at i,j”, since the state tracks two people."
---

## Solution

This problem is still pretty <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span>: the formula is fairly easy to derive, but the Gaussian elimination part is a bit of a pain.

First we enumerate the destination $t$, and let the state $f[i][j]$ be the probability that the people at $i,j$ transition to $t$. Consider the following formula:

<!--more-->

$$
dp[i][j] = dp[i][j] * p_i * p_j + \sum_{(i, u)} dp[u][j] * \frac{1-p_i}{deg_i} * p_j + \sum_{(j, u)} dp[i][u] * \frac{1-p_j}{deg_j} * p_i + \sum_{(i, u)} \sum_{(j, v)} dp[u][v] * \frac{1-p_i}{deg_i} * \frac{1-p_j}{deg_j}
$$

Since there is no way to get a sensible transition order out of this thing, it's best to set up a system of equations and solve it with Gaussian elimination. There are $n^2 - n$ equations in total (the $n$ states $(i,i)$ can be treated as constants). Mind some of the details in the code.

## Code

```cpp
// CF113D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 23, MAX_N2 = MAX_N * MAX_N;
bool mp[MAX_N][MAX_N], fact[MAX_N];
int n, m, deg[MAX_N], id[MAX_N][MAX_N], sa, sb, mx;
double pi[MAX_N], mat[MAX_N2][MAX_N2], f[MAX_N], answers[MAX_N];

void fillNode(int x, int y)
{
    int pos = id[x][y];
    mat[pos][pos] = pi[x] * pi[y] - 1.0;
    for (int i = 1; i <= n; i++)
    {
        if (mp[x][i])
            if (i != y)
                mat[pos][id[i][y]] += f[x] * pi[y];
            else
                mat[pos][mx + 1] -= f[x] * pi[y] * fact[y];
        if (mp[i][y])
            if (i != x)
                mat[pos][id[x][i]] += f[y] * pi[x];
            else
                mat[pos][mx + 1] -= f[y] * pi[x] * fact[x];
        for (int j = 1; j <= n; j++)
        {
            if (mp[i][x] && mp[j][y])
                if (i != j)
                    mat[pos][id[i][j]] += f[x] * f[y];
                else
                    mat[pos][mx + 1] -= f[x] * f[y] * fact[j];
        }
    }
}

double solveCases()
{
    for (int i = 1; i <= mx; i++)
    {
        int pos = i;
        for (int j = i + 1; j <= mx; j++)
            if (fabs(mat[j][i]) > fabs(mat[pos][i]))
                pos = j;
        if (pos != i)
            for (int j = 1; j <= mx + 1; j++)
                swap(mat[i][j], mat[pos][j]);
        for (int j = i + 1; j <= mx; j++)
        {
            double tmp = mat[j][i] / mat[i][i];
            for (int k = i; k <= mx + 1; k++)
                mat[j][k] -= mat[i][k] * tmp;
        }
    }
    for (int i = mx; i >= 1; i--)
    {
        for (int j = mx; j > i; j--)
            mat[i][mx + 1] -= mat[i][j] * mat[j][mx + 1];
        mat[i][mx + 1] /= mat[i][i];
    }
    return mat[id[sa][sb]][mx + 1];
}

double solve(int dst)
{
    memset(mat, 0, sizeof(mat));
    memset(fact, 0, sizeof(fact));
    fact[dst] = 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i != j)
                fillNode(i, j);
    return solveCases();
}

int main()
{
    scanf("%d%d%d%d", &n, &m, &sa, &sb);
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y), mp[x][y] = mp[y][x] = true;
        deg[x]++, deg[y]++;
    }
    if (sa == sb)
    {
        for (int i = 1; i <= n; i++)
            if (i == sa)
                printf("%.10lf ", 1.0);
            else
                printf("%.10lf ", 0.0);
        return 0;
    }
    for (int i = 1; i <= n; i++)
        scanf("%lf", &pi[i]), f[i] = (1.0 - pi[i]) / (1.0 * deg[i]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i != j)
                id[i][j] = ++mx;
    for (int i = 1; i <= n; i++)
        printf("%.10lf ", solve(i));
    return 0;
}
```
