CSTREET - Cobbled streets

The municipal chronicals of an unbelievable lordly major town in a land far, far away tell the following story:
Once upon a time the new crowned king Günther decided to visit all towns in his kingdom. The people of the unbelievable lordly major town expected that king Günther would like to see some of the most famous buildings in their town. For the lordly citizens it seemed neccessary that all streets in the town that the king would have to use had to be cobbled with stone. Unfortunately the unbelievable lordly major town had not much money at that time as they used most of their savings to erect the highest cathedral the world had ever seen.
Roumours were afloat that the real reason for their thriftiness was not that the town treasury was empty but that many people believed that king Günther came to the throne by deceiving his father king Erwin and that in his youth he made a pact with the devil. But anyway, the citizens of the unbelievable lordly major town decided to pave only as much streets as were absolutely necessary to reach every major building.
Can you help the citizens of the unbelievable lordly major town to find out which streets should be paved?
It might be usefull to know that all major buildings are either at the end of a street or at an intersection. In addition to that you can assume that all buildings are connected by the given streets.


t [number of testcases (1 <= t <= 100)]
p [price to pave one furlong of street (positive integer)]
n [number of main buildings in the town (1 <= n <= 1000)]
m [number of streets in the town (1 <= m <= 300000)]
a b c [street from building a to building b with length c (lengths are given in furlong and the buildings are numbered from 1 to n)]


For each testcase output the price of the cheapest possibility to reach all main buildings in the city on paved streets. You can assume that the result will be smaller than 2^32.


1 2 1
2 3 2
2 4 6
5 2 1
5 1 3
4 5 2
3 4 3


hide comments
anmoldua: 2017-09-14 18:52:47

yo hoo!!!!!!!!!!!!!!!!!!

sandeep_4141: 2017-07-24 15:01:44

easy !! again kruskal !!

ravi_ndra12345: 2017-05-14 09:38:12

Kruskal rocks , AC in one Go :))))))

rohit9934: 2017-05-06 18:39:47

Disjoint Union(pAth Compression) .0.18 secs. AC in one Go.
Learnt Kruskal,Disjoint union with this problem.SPOJ is Best tutor.Ever.

nilabja16180: 2017-03-08 09:35:17

Disjoint set with kruskal, AC in One GO!

davidgalehouse: 2017-02-26 23:58:36

You can assume no multiple edges, street lengths are ints, street lengths fit in int32, street costs fit in int32.

weathervane: 2016-12-28 20:20:39

I like Dijkstra, modified for this problem. I see that accepted answers are a fraction of a second, never mind 15 seconds!

flyingduchman_: 2016-12-28 10:19:55

O(V^2) prim's algorithm for adjacency matrix got ac !!!

and_roid: 2016-12-24 11:50:05

Yeah. That's right @kshubham02 !!
And add to that, in central perk, he went bald-headed(actually blonde) and fell in love with rachel :p

vanvinhbk94: 2016-11-28 09:28:50

Last edit: 2016-11-28 09:29:23

Added by:Simon
Time limit:15s
Source limit:32211B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL GOSU JS-RHINO PERL6
Resource:Ulm Algorithm Course SoSe 2005