Travelling Salesman Problem

Given a list of cities numbered from 0 to N-1 and a matrix DISTANCE consisting of 'N' rows and 'N' columns, representing the distances between each pair of cities, find the shortest possible route that visits each city exactly once and returns to the starting city.

Input:

The first line contains a single integer 'T' denoting the number of test cases. Each test case is as follows:

The first line contains an integer 'N', representing the number of cities.
The next 'N' lines contain 'N' space-separated integers 'DISTANCE[i][j]', where DISTANCE[i][j] denotes the distance from the ith city to the jth city.

Output:

For each test case, return the minimum distance of the shortest possible route that visits each city exactly once and returns to the starting city.

Example:

Input:
2
3
0 10 15
10 0 20
15 20 0
4
0 29 20 21
29 0 15 17
20 15 0 28
21 17 28 0

Output:
35
60

Constraints:

  • 1 <= T <= 5
  • 2 <= N <= 16
  • 0 <= DISTANCE[i][j] <= 109
  • Time Limit: 1 sec
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
American Express Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter