Question

3 Given that somebody has already defined the required structs represent a road map as a graph, like this: to struct town string name; int distance; roadlink roads; ) struct roadlink int length; town whereto; roadlink next; ) s you can see, the programmer chose not to give each town a vector for the roads that le ad from it, but a linked list instead. Each roadlink at it leads to. The programmer has even written the code that correctly constructs the graph. records the length of a road leading from the current town, and the town th What you have to do is write the code that will find the length of the shortest path between two towns. Write a function int shortest (town A, town B) that performs this task. Of course you may define any other functions or data structures that will make the job easier.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

int ans=INT_MAX;

void shortest(town* A,town* B,int currentsum,town* currenttown){

if(currenttown->name==B->name) {

ans=min(ans,currentsum);

return ;

}

while(currenttown->roads!=NULL){

shortest(A,B,currentsum+currenttown->roads->length,currenttown->roads->whereto);

currenttown->roads=currenttown->roads->next;

}

return;

}

Description : It is a basic dfs call from the source town and record the current distance , In the base statement this current distance is compared with the overall minimum distance till now.

Our answer is stored in variable "ans".

Hope it helps :)

Add a comment
Know the answer?
Add Answer to:
3 Given that somebody has already defined the required structs represent a road map as a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT