#1329. 坏奶牛 Bad Cowtractors

坏奶牛 Bad Cowtractors

题目描述

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".

Bessie受雇在农场主John's的N个谷仓(2<=N<=1000)\red{(2 <= N <= 1000)}之间建立一个廉价的互联网网络。FJ已经做了一些调查,发现了M(1<=M<=20,000)\red{M (1 <= M <= 20,000)}对谷仓之间可能的连接路径。每个可能的连接路由都有一个相关的成本C(1<=C<=100,000)\red{C (1 <= C <= 100,000)}。农民John's想要在连接网络上花费最少的钱;他甚至都不想付钱给Bessie

意识到农场主John's不会给她钱,Bessie决定做最坏的工作。她必须决定一组连接安装,所以(i)这些连接的总成本是尽可能大,(ii)所有的谷仓都连接在一起(这样就可以达到任何谷仓从其他仓库通过安装连接的路径),和(iii),这样没有周期之间的连接(农民John's很容易能够检测)。

条件(ii)和(iii)确保最后的连接集看起来像一个“树”。

输入格式

  • Line 1: Two space-separated integers: N and M

  • Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.

第1行:两个空格分隔的整数:NM\red{N和M}

第2 . .M+1行:每行包含三个以空格分隔的整数AB\red{A、B}C\red{C},它们描述了成本为C\red{C}的仓库A\red{A}和仓库B\red{B}之间的连接路由。

输出格式

  • Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

第1行:单个整数,包含连接所有仓库的最昂贵树的价格。如果不可能连接所有的谷仓,输出1\red{-1}

样例

输入样例

5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17

输出样例

42

提示

输出样例解释:

The most expensive tree has cost 17+8+10+7=42\red{17 + 8 + 10 + 7 = 42}. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.