This page is still under construction.

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

Heavy Cargo

Time limit1sMemory limit128 MB

Summary
Given an undirected weighted graph, find the path between two cities whose minimum edge weight is as large as possible, for each test case.
Level

Medium6 of 10

Topics
Graph, Union-find, Sorting, Greedy
Solved
No attempts yet

Problem

Big Johnsson Trucks Inc. manufactures very large trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo it can carry is never limited by the truck itself — it is limited only by the weight restrictions of the roads along the route you drive.

Given a start city and a destination city, determine the maximum load the Godzilla V12 can carry so that a path between the two cities still exists. The load a route can bear equals the smallest weight limit among the roads on that route, and you may choose any route; report the largest such minimum over all possible routes.

Input

The input contains one or more test cases. The first line of each test case has two integers: the number of cities nn (2≤n≤2002 \le n \le 200) and the number of road segments rr (1≤r≤199001 \le r \le 19900) in the road network.

Each of the next rr lines describes one road segment by naming the two cities it connects and its weight limit for trucks. City names are at most 30 characters long and contain no whitespace. Weight limits are integers from 0 to 10000. Every road can be travelled in both directions.

The last line of each test case contains two city names: the start city and the destination city.

The input ends with a line containing two zeros for nn and rr; that line is not processed.

Output

For each test case, print two lines: a line Scenario #x, where xx is the test case number (starting from 1), and a line y tons, where yy is the maximum possible load. Separate consecutive test cases with one blank line.

Examples3

  1. Example 1

    Input
    4 3
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Muenchen
    5 5
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Hamburg 220
    Hamburg Muenchen 170
    Muenchen Karlsruhe
    0 0
    
    Expected output
    Scenario #1
    80 tons
    
    Scenario #2
    170 tons
    
  2. Example 2

    Input
    2 1
    A B 50
    A B
    0 0
    
    Expected output
    Scenario #1
    50 tons
    
  3. Example 3

    Input
    4 4
    A B 10
    B D 10
    A C 30
    C D 20
    A D
    0 0
    
    Expected output
    Scenario #1
    20 tons