The Bored Traveling Salesman (Small)
Time limit5sMemory limit512 MB
Choose a start city and a depth-first ticket tour of the graph so the concatenated first-visit ZIP codes form the smallest number.
- Level
Medium6 of 10
- Topics
- Backtracking, DFS, Graph
- Solved
- No attempts yet
Problem
Your company is sending you on an international sales trip. You must visit cities, numbered 1 to , and bidirectional flight routes connect some pairs of cities. Every city must be visited at least once.
You travel by booking tickets. A ticket follows these rules.
- One ticket consists of two flights. One is the outbound flight from a city to a city , the other is the return flight from city back to city . Both flights use the same bidirectional route.
- You must take the outbound flight first and its matching return flight later. You may take other flights in between.
- At most one outbound flight arrives at each city. Return flights have no such limit, so several return flights may arrive at the same city.
- You must take every flight of every ticket you book.
- As long as you keep these rules, you visit the cities in any order you like.
- You choose any city as your starting city. No outbound flight may arrive at your starting city.
Each city has a 5 digit ZIP code, and all ZIP codes within one test case are different. Every time you enter a city for the first time, including the city you start from, you write down its ZIP code. Concatenating the ZIP codes in the order you wrote them gives one large number. Find the smallest number you can obtain.
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains the number of cities and the number of bidirectional flight routes .
The next lines contain the 5 digit ZIP codes of city 1 through city in order, one per line. No ZIP code starts with 0, and all ZIP codes within one test case are different.
The next lines each contain two integers and (), meaning a bidirectional flight route exists between city and city . All routes within one test case are different.
Visiting every city under the rules above is always possible.
Output
For each test case, print one line in the form "Case #x: y", where is the test case number starting from 1 and is the smallest number you can obtain.
Note
Consider six cities whose ZIP codes are 10001, 10002, 10003, 10004, 10005 and 10006 in order, with routes (1, 2), (1, 6), (2, 3), (2, 4), (3, 5) and (4, 5). This trip gives the smallest number.
- Start from city 1 and write 10001.
- Take the outbound flight from city 1 to city 2 and write 10002.
- Take the outbound flight from city 2 to city 3 and write 10003.
- Take the return flight from city 3 to city 2.
- Take the outbound flight from city 2 to city 4 and write 10004.
- Take the outbound flight from city 4 to city 5 and write 10005.
- Take the return flight from city 5 to city 4.
- Take the return flight from city 4 to city 2.
- Take the return flight from city 2 to city 1.
- Take the outbound flight from city 1 to city 6 and write 10006.
- Take the return flight from city 6 to city 1.
The result is 100011000210003100041000510006, and no smaller number is possible.