-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-11377.cpp
82 lines (70 loc) · 1.71 KB
/
uva-11377.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
//uva 11377
//Airport Setup
#include <iostream>
#include <climits>
#include <vector>
#include <set>
#include <map>
using namespace std;
int Dijkstra(vector <vector <int> > & Graph, vector <bool> & has_airport, int source, int destin);
int main(void)
{
int T = 0;
cin >> T;
for(int tcounter = 1; tcounter <= T; ++tcounter){
int n, m, k;
cin >> n >> m >> k;
vector <bool> has_airport(n, false);
for(int i = 0; i < k; ++i){
int tmp;
cin >> tmp;
--tmp;
has_airport[tmp] = true;
}
vector <vector <int> > Graph(n);
for(int i = 0; i < m; ++i){
int u, v;
cin >> u >> v;
--u, --v;
Graph[u].push_back(v);
Graph[v].push_back(u);
}
cout << "Case " << tcounter << ":" << endl;
int Query = 0;
cin >> Query;
while(Query--){
int source, destin;
cin >> source >> destin;
--source, --destin;
int result = Dijkstra(Graph, has_airport, source, destin);
cout << result << endl;
}
cout << endl;
}
return 0;
}
int Dijkstra(vector <vector <int> > & Graph, vector <bool> & has_airport, int source, int destin)
{
if(source == destin)
return 0;
int n = Graph.size();
vector <int> min_airport(n, INT_MAX);
set <pair <int, int> > Heap;
min_airport[source] = !has_airport[source];
Heap.insert(make_pair(min_airport[source], source));
while(!Heap.empty()){
int front = Heap.begin()->second;
Heap.erase(Heap.begin());
for(auto a : Graph[front]){
int count_airport = min_airport[front];
if(!has_airport[a])
++count_airport;
if(count_airport < min_airport[a]){
Heap.erase(make_pair(min_airport[a], a));
min_airport[a] = count_airport;
Heap.insert(make_pair(min_airport[a], a));
}
}
}
return min_airport[destin] == INT_MAX ? -1 : min_airport[destin];
}