-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst.cpp
More file actions
60 lines (59 loc) · 1.06 KB
/
Copy pathmst.cpp
File metadata and controls
60 lines (59 loc) · 1.06 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m;
vector< pair<int, pair<int, int> > >p;
vector<int>v[1001];
ll cost[1001][1001];
ll bos[10001];
ll F_boss(ll a)
{
if(bos[a]==a)
{
return a;
}
else
{
return bos[a]=F_boss(bos[a]);
}
}
void mst()
{
ll i;
for(i=0; i<m; i++)
{
ll c=p[i].first;
ll a=p[i].second.first;
ll b=p[i].second.second;
ll x=F_boss(a);
ll y=F_boss(b);
//cout<<x<<" "<<y<<endl;
if(x!=y)
{
v[a].push_back(b);
v[a].push_back(c);
v[b].push_back(a);
v[b].push_back(c);
bos[x]=y;
cost[a][b]=c;
cost[b][a]=c;
}
}
}
int main()
{
ll a,b,c,i,j;
cin>>n>>m;
for(i=0; i<m; i++)
{
cin>>a>>b>>c;
p.push_back(make_pair(c,make_pair(a,b)));
}
sort(p.begin(),p.end());
for(i=1; i<=n; i++)
{
bos[i]=i;
}
mst();
return 0;
}