| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 27 | 13 | 12 | 50.000% |
An undirected graph with $N$ vertices is given. Each vertex $i$ has an integer value $A_i$. The graph has $M$ edges, and all edges have length $1$.
You want to choose two distinct vertices $a$ and $b$ that satisfy the following condition:
Among all pairs of vertices $(a, b)$ that satisfy the condition, find the minimum possible value of $|A_a + A_b|$. In other words, choose two distinct vertices that share a common neighbor so that the absolute value of the sum of their weights is as small as possible.
The first line contains two integers $N$ and $M$, the number of vertices and the number of edges. ($3 \le N \le 100\,000, 2 \le M \le 2N$)
The second line contains $N$ integers $A_1, A_2, \ldots, A_N$, where $A_i$ is the value of vertex $i$. ($-10^9 \le A_i \le 10^9$)
Each of the next $M$ lines contains two integers $u$ and $v$, indicating an undirected edge between vertices $u$ and $v$. ($1 \le u < v \le N$)
It is guaranteed that the given graph is connected and contains no multiple edges or self-loops.
Print a single integer: the minimum value of $|A_a + A_b|$ among all pairs of distinct vertices $(a, b)$ such that there exists at least one path of length exactly $2$ between $a$ and $b$. It is guaranteed that at least one such pair $(a, b)$ exists.
| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 40 | $3 \le N \le 100$ |
| 2 | 20 | $3 \le N \le 2\,000$ |
| 3 | 20 | $3 \le N \le 100\,000$, In this subtask, all $A_i$ satisfies $A_i \ge 0$ |
| 4 | 20 | $3 \le N \le 100\,000$ |
3 2 4 3 5 1 3 2 3
7
There is a path of length $2$ between vertices $(1, 2)$, namely 1-3-2. On the other hand, there is no path of length $2$ between $(1, 3)$ or $(2, 3)$, so the only valid pair $(a, b)$ is $(1, 2)$. In this case, $|A_a + A_b| = 7$.
This example satisfies the conditions of all subtasks.
3 3 -4 -3 5 1 2 1 3 2 3
1
In this example, all pairs $(1, 2)$, $(1, 3)$, and $(2, 3)$ have a path of length $2$: 1-3-2, 1-2-3, and 2-1-3, respectively. In this case, the pair $(a, b)$ that minimizes the absolute value of the sum of their weights is $(1, 3)$, and the minimum value is $1$.
This example satisfies the conditions of Subtask 1, 2, and 4
4 4 -3 2 -4 7 1 2 2 3 3 4 1 4
7
In this example, only the two pairs $(1, 3)$ and $(2, 4)$ have a path of length $2$ between them.
This example satisfies the conditions of Subtask 1, 2, and 4.
University > 서강대학교 > CSE4152 문제해결프로그래밍실습 > 2025-2학기 기말고사 코딩 테스트 4번