PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000470. Phí qua đường (The Toll! Revisited)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phí qua đường (The Toll! Revisited)

The Toll! Revisited

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Places in a bidirectional road network have single-letter English names. Uppercase names represent towns and lowercase names villages. Entering a village costs one item; entering a town with qq items costs ⌈q/20⌉\lceil q/20\rceil items. Leaving is free, including at the start. Deliver pp items at the destination. Minimize the initial cargo; among optimal routes choose the lexicographically smallest name sequence, with uppercase before lowercase.

Input

Read nn roads, each represented by a pair of letters, then p,s,tp,s,t: the required delivery amount, start and destination.

Output

Print three lines: Case 1:, the minimum initial cargo, and the route with names separated by hyphens.

Subtasks

Subtask 1 (20 points)

  • V≤8V\le8
  • n≥0n\ge0, 1≤p<1091\le p<10^9; VV counts distinct names including s,ts,t; a route from ss to tt exists; names are English letters.

Subtask 2 (30 points)

  • V≤20V\le20
  • n≥0n\ge0, 1≤p<1091\le p<10^9; VV counts distinct names including s,ts,t; a route from ss to tt exists; names are English letters.

Subtask 3 (50 points)

  • V≤52V\le52
  • n≥0n\ge0, 1≤p<1091\le p<10^9; VV counts distinct names including s,ts,t; a route from ss to tt exists; names are English letters.

Examples

Example 1

Input

1
a Z
19 a Z

Output

Case 1:
20
a-Z

Explanation

Entering Z with 20 items costs one.

Example 2

Input

5
A D
D X
A b
b c
c X
39 A X

Output

Case 1:
44
A-b-c-X

Explanation

44 items become 43, 42 and 39; the route through D requires 45.