-
Notifications
You must be signed in to change notification settings - Fork 0
/
1135.cpp
70 lines (58 loc) · 1.27 KB
/
1135.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
class Solution {
public:
struct edge
{
int a;
int b;
int l;
edge(int a, int b, int l):a(a), b(b), l(l){};
};
bool un(int i, int j)
{
while(u[i] != i)
{
u[i] = u[u[i]];
i = u[i];
}
while(u[j] != j)
{
u[j] = u[u[j]];
j = u[j];
}
//此次没有添加新的连结
if(i == j)return false;
u[i] = j;
return true;
}
struct cmp{
bool operator()(edge x, edge y){return x.l > y.l;}
};
int minimumCost(int N, vector<vector<int>>& connections) {
for(int i = 0; i <= 10000; ++i)
{
u[i] = i;
}
for(int i = 0; i < connections.size(); ++i)
{
q.push(edge(connections[i][0], connections[i][1], connections[i][2]));
}
int sum = 0;
int cnt = 0;
while(!q.empty())
{
edge e = q.top();
q.pop();
if(un(e.a, e.b))
{
sum += e.l;
++cnt;
}
}
if(cnt == N-1)
return sum;
else
return -1;
}
int u[10001];
priority_queue<edge, vector<edge>, cmp> q;
};