Travelling Tom

4.7

32 votes
Algorithms, Bit Manipulation, Graphs, Medium, Minimum spanning tree
Problem

Tom is visiting the country Hackerland. Hackerland has n cities and m bi-directional roads. There are k types of tokens. Token i costs ci. The costs of the tokens are such that for all 2ik, ci2ci1. For each road, you need to have a particular set of tokens, if you want to travel it. Note that you don't have to give the tokens, you just need to show them. Thus, one token can be used at any number of roads, where it is required. Tom wants to select a set of tokens, such that using them, he can go from any city to any other city. You have to help him minimize the total cost of tokens he buys.

Input:

  • The first line contains three space separated integers, n m and k .
  • The second line contains k space separated integers, where the ith integer denotes the price of ith token, i.e. ci .
  • ith of the next m lines contains three integers ui,vi,li, where li is the number of tokens required by the ith road, followed by li indices denoting the tokens required. This road connects cities ui and vi

Output:

  • Print one integer containing the minimum cost of tokens Tom has to buy, such that he can travel from any city to any other city. If it is impossible to choose such a set of tokens, print 1.

Constraints

  • 1n105

  • 1m105

  • No road connects a city to the same city. However, there can be multiple roads between two cities.

  • 1ci1018

  • For all 2ik, ci2ci1

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The best way for tom is to buy the first three tokens. Using these tokens, he can use the roads 1 and 2, and using the roads he can go from any city to any other city. The minimum cost is therefore 8

Editor Image

?