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

#BF0000006. Bay đến Fredericton (Flying to Fredericton)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bay đến Fredericton (Flying to Fredericton)

Flying to Fredericton

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Find cheapest Calgary to Fredericton fares with no more than the queried number of stopovers.

A traveler wants to fly from Calgary to Fredericton. The nn cities are listed from nearest to farthest: Calgary first and Fredericton last. Every flight goes from an earlier city in this list to a later city, and multiple airlines may offer different fares between the same city pair.

There are qq queries. Each query specifies the maximum number kk of intermediate stopovers the traveler accepts. Find the minimum Calgary-to-Fredericton airfare using no more than k+1k+1 flights, or print the prescribed message if no suitable journey exists. All queries share the same flight network.

Input

Read nn, then nn city names (Calgary first, Fredericton last), mm flight records (from, to, fare), and qq followed by qq allowed stop counts.

Output

Print Scenario #1, then Total cost of flight(s) is $X or No satisfactory flights for each query.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20; 0≤m≤500\le m\le50; 1≤q≤101\le q\le10; 0≤k≤10000\le k\le1000; 1≤p≤100001\le p\le10000; city names distinct, at most 20 letters.
  • Subtask 2 (30 points): 2≤n≤1002\le n\le100; 0≤m≤5000\le m\le500; 1≤q≤101\le q\le10; 0≤k≤10000\le k\le1000; 1≤p≤100001\le p\le10000; city names distinct, at most 20 letters.
  • Subtask 3 (50 points): 2≤n≤1002\le n\le100; 0≤m≤10000\le m\le1000; 1≤q≤101\le q\le10; 0≤k≤10000\le k\le1000; 1≤p≤100001\le p\le10000; city names distinct, at most 20 letters.

Examples

Input

4
Calgary
Winnipeg
Ottawa
Fredericton
6
Calgary Winnipeg 125
Calgary Ottawa 300
Winnipeg Fredericton 325
Winnipeg Ottawa 100
Calgary Fredericton 875
Ottawa Fredericton 175
3 2 1 0

Output

Scenario #1
Total cost of flight(s) is $400
Total cost of flight(s) is $450
Total cost of flight(s) is $875

Explanation

The three stop limits 2, 1, 0 yield minimum fares of 400400, 450450, 875875 respectively.