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

    ID: 19428 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>2017网络流最小割ICPC西安

[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 nn vertices labelled from 11 to nn and mm edges. The length of each edge is 11. Naomi needs to add some edges (lengths of which should be 11 as well) to the graph such that each of them connects two different vertices and minimize the cost of the graph.

We define dist[i]\text{dist}[i] as the length of the shortest path between vertex 11 and vertex ii. Vertex ii has a value A[i]A[i]. The cost of the graph equals to i=1n(A[i]dist[i])2\sum_{i=1}^n (A[i] - \text{dist}[i])^2.

Can you help her?

Input Format

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

In each test case:

The first line contains two numbers nn, mm. (1n40, 0m16001 \le n \le 40,\ 0 \le m \le 1600)

The following mm lines each contain two numbers xx, yy (1x,yn1 \le x, y \le n) denoting that there is an edge between xx and yy.

And the last line of each test case contains nn numbers denoting the array AA. 0A[i]10000 \le A[i] \le 1000.

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