-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (58 loc) · 1.19 KB
/
Copy pathmain.cpp
File metadata and controls
59 lines (58 loc) · 1.19 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
//#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <memory.h>
#include <math.h>
using namespace std;
typedef struct
{
int from;
int to;
int weight;
}Edge;
int n, e,cnt = 0;//n:vertexes e:edge cnt:the minimum
vector <Edge> edge;
bool cmp(Edge a, Edge b)
{
return a.weight < b.weight;
}
int Find(int *parent, int f)
{
while(parent[f] > 0)
f = parent[f];
return f;
}
void MST()
{
int n, m, i;
int parent[1001];
memset(parent, 0, sizeof(parent));
for(i = 0; i < e; i++)
{
n = Find(parent, edge[i].from);
m = Find(parent, edge[i].to);
if(n != m)
{
parent[n] = m;
printf("(%d, %d) %d\n", edge[i].from, edge[i].to, edge[i].weight);
cnt += edge[i].weight;
}
}
cout << "The minimum: " << cnt << endl;
}
int main()
{
ifstream cin("input.txt");
cin >> n >> e;
for(int i = 0; i < e; i++)
{
Edge eg;
cin >> eg.from >> eg.to >> eg.weight;
edge.push_back(eg);
}
sort(edge.begin(), edge.end(), cmp);
MST();
return 0;
}