forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 5
/
primsAlgo.cpp
55 lines (47 loc) · 1.14 KB
/
primsAlgo.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
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long int ll;
ll mW(vector<ll>& d, vector<bool>&vis){
ll mw = 1e8, mwv=-1;
for(ll i=0 ; i<d.size() ; i++){
if(!vis[i] && mw>d[i]){
mw = d[i];
mwv = i;
}
}
return mwv;
}
int main(){
ll t;
cin>>t;
while(t--){
ll n, e, u, v, w;
cin>>n>>e;
vector<vector<pair<int, int>>> adj(n);
for(int i=0 ; i<e ; i++){
cin>>u>>v>>w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
vector<ll> d(n, 1e8);
vector<bool> vis(n, 0);
d[0] = 0;
for(ll i=0 ; i<n ; i++){
ll mv = mW(d, vis);
vis[mv]=1;
for(ll j=0 ; j<adj[mv].size() ; j++){
if(!vis[adj[mv][j].F] && d[adj[mv][j].F]>adj[mv][j].S){
d[adj[mv][j].F] = adj[mv][j].S;
}
}
}
ll wt = 0;
for(auto x: d)
wt+=x;
cout<<wt<<"\n";
}
// write your code here
return 0;
}