You are on page 1of 1

,

Costly Roads
Input file: standard input
Output file: standard output
Time limit: 3 seconds
Memory limit: 256 megabytes

You are given a network of bidirectional roads connecting N different cities numbered 1,2,3.....N. Each
road has a cost of maintenance associated with it. You are given the task of planning the budget for the
district and you need to cut down on road maintenance charges. You decide to break down the costly
roads so as to make sure that every city still remains connected. Come up with an algorithm to maximise
savings while making sure that every city is still reachable from every other city ie: cities should stay
connected.

Input
First line contains T denoting the number of test cases. First line of each test case consists of two space
separated integers N and K denoting the number of cities and number of roads respectively. This is followed
by K lines. Each line contains 3 integers a1 , a2 and a3 . This indicates that there is a road between cities
numbered a1 and a2 which has a cost of maintenance a3 .

Output
You need to output the total savings you made after deleting the costly roads. Since this might be a big
value, you should use the “long” data type instead of “int”.
Constraints:

• 1≤T≤5

• 1 ≤ N ≤ 104

• 1 ≤ K ≤ min(105 , N ∗ (N − 1)/2)

• 1 ≤ cost of each road ≤ 106

Example
standard input standard output
1 56
9 14
1 2 4
1 8 8
2 3 8
2 8 11
3 4 7
3 9 2
3 6 4
4 6 14
4 5 9
5 6 10
6 7 2
7 8 1
7 9 6
8 9 7

Page 1 of 1

You might also like