2346: 道路规划

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:0 解决:0

题目描述

有 lns="http://www.w3.org/1998/Math/MathML"> 个村庄,编号从 lns="http://www.w3.org/1998/Math/MathML">1 到 lns="http://www.w3.org/1998/Math/MathML"> ,请你建立一些道路,使得任意两个村庄可以相连。我们说两个村 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 是相连的,当且仅当有一条公路在 lns="http://www.w3.org/1998/Math/MathML"> 与 lns="http://www.w3.org/1998/Math/MathML">之间,或存在一个村庄 lns="http://www.w3.org/1998/Math/MathML">,有一条的道路连接着 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 之间,一条连接着 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 。

我们知道,已经有一些道路在一些村庄之间,你的工作是再建一些道路,使得所有的村庄连接并且所有的道路建造总和最低。

输入

第一行是一个整数 lns="http://www.w3.org/1998/Math/MathML"> (lns="http://www.w3.org/1998/Math/MathML">3100),表示村庄的数量。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行包含 lns="http://www.w3.org/1998/Math/MathML"> 个整数,在这 lns="http://www.w3.org/1998/Math/MathML"> 行中的第 lns="http://www.w3.org/1998/Math/MathML"> 行的第 lns="http://www.w3.org/1998/Math/MathML"> 个整数 lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML"> 表示村庄 lns="http://www.w3.org/1998/Math/MathML"> 到村庄 lns="http://www.w3.org/1998/Math/MathML"> 建立道路的费用 (lns="http://www.w3.org/1998/Math/MathML">1lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">1000)。

然后有一个整数 lns="http://www.w3.org/1998/Math/MathML"> (lns="http://www.w3.org/1998/Math/MathML">0×(+1)/2)。

接下来再 lns="http://www.w3.org/1998/Math/MathML"> 行,每一行包含两个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> (lns="http://www.w3.org/1998/Math/MathML">1<),即村庄 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 之间已经建成道路。

输出

你应该输出一行包含一个整数,表示所有村庄都连接所要建设道路的最小费用。

样例输入 复制

3
0 990 692
990 0 179
692 179 0
1
1 2

样例输出 复制

179