This page is still under construction.

Parts of this page are still being built. What you see may change.

Let's go on an adventure

Time limit1sMemory limit128 MB

Summary
Plan a route from the start to the destination within a fuel limit that collects the largest possible total of region values, counting each visited region once.
Level

Medium7 of 10

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

Problem

Hanshin lost the vote for his party's top seat, took the blame, and gave up his seat in the assembly. Then he left for the east coast of the United States to find a new kind of fun. A friend who lives in Virginia lent him a car, but the tank holds a limited amount of fuel, so he planned a trip around Virginia that stays inside the distance he can drive.

The friend told him what every region is worth. Hanshin wants a route from the start to the destination that makes the total value of the regions he passes through as large as possible, without running out of fuel.

The trip is only for fun, so he may pass through the same city more than once. A region adds its value the first time he passes through it and adds nothing after that. The start and the destination both count toward the total.

Input

The first line has cc, the number of regions he can visit. Each of the next cc lines has the name of a region and the value of that region, separated by a space.

The next line has mm, the number of roads between regions. Each of the next mm lines has the names of the two regions a road connects and the length of that road. Road lengths are integers and every road can be driven in both directions. Two regions may be joined by more than one road, and a road may start and end at the same region.

The next line has pp, the number of test cases. Each of the next pp lines has a start, a destination, and the maximum distance he can drive.

Every value is separated by spaces and no region name contains a space. A name written as Virginia Beach never appears in the input.

  • 1≤c≤111 \le c \le 11
  • each region name is a string of 11 to 2020 letters and digits, and all names are distinct
  • each region value is an integer between −1000-1000 and 10001000
  • 0≤m≤1000 \le m \le 100
  • each road length is an integer between 11 and 1000010000
  • 1≤p≤101 \le p \le 10
  • the maximum distance is an integer between 00 and 10000001000000
  • the start and the destination are region names given above, and they may be the same

Output

For each test case print one line in the form Case x: v, where xx is the case number starting from 11 and vv is the largest total value he can collect. The total includes the start and the destination.

If no route from the start to the destination fits inside the maximum distance, print Not possible in place of vv.

Examples2

  1. Example 1

    Input
    10
    VirginiaBeach 4
    Richmond 5
    Charlottesville 10
    Blacksburg -5
    Roanoke 3
    Fredericksburg 3
    Danville 8
    Harrisonburg 3
    Lynchburg 7
    Arlington 5
    7
    VirginiaBeach Richmond 90
    Richmond Charlottesville 72
    Charlottesville Lynchburg 65
    Richmond Fredericksburg 75
    Fredericksburg Arlington 40
    Lynchburg Roanoke 30
    Roanoke Blacksburg 20
    3
    VirginiaBeach Charlottesville 400
    Charlottesville Lynchburg 75
    VirginiaBeach Richmond 10
    
    Expected output
    Case 1: 29
    Case 2: 17
    Case 3: Not possible
    
  2. Example 2

    Input
    5
    Start 1
    Junction 2
    Middle 3
    Cavern 100
    Finish 5
    4
    Start Junction 2
    Junction Middle 2
    Middle Finish 2
    Junction Cavern 3
    4
    Start Finish 6
    Start Finish 12
    Start Finish 11
    Start Finish 5
    
    Expected output
    Case 1: 11
    Case 2: 111
    Case 3: 11
    Case 4: Not possible