The Mark of a Wizard

Time limit1sMemory limit128 MB

Summary
On a small DAG, find the shortest path from A to F and the fewest intersections to mark so that following marks still guarantees the shortest time.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Goblins have a web of tunnels leading up from their underground lairs. In the schematic of one of their simpler systems, the vertical direction on the page points upward. The lair is at label AA and the surface (the exit) is at label FF; the other labels mark tunnel intersections. Tunnels heading toward the surface from an intersection are drawn going upward. This is a 2D schematic of a 3D system, so paths that appear to cross anywhere other than at a lettered intersection do not actually meet (for example, edge BDBD does not intersect edge CECE).

A group of good wizards often needs to rush up through such tunnels to the surface along a path that takes the minimum possible time. Many upward tunnels can leave an intersection, and in general there is no obvious way to pick the best one. The wizards mapped the systems, recording the time needed to travel up from one intersection to the next. In a hurry they cannot consult elaborate notes, and they do not want to help enemies who might be rushing through the same tunnels. So they place a very discreet private mark at some intersections. A mark on a tunnel leaving an intersection means a wizard should take that tunnel. Because the system might be discovered, especially if there are too many marks, they want to mark as few intersections as possible while still guaranteeing that a wizard who always heads up and always takes a marked tunnel where one is shown will emerge in the minimum time.

Take the first system described above. One valid set of marks points from AA to BB and from BB to CC. There is only one way up from CC, so a wizard following the marks goes A→B→C→FA \to B \to C \to F in total time 3+1+4=83 + 1 + 4 = 8, the minimum possible.

The same system needs only one mark: a single mark at EE directing a wizard to DD. This does not fully determine a path -- from AA a wizard may go toward BB or EE, and from BB there are again two choices -- but every one of these upward paths covers the same minimum time 88. The mark at EE cannot be removed, or a wizard might take A→E→C→FA \to E \to C \to F for time 2+3+4=92 + 3 + 4 = 9. So one mark suffices for this system (another single-mark option is a mark at AA pointing to BB).

Help the wizards plan their marks. Be careful with your algorithm -- this can be time-consuming.

Input

The input consists of 1 to 16 data sets, followed by a line containing only 00.

The first line of a data set contains an integer nn (2≤n≤172 \le n \le 17), the number of labeled places; these are the starting point, the ending point, and any intersections in between.

Each of the next nn lines describes the upward tunnels from one labeled point. Every line has the same form, with blank-separated parts: first a letter labeling the tunnel's starting point, then an integer uu, the number of upward tunnels from that point. After the label and uu come uu pairs (letter, time), each giving the integer timetime (1≤time≤5001 \le time \le 500) to travel through a tunnel from the current point to the point with that letter. The labels at the start of the nn lines are taken in order from the beginning of the capital letters. AA is always the starting point and the last letter used is always the exit. Only the final line (the exit) has u=0u = 0; every earlier line has 1≤u≤61 \le u \le 6.

Limiting assumptions:

  • Wizards may only follow upward tunnels out of any intersection.
  • The total number of tunnels is at most 35.
  • There is always a minimum-time path from the lair to the surface, and it uses at most 7 tunnels.
  • Every labeled point other than the start and the exit has at least one tunnel coming up to it and at least one heading up from it.

The first data set of the example corresponds to the tunnel system described above.

Output

Print one line for each data set with two space-separated numbers: the minimum time to travel from the lair to the surface, and the minimum number of marks needed to guarantee that every wizard travels in that minimum time.

Examples2

  1. Example 1

    Input
    6
    A 2 B 3 E 2
    B 2 C 1 D 4
    C 1 F 4
    D 1 F 1
    E 2 C 3 D 5
    F 0
    7
    A 3 B 1 C 5 D 4
    B 2 C 2 E 5
    C 2 E 4 F 3
    D 2 C 2 F 3
    E 1 G 6
    F 1 G 4
    G 0
    7
    A 2 B 2 C 4
    B 2 D 4 C 1
    C 2 D 3 E 5
    D 2 F 4 E 2
    E 2 F 2 G 5
    F 1 G 2
    G 0
    0
    
    Expected output
    8 1
    10 3
    12 2
    
  2. Example 2

    Input
    2
    A 1 B 5
    B 0
    0
    
    Expected output
    5 0