P5304: [GXOI/GZOI2019] Traveler — Solution
Main Idea
This problem is pretty godlike. 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.
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.
// 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;
}