haleyk100198's blog

By haleyk100198, history, 6 years ago, In English

I am keenly interested knowing the solution for a problem that our team didn't found a solution even when we are reviewing the questions, would like to hear from others thoughts on this question. :)


Problem E — Naomi with the graph

Time limit: 1~3s, I forgot the exact time limit. Memory Limit: 512 MB

As we know, Naomi is poor at math. But she practices math problems every day. The following problem is one which she met.

Naomi has a non-directed connected graph with n nodes, labeled from 1 to n, and m edges. The length of each edge is exactly 1. Each node in the graph has a special value and the value of the i-th node is denoted by A[i]. She defines dist[i] as the length of the shortest path bewteen the i-th node and the first node and especially dist[1] = 0.

Then she defines the cost of the graph as Sum( (A[i] — dist[i])^2 | for all 1 <= i <= n ).

Naomi wants to MINIMIZE the cost of the graphby inserting some new edges (the lengths of which should be 1 as well) into the graph. She could insert nothing or insert any number of new edges in her mind.

Can you help her?

Input

The input contains several test cases which is no more than 10.

In each test case, the first line contains two integers n and m ( 1 <= n <= 40, 0 <= m <= 1600).

Each of the following m lines contains two integers x and y ( 1 <= x, y <= n ) indicating an edge between the x-th node and the y-th node.

The last line of the test case contains n integers describing the array A and each element A[i] satisfies 0 <= A{i] <= 1000.

Output

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

Sample input

4 3

1 2

2 3

3 4

0 3 3 3

Sample Output

5


So far the best algorithm our team have came up with is a min-cut algorithm with nodes (a[i], dist[i]), but we are worried that having O(V^2) nodes will result in O(V^6) runtime per test case thus a brutal TLE.

I am grateful if someone could shed some light on this problem, I wonder if we are on a completely wrong path — It seems to be impossible to solve the problem with flow related algorithms without constructing O(V^2) nodes.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it +5 Vote: I do not like it

This problem is the same as TopCoder SRM 590 FoxAndCity.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Woah, thanks for the input, seems like my team had the correct idea but I estimated runtime incorrectly.

    No wonder why did some of the teams managed to solve this problem in the first hour ...