-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprimsmstsub.cpp
97 lines (95 loc) · 1.67 KB
/
primsmstsub.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//hackerrank_prim-vector;
//mandeep singh @msdeep14
#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;
struct vec
{
int dest;
int src;
int wt;
int track;
};
int check(int visited[],int dest,int n)
{
int flag=0,c=0;
if(dest==visited[dest])
{c=0;
flag=1;
}
if(flag==0)
c=1;
return c;
}
int main()
{
vector<vector <vec> > g;
int a,b,i,j,k,l,x;
int visited[3000];
int sumwt=0;
int n,m,wtt,strtver;
int keeptrack[3000];
cin>>n>>m;
g.resize(m+1);
for(i=0;i<=n;i++)
{
visited[i]=-1;
}
vec temp;
for(i=0;i<m;i++)
{
cin>>a>>b>>wtt;
temp.dest=b;
temp.src=a;
temp.wt=wtt;
temp.track=INT_MAX;
g[a-1].push_back(temp);
g[b-1].push_back(temp);
}
cin>>strtver;
int min=INT_MAX;
int node1;
k=0;
int node;
keeptrack[k]=strtver;
k++;
int size=k;
visited[strtver]=strtver;
while(size!=n)
{
for(j=0;j<size;j++)
{
strtver=keeptrack[j]-1;
for(i=0;i<g[strtver].size();i++)
{
if(min>g[strtver][i].wt)
{
if(check(visited,g[strtver][i].dest,n) )
{
min=g[strtver][i].wt;
node=g[strtver][i].dest;
node1=strtver;
x=i;
}
else if(check(visited,g[strtver][i].src,n))
{
min=g[strtver][i].wt;
node=g[strtver][i].src;
node1=strtver;
x=i;
}
}
}
}
//when node is visited mark it in the visited array;
visited[node]=node;
sumwt=sumwt+g[node1][x].wt;
keeptrack[k]=node;
k++;
min=INT_MAX;
size=k;
}
cout<<sumwt<<endl;
return 0;
}