-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11631.cpp
77 lines (70 loc) · 1.17 KB
/
11631.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
/*
11631 - Dark roads
*/
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,pair<int,int>>>p;
int parent[200001];
void initialize(int n)
{
for(int i=0; i<n; i++)
{
parent[i]=i;
}
}
int findd(int i)
{
return (parent[i]==i)?i:findd(parent[i]);
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n,m;
while(scanf("%d %d",&n,&m)==2)
{
if(n==0 && m==0)break;
initialize(n);
p.clear();
int total=0;
for(int i=0; i<m; i++)
{
int x,y,c;
scanf("%d %d %d",&x,&y,&c);
p.push_back(make_pair(c,make_pair(x,y)));
total+=c;
}
sort(p.begin(),p.end());
int sum=0;
for(int i=0; i<m; i++)
{
int px = findd(p[i].second.first);
int py = findd(p[i].second.second);
if(px!=py)
{
sum+=p[i].first;
parent[py]=px;
}
}
cout<<total-sum<<endl;
}
return 0;
}
/*
Sample Input
7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0
Sample Output
51
*/