GreedyTravelingSalesman
TCO12 Parallel Round 2C · 2012-03-27 · by ir5
Problem Statement
A travelling salesman wants to visit each city of the country in order to sell the products of his company. To travel as quickly as possible, he plans the following strategy.
- First, he visits city 0.
- In each of the next steps, he travels to one of the cities he has not visited yet. When taking the decision which city to visit next, the salesman looks at roads from his current city into all unvisited cities, and picks the shortest of these roads. If there are multiple shortest roads, the salesman picks the one of them that leads into the city with the smallest index.
- He terminates the travel when he has visited all the cities. Note that he does not have to go back to city 0.
The salesman was just about to leave for his journey when he heard a rumor. According to the rumor, one of the roads is just going to be reconstructed. The reconstruction will be done instantly, before the salesman starts to travel. Still, there are two problems. First, the salesman has no idea which one of the roads is the one that's going to be reconstructed. Second, after the reconstruction the new length of the road can be an arbitrary integer between 1 and 9999, inclusive. The salesman is worried how will this change influence his travels in the worst case.
You are given the length of the roads in four separate
Return the distance the salesman will travel in the worst possible case, given that the length of any single road may change.
Constraints
- thousands will contain between 2 and 30 elements, inclusive.
- Each element of thousands will contain N characters, where N is the number of elements in thousands.
- Each element of thousands will contain only digits ('0' - '9').
- hundreds, tens and ones will each contain N elements.
- Each element of hundreds, tens and ones will contain N characters.
- Each element of hundreds, tens and ones will contain only digits ('0' - '9').
- The i-th character of the i-th element of thousands, hundreds, tens and ones will be '0'.
- The length of each road represented by thousands, hundreds, tens and ones is strictly positive.
{"055", "505", "550"}
{"000", "000", "000"}
{"000", "000", "000"}
{"000", "000", "000"}
Returns: 14999
Every pair of two cities is connected by a road with length 5000. The travel length can reach 14999, for example, if the road from 1 to 2 is reconstructed and its new length is 9999.
{"018", "101", "990"}
{"000", "000", "990"}
{"000", "000", "990"}
{"000", "000", "990"}
Returns: 17999
One of the worst situations for the salesman is if the road from 0 to 1 is reconstructed and its new length is 9999. After this change, the salesman's path becomes 0 -> 2 -> 1. The total distance is 8000 + 9999 = 17999.
{"00888", "00999", "00099", "00009", "00000"}
{"00000", "00999", "00099", "00009", "00000"}
{"00000", "10999", "11099", "11109", "11110"}
{"01000", "00999", "00099", "00009", "00000"}
Returns: 37997
The worst possible case is when the length of the road from 0 to 1 is changed to 8000. After this change, the salesman's path becomes 0 -> 1 -> 2 -> 3 -> 4.
{"000000", "000000", "990999", "999099", "999909", "999990"}
{"000000", "000000", "990999", "999099", "999909", "999990"}
{"000000", "000000", "990999", "999099", "999909", "999990"}
{"011111", "101111", "990998", "999099", "999809", "999980"}
Returns: 39994
One of the worst possible cases is when the length of the road from 0 to 1 is changed to 2.
{"00", "00"}
{"00", "00"}
{"00", "00"}
{"01", "10"}
Returns: 9999
Submissions are judged against all 109 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class GreedyTravelingSalesman with a public method int worstDistance(vector<string> thousands, vector<string> hundreds, vector<string> tens, vector<string> ones) · 109 test cases · 2 s / 256 MB per case