本题就是从一处storage找到最近的一处非storage。

 

先画图,再判断是storage的点少,还是非storage的点少(否则直接找会超时),接着从少的那一类点中,挨个查找每个点与另一类点的最近距离,最后答案为所有最近距离的最小值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define pii pair<int,int>
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
vector<int>storage;
struct cmp {
bool operator() (const pii p1, const pii p2) {
return p1.second > p2.second;
}
};
struct Node
{
vector<pii>to;
}nd[maxn];
bool is_store[maxn];
bool vised[maxn];

int one_len(int s){
bool is = is_store[s];
priority_queue<pii,vector<pii>,cmp>ch;
for (auto c : nd[s].to) {
ch.push(c);
}
while (!ch.empty()) {
pii tmp = ch.top();
if (!(is^is_store[tmp.first])) { //两点属于同一类,继续找
ch.pop();
for (auto t : nd[tmp.first].to) {
if(!vised[t.first])
ch.push(t);
vised[t.first] = 1;
}
}
else { //不属于同一类,停止
return tmp.second;
}
}
return INF;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
if (k == 0 || n == 1||k==n) {
cout << -1;
return 0;
}
for (int i = 0; i < m; i++) {
int u, v, l;
cin >> u >> v >> l;
nd[u].to.push_back(pii(v,l));
nd[v].to.push_back(pii(u,l));
}
for (int i = 0; i < k; i++) {
int a;
cin >> a;
is_store[a] = 1;
storage.push_back(a);
}
int ans=INF;
if (k <= n / 2) {
for (auto c : storage) {
ans = min(ans, one_len(c));
}
}
else {
for (int i = 1; i <= n; i++) {
if (!is_store[i]) {
ans = min(ans, one_len(i));
}
}
}
if (ans == INF) {
cout << -1;
}
else {
cout << ans;
}
return 0;
}