#P15291. [MCO 2023] Love Letter

    ID: 15307 远端评测题 2500ms 256MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2023广度优先搜索 BFS最短路MCC/MCO(马来西亚)

[MCO 2023] Love Letter

Description

There are nn dragons numbered from 11 to nn. Dragon ii has age aia_i. The values of a_i are distinct\textbf{The values of a\_i are distinct}. Dragon 11 is Evirir the dragon.

Evirir has a letter it wants to send to dragon tt. To avoid awkwardness caused by age difference, Evirir can send the letter to another dragon (not necessarily dragon tt). The dragon who received the letter can then send the letter to another dragon, and so on. The goal is to eventually send the letter to dragon tt.

For all ii (1in1 \le i \le n), when dragon ii has the letter, it can send the letter to dragon jj in aiaj|a_i - a_j| time1^1. A dragon can "send" the letter to itself in 00 time. However, there are kk pairs of dragons who are close friends. If dragons ii and jj are close friends, then it takes 00 time instead for dragon ii to send a letter to dragon jj (and vice versa).

For each dragon tt (1tn1 \le t \le n), answer the question (independently): What is the minimum total time needed for dragon 11 to send a letter to dragon tt?

1^1Note: x|x| denotes the absolute value of xx. For example, 9=9|9| = 9, 6=6\lvert -6 \rvert = 6, and 0=0|0| = 0.

Input Format

The first line contains two space-separated integers nn and kk (1n21051 \le n\le 2 \cdot 10^5, 0k21050 \le k \le 2 \cdot 10^5) -- the number of dragons and the number of pairs of close friends.

The second line contains nn distinct\textbf{distinct} space-separated integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) -- the dragons' ages.

Then, kk lines follow. Each of the kk lines contains two space-separated integers uu and vv (1u,vn1 \le u, v \le n, uvu \ne v), which means that dragons uu and vv are close friends. It is guaranteed that the same pair of close friends will not appear twice (if (u,v)(u,v) appears, then (u,v)(u,v) and (v,u)(v,u) will not appear afterwards).

Output Format

Output nn space-separated integers d1,d2,,dnd_1, d_2, \ldots, d_n, where did_i is the minimum total time needed for dragon 11 to send the letter to dragon ii.

8 4
50 30 23 10 3 67 69 47
3 7
3 1
2 4
7 1
0 7 0 7 14 2 0 3
3 0
2 3 1
0 1 1

Hint

Note

Explanation for the first sample:

When t=1t = 1, it takes 00 time because dragon 11 already has the letter.

When t=3t = 3, since dragon 11 and 33 are close friends, dragon 11 can send the letter directly to dragon 33 in 00 time.

When t=2t = 2, dragon 11 can send the letter to dragon 33 first in 00 time (they are close friends), then dragon 33 sends the letter to dragon 22 in 2330=7|23 - 30| = 7 time. The total time taken is 0+7=70 + 7 = 7.

When t=8t = 8, dragon 11 can just send the letter directly to dragon 88, taking 5047=3|50 - 47| = 3 time.

Here is one optimal way each for the remaining tt's (ixji \xrightarrow{x} j means dragon ii sends the letter to dragon jj using xx time):

  • Dragon 44: $1 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4$
  • Dragon 55: $1 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4 \xrightarrow{7} 5$
  • Dragon 66: $1 \xrightarrow{0} 3 \xrightarrow{0} 7 \xrightarrow{2} 6$
  • Dragon 77: 1071 \xrightarrow{0} 7

Scoring

Subtask 1 (1818 points): n,k2000n, k \le 2000, ai=ia_i = i

Subtask 2 (1414 points): n,k2000n, k \le 2000

Subtask 3 (99 points): k=0k = 0

Subtask 4 (2929 points): ai=ia_i = i for all ii (1in1 \le i \le n)

Subtask 5 (1616 points): The sequence aa is non-decreasing, i.e. aiai+1a_i \le a_{i+1} for 1in11 \le i \le n-1

Subtask 6 (1414 points): No additional constraints