Valmiki Bhavan has a network of computers connected by LAN cables. It is guaranteed that any two computers in the network can commute with these cables. There can be multiple cables connecting computers. A computer may be connected via a cable to itself. We say that computer can communicate with computer , if there exists a sequence of cables connecting these computers directly or indirectly. Each cable has a latency associated with it. Now, SWD have asked the intelligent Addy to revamp the existing network( with treat assured which Addy loves more than anything) and replace some existing cables with newer cables. He is given new cables, each having its own latency. He can pick any number of cables (maybe ) from these cables and use it to replace any cable in the existing network. Each new cable can be used at most once. It is not necessary to replace every cable from the existing network. Now, considering he uses an arbitrary number of new cables and embeds them into the existing network, he needs to pick cables from this network (can consist of old as well as new ) such that using these cables, it is possible to communicate between any computers present in the network. What can be the minimum sum of latencies of these cables satisfying the above constraints, considering he performs the replacement of the cables optimally?
Input format
The first line of input consists of two integers and . Next lines consists of three integers each: , and denoting a cable between computers and having latency . Note that the computers are labelled from to .Next line consists of an integer denoting the number of cables available for use. Next line consists of an array denoting the latencies of the cables.
Given below is the link to the test file. Your answer is the sum of latencies of all cables in the most optimal network possible.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Use Kruskal to find the minimum spanning tree, push the edges to the MST into a list and then greedily replace the cables from highest to lowest in the MST greedily with new cables of the lowest possible latencies given in the additional list.
This gives the answer as 8 2 8 6 2