#abc338d. D - Island Tour
D - Island Tour
Score: points
问题描述
AtCoder群岛由个岛屿和座桥梁连接而成。岛屿编号从1到,其中第座桥()双向连接着岛屿和,而第座桥则双向连接着岛屿和。除了通过桥梁,无法在岛屿之间通行。
在这些岛屿上,定期进行一场从岛屿出发并按顺序访问岛屿的旅行。旅行过程中可能会经过除指定访问岛屿之外的其他岛屿,并将旅行期间总共穿越桥梁的次数定义为该旅行的长度。
更准确地说,一个旅行是一个包含个岛屿的序列,满足以下所有条件,其长度被定义为:
- 对于所有,岛屿和由一座桥梁直接相连。
- 存在一些,使得对于所有,有。
由于财务困难,群岛将关闭一座桥梁以减少维护成本。请确定当选择最优要关闭的桥梁时,旅行的最小可能长度。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
The AtCoder Archipelago consists of islands connected by bridges. The islands are numbered from to , and the -th bridge () connects islands and bidirectionally, while the -th bridge connects islands and bidirectionally. There is no way to travel between islands other than crossing the bridges.
On the islands, a tour that starts from island and visits islands in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.
More precisely, a tour is a sequence of islands that satisfies all the following conditions, and its length is defined as :
- For all , islands and are directly connected by a bridge.
- There are some such that for all , .
Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1
3 3
1 3 2
Sample Output 1
2
- If the first bridge is closed: By taking the sequence of islands , it is possible to visit islands in order, and a tour of length can be conducted. There is no shorter tour.
- If the second bridge is closed: By taking the sequence of islands , it is possible to visit islands in order, and a tour of length can be conducted. There is no shorter tour.
- If the third bridge is closed: By taking the sequence of islands , it is possible to visit islands in order, and a tour of length can be conducted. There is no shorter tour.
Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is .
The following figure shows, from left to right, the cases when bridges are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.
Sample Input 2
4 5
2 4 2 4 2
Sample Output 2
8
The same island may appear multiple times in .
Sample Input 3
163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369
Sample Output 3
390009
update @ 2024/3/10 01:29:35