루미 알고리즘

#1572실버Indoor Orientation

PO 2023 학교 예선코딩출제 Pär Söderhjelm영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

A rapidly growing sport is indoor orienteering, where the largest competitions attract thousands of participants running around places like a campus. Through the organizers' carefully planned barriers, it becomes a labyrinthine challenge to navigate between control points. Often, it is essential to optimize the use of stairs, and this is what we will focus on in this task. Given a map divided into N areas, you are to calculate the minimal number of stair runs the orienteer needs to make to visit the N areas in sequence (to reach a control point in each).

The building contains several vertical stairwells labeled with letters. Each area is located on a certain floor and is connected to one or more stairwells. A stairwell does not need to be connected to an area on every floor. For example, stairwell B can connect an area on floor 1 with another area on floor 3, without being connected to any area on the intermediate floor 2. Moving one floor up or down in a stairwell costs one time unit. However, we do not care about the time it takes to move within an area and find the control point.

Input

The first line contains the integer N (3 ≤ N ≤ 15), the number of areas.

The next N lines each describe one area. Each line starts with the floor that the area is located at (an integer between 1 and 100), followed by a string with at least one letter, each letter being chosen from A-Z, which describes the stairwells the area is connected to.

Output

Print an integer: the minimal time required to visit all the N areas in the order given in the input, starting in area 1 and ending in area N.

Scoring

Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.

GroupPointsConstraints
120All strings are a single A.
280No additional constraints.

Explanation of Samples

(그림은 원문 저장소에서 볼 수 있습니다)

Figure 1: Cross-section of the building in example 2. The large numbers indicate floor numbers, and the small ones indicate the numbering of the areas. One possible shortest path through the entire course is: AAD – EEE – CBAA – DEE – EEDD – DAAB – CFF Note that the control points must be visited in order.

In the second example, there is a path with 24 steps (the letters indicate which staircase was used at each step).

예제

예제 1

입력
3
2 A
100 A
3 A
출력
195

예제 2

입력
8
1 AB
4 DE
1 CEF
3 AD
2 E
2 D
2 BC
3 F
출력
24

출처 표기

출처: Programmeringsolympiaden 2023년 학교 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.