-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairportSetup_UVA11377.cpp
98 lines (77 loc) · 2.14 KB
/
airportSetup_UVA11377.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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("airportSetup_UVA11377.in");
ofstream fout("airportSetup_UVA11377.out");
const int inf = INT_MAX / 2;
struct segment
{
int u;
int b;
bool operator < (const segment & temp) const
{
return b > temp.b;
}
};
segment _segment(int u, int b)
{
segment temp{u, b}; return temp;
}
int main()
{
int tcc; fin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int n, m, k; fin >> n >> m >> k;
vector<int> haveAirport(n + 1, 0);
for (int c = 1; c <= k; ++c)
{
int u; fin >> u;
haveAirport[u] = 1;
}
vector<vector<int>> g(n + 1);
for (int c = 1; c <= m; ++c)
{
int u, v; fin >> u >> v;
g[u].push_back(v); g[v].push_back(u);
}
fout << "Case " << t << ":\n";
int q; fin >> q;
for (int c = 1; c <= q; ++c)
{
int s, e; fin >> s >> e;
if (s == e)
{
fout << "0\n";
continue;
}
vector<int> ans(n + 1, inf);
priority_queue<segment> pq;
if (haveAirport[s] == 1) pq.push(_segment(s, 0));
else pq.push(_segment(s, 1));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();
if (now.b >= ans[now.u]) continue;
ans[now.u] = now.b;
if (now.u == e) break;
int size = g[now.u].size();
for (int i = 0; i <= size - 1; ++i)
{
int v = g[now.u][i];
if (haveAirport[v] == 1) pq.push(_segment(v, now.b));
else pq.push(_segment(v, now.b + 1));
}
}
if (ans[e] == inf)
{
fout << "-1\n";
}
else
{
fout << ans[e] << '\n';
}
}
fout << '\n';
}
return 0;
}