QOJ.ac

QOJ

时间限制: 1 s 内存限制: 2048 MB 总分: 100

#5540. 市政厅

统计

你是 ICPC 市的市长。这座城市有 $N$ 个交叉路口,编号从 $1$ 到 $N$,其中交叉路口 $i$ 的海拔高度为 $H_i$。你的家位于交叉路口 $S$,而市政厅位于交叉路口 $T$。

城市中有 $M$ 条双向道路,编号从 $1$ 到 $M$,连接着这些交叉路口。第 $i$ 条道路直接连接交叉路口 $U_i$ 和 $V_i$。每对交叉路口之间最多只有一条直接相连的道路。这些道路的连接方式保证了从任何一个交叉路口出发,经过一条或多条道路,都可以到达其他任何交叉路口。

每天早上,你都要从家骑车前往市政厅。假设你正在通过一条直接连接交叉路口 $u$ 和 $v$ 的道路,那么通过该道路所消耗的能量为 $(H_u - H_v)^2$。一条路径所需的总能量是该路径上经过的每条道路所消耗能量之和。

作为市长,你可以将最多一个交叉路口的海拔高度更改为任何非负实数。利用这个机会,你希望最小化从家骑车到市政厅所需的总能量。

输入格式

输入的第一行包含 4 个整数 $N, M, S, T$ ($2 \le N \le 100\,000$; $N - 1 \le M \le \min(\frac{N(N-1)}{2}, 200\,000)$; $1 \le S, T \le N$; $S \neq T$)。下一行包含 $N$ 个整数 $H_i$ ($0 \le H_i \le 100\,000$),表示交叉路口 $i$ 的海拔高度。接下来的 $M$ 行,每行包含 2 个整数 $U_i, V_i$ ($1 \le U_i < V_i \le N$),表示由道路 $i$ 直接连接的交叉路口。每对交叉路口之间最多只有一条直接相连的道路。此外,这些道路的连接方式保证了从任何一个交叉路口出发,经过一条或多条道路,都可以到达其他任何交叉路口。

输出格式

输出一个实数,表示所需的最小总能量。如果你的答案的绝对误差不超过 $10^{-6}$,则被视为正确。

样例

输入 1

5 6 1 3
5 100 8 2 10
1 2
2 3
2 5
1 4
4 5
3 5

输出 1

4.500000

说明 1

为了获得最小的总能量,你应该将交叉路口 $2$ 的海拔高度更改为 $6.5$。此时,骑行路线 $1 \to 2 \to 3$ 所需的总能量为 $(5 - 6.5)^2 + (8 - 6.5)^2 = 4.5$。

输入 2

5 5 1 5
1 2 10 10 4
1 2
2 3
2 4
3 5
4 5

输出 2

3.000000

说明 2

为了获得最小的总能量,你可以选择交叉路口 $3$ 或交叉路口 $4$,并将它的海拔高度更改为 $3$。

输入 3

5 4 1 4
8 8 8 8 100
1 2
2 3
3 4
4 5

输出 3

0.000000

说明 3

骑行路线 $1 \to 2 \to 3 \to 4$ 所需的总能量为 $0$,因为其所有交叉路口的海拔高度相同。更改任何交叉路口的高度都不会减少所需的总能量。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.