-
Notifications
You must be signed in to change notification settings - Fork 2
/
11377.cpp
69 lines (68 loc) · 1.48 KB
/
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
#include<iostream>
#include<vector>
#include<queue>
#include<functional>
#include<set>
#include<map>
using namespace std;
typedef vector<int> vi;
typedef vector<ii> vii;
const int MAX = 1e7;
int main()
{
int N, M, K, T, Q, a,b,c;
cin >> T;
for(int case_num = 1; case_num <= T; case_num++)
{
cin >> N >> M >> K;
N++;
vector<bool> airports(N, false);
vector<vi> flights(N);
for(int i = 0; i < K; i++)
{
cin >> c;
airports[c] = true;
}
for(int i = 0; i < M; i++)
{
cin >> a >> b;
flights[a].push_back(b);
flights[b].push_back(a);
}
cout << "Case " << case_num << ":" << endl;
cin >> Q;
for(int i = 0; i < Q; i++)
{
cin >> a >> b;
int built = airports[a] ? 0 : 1;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pq.push(ii(built, a));
vi dist(N, MAX);
dist[a] = built;
while(pq.size() && a != b) // Dijkstra's
{
int built = pq.top().first;
int city = pq.top().second;
pq.pop();
if(city == b) break;
if(built == dist[city])
{
for(vi::iterator it = flights[city].begin(); it != flights[city].end(); it++)
{
int next = *it;
int airport = airports[next] ? 0 : 1;
if(built+airport < dist[next])
{
dist[next] = built+airport;
pq.push(ii(dist[next], next));
}
}
}
}
if(a == b) cout << 0 << endl;
else if(dist[b] == MAX) cout << -1 << endl;
else cout << dist[b] << endl;
}
cout << endl;
}
}