#P17150. [ICPC 2017 Xi'an R] Naomi with Graph
[ICPC 2017 Xi'an R] Naomi with Graph
Problem Description
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?
Input Format
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 . .
Output Format
For each test case, print the minimum cost of the graph in a single line.
4 3
1 2
2 3
3 4
0 3 3 3
5