题意:给一张图以及 k k k个关键点,求关键点两两最短路最小值。
这题难道不是寒假里正睿讲过的原题???
官方正解比较厉害,感觉有点难想。
我们考虑将关键点分为两个集合 A A A和 B B B,建立源点 s s s和汇点 t t t,从 s s s向 A A A中所有点连权值为 0 0 0的边,从 B B B中所有点向 t t t连权值为 0 0 0的边,然后求 s s s到 t t t的最短路。
我们知道答案一定由某一对点产生,关键是你如何恰好让这一对点分属 A A A和 B B B两个集合?
注意到这一对点的编号显然不相同,所以这两个编号一定有一位二进制位是不同的。所以我们可以枚举二进制位,每次将这一位为 0 0 0的点放进 A A A集合,将这一位为 1 1 1的点放进 B B B集合,跑最短路;再把 A A A集合和 B B B集合交换,跑最短路。那么在这个过程中产生答案的那一对点一定至少有一次被放进了两个集合,就统计到了。
我不知道为什么我写的死活TLE,可能对于这种要跑很多次dijkstra又多测的题不适合用zkw线段树优化吧。然后我把一些地方循环展开了一下,就飞起了(比原来快了不止一点点)。。。
#include <cctype>
#include <cstdio>
#include <climits>
#include <algorithm>
template <typename T> inline void read(T& x) {
int f = 0, c = getchar(); x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
if (f) x = -x;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
read(x); read(args...);
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <typename T> inline void writeln(T x) { write(x); puts(""); }
template <typename T> inline bool chkmin(T& x, const T& y) { return y < x ? (x = y, true) : false; }
template <typename T> inline bool chkmax(T& x, const T& y) { return x < y ? (x = y, true) : false; }
typedef long long LL;
const int maxn = 1e5 + 207, maxm = 7e5 + 207;
const LL inf = 1e16;
int v[maxm], w[maxm], next[maxm], head[maxn], tot;
int th[maxn];
int key[maxn], n, m, K;
int mp[maxn << 2], M = 1;
LL dist[maxn];
int T;
inline void ae(int x, int y, int z) {
v[++tot] = y; w[tot] = z; next[tot] = head[x]; head[x] = tot;
}
inline int cmp(int a, int b) { return dist[a] < dist[b] ? a : b; }
inline void upd(int x) { mp[x] = cmp(mp[x << 1], mp[x << 1 | 1]); }
inline void build(int n) { while (M < n + 2) M <<= 1; mp[0] = n + 1; }
inline void modify(int x, LL nv) { for (int i = x + M; dist[mp[i]] > nv; i >>= 1) mp[i] = x; dist[x] = nv; }
inline void del(int x) { for (mp[x += M] = 0, x >>= 1; x; upd(x), x >>= 1); }
inline void clear_tr() {
for (int i = 1; i <= (M << 1); i += 4) {
mp[i] = 0;
mp[i + 1] = 0;
mp[i + 2] = 0;
mp[i + 3] = 0;
}
}
inline void dijkstra(int n, int s) {
for (int i = 0; i <= n; i += 4) {
dist[i] = inf;
dist[i + 1] = inf;
dist[i + 2] = inf;
dist[i + 3] = inf;
}
dist[n + 1] = dist[n + 2] = dist[n + 3] = 0;
clear_tr(); modify(s, 0);
for (int t = 1, x; t < n; ++t) {
del(x = mp[1]);
for (int i = head[x]; i; i = next[i])
if (dist[v[i]] > dist[x] + w[i])
modify(v[i], dist[x] + w[i]);
}
}
inline void clear() {
for (int i = 1; i <= n + 2; i += 4) {
head[i] = 0; th[i] = 0;
head[i + 1] = 0; th[i + 1] = 0;
head[i + 2] = 0; th[i + 2] = 0;
head[i + 3] = 0; th[i + 3] = 0;
}
tot = 0;
M = 1;
}
int main() {
read(T);
while (T--) {
read(n, m, K);
for (int i = 1, x, y, z; i <= m; ++i)
read(x, y, z), ae(x, y, z);
int mark = tot;
for (int i = 1; i <= n; ++i) th[i] = head[i];
for (int i = 1; i <= K; ++i) read(key[i]);
int s = n + 1, t = n + 2;
build(n + 2);
LL ans = inf;
for (int w = 0; (1 << w) <= K; ++w) {
for (int i = 1; i <= K; ++i)
if (i & (1 << w)) ae(key[i], t, 0);
else ae(s, key[i], 0);
dijkstra(n + 2, s);
chkmin(ans, dist[t]);
for (int i = 1; i <= n + 2; i += 4) {
head[i] = th[i];
head[i + 1] = th[i + 1];
head[i + 2] = th[i + 2];
head[i + 3] = th[i + 3];
}
tot = mark;
for (int i = 1; i <= K; ++i)
if (i & (1 << w)) ae(s, key[i], 0);
else ae(key[i], t, 0);
dijkstra(n + 2, s);
chkmin(ans, dist[t]);
for (int i = 1; i <= n + 2; i += 4) {
head[i] = th[i];
head[i + 1] = th[i + 1];
head[i + 2] = th[i + 2];
head[i + 3] = th[i + 3];
}
tot = mark;
}
writeln(ans);
clear();
}
return 0;
}