-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10278.cpp
89 lines (84 loc) · 1.68 KB
/
10278.cpp
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
83
84
85
86
#include<iostream>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = 1e8;
int T, F, I;
int dijkstra(vector<vii>& AL, const vector<bool> stations, const int new_station, const int src)
{
vector<int> dist(I, INF);
dist[src] = 0;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pq.push(ii(0, src));
while(pq.size())
{
int d = pq.top().first;
int u = pq.top().second;
pq.pop();
if(stations[u] || u == new_station)
return dist[u];
if(d == dist[u])
for(vii::iterator it = AL[u].begin(); it != AL[u].end(); it++)
{
int w = it->first;
int v = it->second;
if(dist[v] > d+w)
pq.push(ii(dist[v]=d+w, v));
}
}
return INF;
}
int main()
{
bool first = true;
for(cin>>T; T--;)
{
cin >> F >> I;
vector<bool> stations(I, false);
vector<vii> AL(I);
for(int i = 0,s; i < F; i++)
{
cin >> s;
s--;
stations[s] = true;
}
cin.ignore(100, '\n');
while(cin.peek() != '\n' && cin.good())
{
int a,b,w;
cin >> a >> b >> w;
cin.ignore(100, '\n');
a--;
b--;
AL[a].push_back(ii(w,b));
AL[b].push_back(ii(w,a));
}
int new_station=1, dist, min_max = INF;
for(int i = 0; i < I; i++)
{
if(!stations[i])
{
dist = -1;
for(int j = 0; j < I; j++)
if(!stations[j] && i != j)
{
int d = dijkstra(AL, stations, i, j);
dist = max(dist, d);
}
if(dist < min_max)
{
min_max = dist;
new_station = i+1;
}
}
}
if(first) first = false;
else cout << endl;
cout << new_station << endl;
}
return 0;
}