logo AlgoBeat OnlineJudge
登录 注册

#217101. [ICPC 2017 Xi'an R] Naomi with Graph

内存限制:512 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

As we know, Naomi is poor at math. But Naomi practices math problems every day. The following is one of them.

Naomi has a non-directed connected graph with vertices labelled from to and edges. The length of each edge is . Naomi needs to add some edges (lengths of which should be as well) to the graph such that each of them connects two different vertices and minimize the cost of the graph.

We define as the length of the shortest path between vertex and vertex . Vertex has a value . The cost of the graph equals to .

Can you help her?

输入格式

The input contains multiple test cases. (No more than )

In each test case:

The first line contains two numbers , . ()

The following lines each contain two numbers , () denoting that there is an edge between and .

And the last line of each test case contains numbers denoting the array . .

输出格式

For each test case, print the minimum cost of the graph in a single line.

样例

样例输入 1

4 3
1 2
2 3
3 4
0 3 3 3

样例输出 1

5