---
id: 2327
lang: en
translation_of: index.md
source_sha256: f399482a6b07da38db0cbda42442874aa940df051842ac3e8c0811a19d80ad04
title: "P5304: [GXOI/GZOI2019] Traveler — Solution"
description: "Split the marked vertices into two sets by each bit of their index, attach a super source and sink, and run shortest paths a logarithmic number of times."
tags: ["OI","Shortest Path","Binary"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "跑一边最短路 read as the typo 跑一遍最短路 (run shortest path once)."
  - "编号第 i 位是否为 01 (whether bit i of the index is 01, i.e. 0 or 1): the formula 01 is kept unchanged."
---

## Main Idea

This problem is 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>. I really like it.

If we just do brute force, this line of thinking only goes so far. Let's think about it a different way. Since the final answer requires shortest paths over $\Theta(k^2)$ pairs, we try to shrink this scale, solve only the global answer for a small scale (not computing all the shortest paths), and then after solving it several times we can get the global answer for the full scale.

<!--more-->

Here is an idea for solving the small-scale problem: suppose we split the vertices into sets $A, B$, then connect the source $S$ to set $A$ with zero-weight edges, and connect set $B$ to the sink $T$ with zero-weight edges. Running shortest path once like this gives the minimum over all $(x, y), x \in A, y \in B$, so the scale is $|A| \times |B|$.

We try to make the number of distinct $(x, y), x \in A, y \in B$ as large as possible each time we solve a small-scale problem, so that we can cover the global scale in as few rounds as possible. The method used here is to do $\log_2 n$ rounds, and in each round the vertex sets $A, B$ are split based on whether the $i$-th bit of the index is $01$.

This guarantees that any two vertices end up in different vertex sets in at least one split, because no two vertices have the same index.

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

using namespace std;

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

typedef long long ll;
typedef pair<ll, int> pli;

int T, n, m, kind, head[MAX_N], current, ui[MAX_E], vi[MAX_E], wi[MAX_E], start_pos, end_pos, pts[MAX_N];
bool vis[MAX_N];
ll dist[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 shortest_path()
{
    memset(dist, 0x3f, sizeof(dist));
    memset(vis, false, sizeof(vis));
    priority_queue<pli> pq;
    pq.push(make_pair(0, start_pos)), dist[start_pos] = 0;
    while (!pq.empty())
    {
        int u = pq.top().second;
        pq.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] > dist[u] + edges[i].weight)
                dist[edges[i].to] = dist[u] + edges[i].weight, pq.push(make_pair(-dist[edges[i].to], edges[i].to));
    }
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d", &n, &m, &kind);
        for (int i = 1; i <= m; i++)
            scanf("%d%d%d", &ui[i], &vi[i], &wi[i]);
        for (int i = 1; i <= kind; i++)
            scanf("%d", &pts[i]);
        ll ans = 2e18;
        for (int i = 0; (1 << i) <= kind; i++)
        {
            memset(head, -1, sizeof(head)), current = 0;
            for (int j = 1; j <= m; j++)
                addpath(ui[j], vi[j], wi[j]);
            start_pos = n + 1, end_pos = n + 2;
            for (int j = 1; j <= kind; j++)
                if (j & (1 << i))
                    addpath(start_pos, pts[j], 0);
                else
                    addpath(pts[j], end_pos, 0);
            shortest_path(), ans = min(ans, dist[end_pos]);
        }
        for (int i = 0; (1 << i) <= kind; i++)
        {
            memset(head, -1, sizeof(head)), current = 0;
            for (int j = 1; j <= m; j++)
                addpath(ui[j], vi[j], wi[j]);
            start_pos = n + 1, end_pos = n + 2;
            for (int j = 1; j <= kind; j++)
                if (!(j & (1 << i)))
                    addpath(start_pos, pts[j], 0);
                else
                    addpath(pts[j], end_pos, 0);
            shortest_path(), ans = min(ans, dist[end_pos]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}
```
