#include // Suppose there are four locations, A, B, C and D, and // you can travel directly between any pair of locations. // // We can represent the possible connections as // // B // / | \ // / | \ // A ----+---- D // \ | / // \ | / // C // // There are six distances: // // AB, AC, AD, BC, BD and CD. // // Each distance is a positive integer. // // Write a program that asks the user for these six // distances and determines the shortest distance from // A to D. // // You may travel directly from A to D, or you may travel // through B, C, or both before reaching D. // // For example, possible paths include // // A -> D // A -> B -> D // A -> C -> D // A -> B -> C -> D // A -> C -> B -> D // // Because all distances are positive, there is never an // advantage to visiting the same location more than once. // Thus, these are the only five paths from A to D that // need to be considered. // // If the user enters a distance that is not positive, // immediately terminate the program by returning 0 from // main(). // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // Begin by assuming that the direct path from A to D is // the shortest: // // int distance{ AD }; // // Then calculate the length of each of the other possible // paths. // // Whenever you find a path that is shorter than the // shortest path found so far, update 'distance'. // // For example, the length of // // A -> B -> D // // is // // AB + BD. // // Thus, you can check this path using // // if ( (AB + BD) < distance ) { // distance = AB + BD; // } // // Repeat this for each of the remaining paths. // The five possible paths and their lengths are: // // Path Length // // A -> D AD // A -> B -> D AB + BD // A -> C -> D AC + CD // A -> B -> C -> D AB + BC + CD // A -> C -> B -> D AC + BC + BD // // Notice that the six distances describe the connections // between the four locations, but a path from A to D does // not necessarily use all of them. // Here is one solution: /* int main() { int AB{}; std::cout << "Enter the distance between A and B: "; std::cin >> AB; if ( AB <= 0 ) { return 0; } int AC{}; std::cout << "Enter the distance between A and C: "; std::cin >> AC; if ( AC <= 0 ) { return 0; } int AD{}; std::cout << "Enter the distance between A and D: "; std::cin >> AD; if ( AD <= 0 ) { return 0; } int BC{}; std::cout << "Enter the distance between B and C: "; std::cin >> BC; if ( BC <= 0 ) { return 0; } int BD{}; std::cout << "Enter the distance between B and D: "; std::cin >> BD; if ( BD <= 0 ) { return 0; } int CD{}; std::cout << "Enter the distance between C and D: "; std::cin >> CD; if ( CD <= 0 ) { return 0; } // Begin by assuming that the direct path // // A -> D // // is the shortest path. int distance{ AD }; // Check // // A -> B -> D. if ( (AB + BD) < distance ) { distance = AB + BD; } // Check // // A -> C -> D. if ( (AC + CD) < distance ) { distance = AC + CD; } // Check // // A -> B -> C -> D. if ( (AB + BC + CD) < distance ) { distance = AB + BC + CD; } // Check // // A -> C -> B -> D. if ( (AC + BC + BD) < distance ) { distance = AC + BC + BD; } std::cout << "The shortest distance from A to D is " << distance << std::endl; return 0; } */