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.

Input

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)]

Output

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.

Example

Input:
1
2
5
7
1 2 1
2 3 2
2 4 6
5 2 1
5 1 3
4 5 2
3 4 3

Output:
12

hide comments
bholagabbar: 2015-05-24 06:41:08

Yeah. Kruskal works even without Union by rank. However, I still keep getting 81.82 in the problem MST

GAURAV CHANDEL: 2015-03-30 17:17:41

Good problem to learn MST and kruskal..

Bhargav Parsi: 2015-02-12 09:41:46

accepted in 1 go!!..:)..use kruskal algo

Ashish Sareen: 2015-01-24 18:47:49

Silly mistake cost me 2 WA :(.
Kruskal is working fine

Mayank Ladia: 2015-01-18 22:14:33

Green in one go.... easy one.. :)

Francky: 2014-12-29 20:33:53

Time limit was 15s on Pyramid.

(Tjandra Satria Gunawan)(曾毅昆): 2014-12-29 20:18:08

I confirm what Jackson said, here is detail from my experiment:
With path compression O(amortized:m) --> 0.04s
Without path compression
O(amortized:m*log(n)) --> 0.07s
Most of time spend on processing huge I/O (I use scanf/printf for I/O).
Time limit --> 15s on cube cluster.
I suspect there are some misconfiguration.
Is there anyone who remember time limit on this problem before cluster changed to cube?
EDIT: I'll try with python.. Done: 0.89s
@Francky: Thanks for the info, I'll remember this problem as it have "incorrect time limit", time limit should be arround 1.5s.

Last edit: 2014-12-29 20:49:55
Jackson: 2014-12-27 16:15:43

Simple Kruskal without path compression works fine....

Pranav Vaish: 2014-12-12 14:35:53

HUGE DESCRIPTION!!!!!!! Overkill!

fanatique: 2014-12-10 11:52:49

kruskal works fine...

Last edit: 2014-12-10 13:50:14

Added by:Simon
Date:2005-05-24
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