forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMST.cpp
More file actions
62 lines (61 loc) · 1.09 KB
/
MST.cpp
File metadata and controls
62 lines (61 loc) · 1.09 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define eb emplace
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define loop(i,a,n) for(int i=a;i<n;i++)
#define trav(a,x) for(auto &a:x)
#define pii pair<ll,int>
const int w=1e6+1;
vector<pii>g[w];
vector<int>v(w,0);
ll prim()
{
ll ans=0;
priority_queue<pii,vector<pii>,greater<pii>>q;
q.push({0,1});
pii p;
while(!q.empty())
{
p=q.top();
q.pop();
int j=p.ss;
if(v[j]==1)
continue;
ans+=p.ff;
v[p.ss]=1;
for(auto c:g[j])
{
if(!v[c.ss])
{
q.push(c);
}
}
}
return ans;
}
// map<string,int>h;
int main() {
// your code goes here
io;
int t;
cin>>t;
while(t--)
{ int p,n,m;
cin>>p>>n>>m;
loop(i,0,m)
{
int a,b,c;
cin>>a>>b>>c;
g[a].pb({c,b});
g[b].pb({c,a});
}
ll ans=prim();
}
return 0;
}