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
ranjanakash166: 2016-05-20 15:58:01

SIMPLE MST kruskal algo and disjoint set ROCKS!!!

dwij28: 2016-05-09 01:06:07

Number of edges is high. Prim is a better option than Kruskal here.

Akshay Damle: 2016-04-20 11:09:29

@bholagabbar Use long long int in the MST problem to get 100

Carson Kreppein: 2016-03-15 17:22:45

Easy Problem AC on first go. My 26th ;)

Kyle Dencker: 2016-03-15 12:19:06

I see a lot of Kruskal, however, the set is not disjointed. So you could use Prims if you like. That is what I did. :)

accepted with first attempt. (Don't mind the CE due to not naming my class Main)

anshumakkar: 2016-03-10 05:48:13

good practice problem in Kruskal and Sorting to be done in QSort otherwise TimeLimit exceeds (i.e NLogN, any alogo )

gaurav117: 2016-01-26 11:34:23

good one to practice algorithms :D but spoilers do reduce the beauty of the problem.

kartikay singh: 2015-12-25 19:24:04

Piece of cake
kruskal's mst + union-find -> AC 0.10s
[EDIT]: union by rank and path-compression 0.09s
Can be optimised with fast I/O ?.

Last edit: 2015-12-26 07:58:33
anando_du: 2015-10-16 22:50:53

Normal MST .
BTW don't know why time limit is 15s !! it should be 0.5 sec

Last edit: 2015-10-16 22:52:24
sujit yadav: 2015-06-22 05:19:31

After learning kruskal algo and disjoint set ... its too easy :) AC


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