Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Forgotten Report
There are N cities in a state. You start your ride from the first city. You have to visit all other cities exactly once and finally return to your origin city. After visiting each city, you collect the analysis report.
But when you reached the last unvisited city, you remembered that you did not collect the report from city K. So, now you decide to first collect the report from city K and then return to your home city.

Given the distances between each pair of cities, you are required to find the shortest possible distance of your whole journey.
INPUT

The input begins with T (Number of test cases).

Second line contains K (City No. Where you forgot to collect the report). Third line contains N (Number of cities).

Next there are N lines, Ith line have exactly N numbers denoting distance from city / to all N cities.

OUTPUT

For each test case, print the Minimum Distance of total journey. Answer for each test case should come in a new line


What I have tried:

I tried but I not got answer please help me to solve this
Posted
Updated 24-Nov-21 1:37am
Comments
Richard MacCutchan 24-Nov-21 4:32am    
Start by drawing some sample maps and routes on paper. Using the information in the question above think how you would calculate the best routes.
Patrice T 24-Nov-21 7:19am    
What about showing your work ?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Read the question carefully, and try it on paper first - when you have a "paper based manual solution" sorted in your mind, then think about computerizing it.
But just posting the assignment and saying "I tried" isn't going to get you anywhere!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
This is a variant of the Travelling Salesman Problem[^] in whichever city is chosen as the starting point must make a loop to city K after all the other cities have been visited. In the article, you will see that you need to try all possibilities (permutations). That is, the problem is O(n!).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900