forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10278.cpp
137 lines (127 loc) · 1.9 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
using namespace std;
const int kMaxn = 507;
const int kInf = 0x7f7f7f7f;
struct Edge {int v, w;};
Edge e[kMaxn * 40];
int r[107], fst[kMaxn], nxt[kMaxn * 40], f, n, cnt, ans, value, d[107][kMaxn], near[kMaxn];
void Clear()
{
cnt = -1;
memset(fst, -1, sizeof(fst));
}
void AddEdge(int u, int v, int w)
{
cnt++;
e[cnt].v = v;
e[cnt].w = w;
nxt[cnt] = fst[u];
fst[u] = cnt;
}
void Input()
{
scanf("%d%d", &f, &n);
for (int i = 1; i <= f; i++)
{
scanf("%d", &r[i]);
}
getchar();
Clear();
string in;
while (getline(cin, in) && !in.empty())
{
int u, v, w;
istringstream(in) >> u >> v >> w;
AddEdge(u, v, w);
AddEdge(v, u, w);
}
}
void SPFA(int start, int *dist)
{
deque<int> q;
bool inq[kMaxn];
memset(inq, false, sizeof(inq));
for (int i = 1; i <= n; i++)
{
dist[i] = kInf;
}
dist[start] = 0;
q.push_back(start);
while (!q.empty())
{
int u = q.front();
q.pop_front();
inq[u] = false;
for (int i = fst[u]; i != -1; i = nxt[i])
{
int v = e[i].v;
if (dist[v] > dist[u] + e[i].w)
{
dist[v] = dist[u] + e[i].w;
if (!inq[v])
{
if (!q.empty() && dist[v] < dist[q.front()])
{
q.push_front(v);
}
else
{
q.push_back(v);
}
inq[v] = true;
}
}
}
}
}
void Solve()
{
for (int i = 1; i <= f; i++)
{
SPFA(r[i], d[i]);
}
memset(near, 0x7f, sizeof(near));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= f; j++)
{
near[i] = min(near[i], d[j][i]);
}
}
value = kInf;
for (int i = 1; i <= n; i++)
{
SPFA(i, d[f + 1]);
int t = 0;
for (int j = 1; j <= n; j++)
{
t = max(t, min(near[j], d[f + 1][j]));
}
if (value > t)
{
ans = i;
value = t;
}
}
}
int main()
{
bool fst_case = true;
int T;
scanf("%d", &T);
while (T--)
{
Input();
Solve();
if (fst_case)
{
fst_case = false;
}
else
{
printf("\n");
}
printf("%d\n", ans);
}
return 0;
}