-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
union.cpp
162 lines (149 loc) · 2.53 KB
/
union.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct edge
{
int src,dest,sum,weight;
};
struct graph
{
int v,e;
struct edge* edg;
};
struct subset
{
int par;
int rnk;
};
struct graph* create(int v,int e)
{
struct graph* grap=(struct graph*)malloc(sizeof(struct graph));
grap->v=v;
grap->e=e;
grap->edg=(struct edge*)malloc(sizeof(struct edge)*e);
};
void print(struct graph* graph)
{
int i;
for(i=0;i<graph->e;i++)
{
cout<<graph->edg[i].src<<" "<<graph->edg[i].dest<<graph->edg[i].sum<<endl;
}
}
bool compare(struct edge e1,struct edge e2)
{
if(e1.weight==e2.weight)
{
if(e1.sum!=e2.sum)
{
return e1.sum<e2.sum;
}
else
{
return e1.sum;
}
}
return e1.weight<e2.weight;
}
void swap(struct edge *a,struct edge *b )
{
struct edge temp=*a;
*a=*b;
*b=temp;
}
int fnd(struct subset s[],int x)
{
if(s[x].par!=x)
{
s[x].par=fnd(s,s[x].par);
}
return s[x].par;
}
void uni(struct subset s[],int x,int y)
{
int xt=fnd(s,x);
int yt=fnd(s,y);
if(s[xt].rnk<s[yt].rnk)
{
s[xt].par=yt;
}
else if(s[xt].rnk>s[yt].rnk)
{
s[yt].par=xt;
}
else
{
s[yt].par=xt;
s[xt].rnk++;
}
}
void kruskal(struct graph *g)
{
int i=0;
struct edge res[g->v];
for(int d=0;d<g->v;d++)
{
res[d].src=0;
res[d].dest=0;
}
struct subset *sub=(struct subset*)malloc(sizeof(struct subset)*(g->v+1));
for(int i=1;i<g->v+1;i++)
{
sub[i].par=i;
sub[i].rnk=0;
}
int e=0;
while(e<g->v-1)
{
struct edge ed=g->edg[i++];
int x=fnd(sub,ed.src);
int y=fnd(sub,ed.dest);
if(x!=y)
{
res[e++]=ed;
uni(sub,x,y);
}
}
int sum=0;
for(int e=0;e<g->v;e++)
{
if(res[e].src!=0&&res[e].dest!=0)
{
sum=sum+res[e].weight;
}
}
cout<<sum;
}
int main()
{
int v,e;
cin>>v>>e;
struct graph* g=create(v,e);
for(int i=0;i<e;i++)
{
int x,y,z;
cin>>x>>y>>z;
g->edg[i].src=x;
g->edg[i].dest=y;
g->edg[i].sum=x+y+z;
g->edg[i].weight=z;
}
for(int i=0;i<e;i++)
{
for(int j=0;j<e-i-1;j++)
{
if(g->edg[j].weight>g->edg[j+1].weight)
{
swap(g->edg[j],g->edg[j+1]);
}
if(g->edg[j].weight==g->edg[j+1].weight)
{
if(g->edg[j].sum>g->edg[j].sum)
{
swap(g->edg[j],g->edg[j+1]);
}
}
}
}
kruskal(g);
}