#7612. [MCO 2023] Love Letter

    ID: 7612 传统题 2500ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>P152912023广度优先搜索 BFS最短路MCC/MCO(马来西亚)

[MCO 2023] Love Letter

[MCO 2023] Love Letter

题目描述

There are $n$ dragons numbered from $1$ to $n$. Dragon $i$ has age $a_i$. $\textbf{The values of a\_i are distinct}$. Dragon $1$ 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 (1≤i≤n1 \le i \le n), when dragon ii has the letter, it can send the letter to dragon jj in ∣ai−aj∣|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 (1≤t≤n1 \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.

输入格式

The first line contains two space-separated integers $n$ and $k$ ($1 \le n\le 2 \cdot 10^5$, $0 \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 (1≤ai≤1091 \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 (1≤u,v≤n1 \le u, v \le n, u≠vu \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 $n$ space-separated integers $d_1, d_2, \ldots, d_n$, where $d_i$ is the minimum total time needed for dragon $1$ to send the letter to dragon $i$.
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

提示

### 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 ∣23−30∣=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 ∣50−47∣=3|50 - 47| = 3 time.

Here is one optimal way each for the remaining tt's (i→xji \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: 1→071 \xrightarrow{0} 7

Scoring

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

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

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

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

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

Subtask 6 (1414 points): No additional constraints 标签: P15291|2023|广度优先搜索 BFS|最短路|MCC/MCO(马来西亚)

来源

P15291|[MCO 2023] Love Letter