-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_1197.cpp
More file actions
48 lines (45 loc) · 918 Bytes
/
back_1197.cpp
File metadata and controls
48 lines (45 loc) · 918 Bytes
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<pair<int, int> >v[10001];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int, int> > >pq;
bool bv[10001];
int sum;
void prim(int a)
{
bv[a] = true;
for (int i =0; i < v[a].size(); i++)
{
if(!bv[v[a][i].second])
{
pq.push(make_pair(v[a][i].first,v[a][i].second));
}
}
while (!pq.empty())
{
pair<int,int> pp = pq.top();
pq.pop();
if (!bv[pp.second])
{
sum+=pp.first;
prim(pp.second);
return ;
}
}
}
int main()
{
int a, b;
cin >> a >> b;
for (int i = 1; i <= b; i++)
{
int a1, a2, a3;
cin >> a1 >> a2 >> a3;
v[a1].push_back(make_pair(a3,a2));
v[a2].push_back(make_pair(a3,a1));
}
prim(1);
cout << sum;
return 0;
}