编程介的小学生 2017-04-20 10:07 采纳率: 20.5%
浏览 1096
已采纳

IP-TV

A consortium of European Internet providers manages a large backbone network, with direct links (connections) between a large number of European cities. A link between a pair of cities is bidirectional. The transmission of a message in a link has an associated cost. As it is common in the Internet, it is possible to use a(unbounded) sequence of direct links to indirectly transfer data between any pair of cities.

For allowing the broadcast of TV programs using this backbone, it is necessary to continuously send data to all nodes in the network. For helping to minimize costs, it is necessary to select the network links that will be used for transmitting data. The set of selected links must be connected and include all nodes in the network.

For helping the consortium to manage its network, you have been asked to create a program that computes the minimum cost for transmitting data to all cities of the backbone.

Given a set of network links, compute the minimum transmission cost for reaching all nodes.

Input

A positive integer P in a single line followed by a sequence of P test cases. The first line of the each test case contains a positive integer M, not greater than 2,000, with the number of cities that have network connections. The second line contains an integer N not greater than 50,000, with the number of existing links. Each of the following N lines contains the representation of a link. Each line contains two strings and one integer, separated by empty spaces, B E C, where B and E are the city names of the endpoints of the network link, with no more than 8 characters, and C is a positive integer, not greater than 30, representing the cost of transmitting in the link.

Output

The output consists of one single line that contains an integer with the minimum transmission cost for sending data to all cities.

Sample Input

1
4
5
lisbon london 6
lisbon paris 5
london paris 1
london berlin 2
paris berlin 10
Sample Output

8

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-05-05 04:17
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波