-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10423.cpp
54 lines (51 loc) · 1.19 KB
/
10423.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<pair<int, int>> cable[100000];
bool connected[1000];
int pp[1000];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k, a, b, c;
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
connected[i] = false;
}
for (int i = 0; i < k; i++) {
cin >> a;
pp[i] = a - 1;
connected[a - 1] = true;
}
while (m--) {
cin >> a >> b >> c;
cable[a - 1].push_back(make_pair(b - 1, c));
cable[b - 1].push_back(make_pair(a - 1, c));
}
priority_queue<pair<int, int>> lnk;
int answer = 0;
for (int i = 0; i < k; i++) {
for (int j = 0; j < cable[pp[i]].size(); j++) {
lnk.push(make_pair(-1 * cable[pp[i]].at(j).second, cable[pp[i]].at(j).first));
}
}
while (!lnk.empty()) {
int cleng = -1 * lnk.top().first;
int city = lnk.top().second;
lnk.pop();
if (!connected[city]) {
answer += cleng;
connected[city] = true;
for (int i = 0; i < cable[city].size(); i++) {
int nextcity = cable[city].at(i).first;
int nextleng = cable[city].at(i).second;
if (!connected[nextcity]) {
lnk.push(make_pair(-1 * nextleng, nextcity));
}
}
}
}
cout << answer;
}