Use profiteering to enumerate all possible arrangements and find the minimum cost.
Use an array to save the input, use an array to generate an arrangement, and use another array to save the arrangement of the current minimum cost.
For (int I = 0; I <pointNumber; I ++) {cin> input [I]. x> input [I]. y; array [I] = I; // You must assign an initial value to solution, which may be null in solution [I] = I ;} for (int I = 0; I <pointNumber; I ++) {cin> input [I]. x> input [I]. y; array [I] = I; // You must assign an initial value to solution, which may be null in solution [I] = I;
} The initial value must be assigned to the array storing the result at the beginning, because the first arrangement may be the optimal solution. If the first one is the optimal solution, then ...... I just did not assign a value to it, and Wrong Answer had to do so many times.
Specific Code:
# Include <iostream> # include <algorithm> # include <cmath> # include <cstdio> # include <cstring> using namespace std; struct point {int x; int y ;} input [10]; // record Coordinate double getDistance (double, double); int main () {int pointNumber; int count = 1; while (cin> pointNumber, pointNumber! = 0) {int solution [8]; // stores the ordered int array [8] after each update; for (int I = 0; I <pointNumber; I ++) {cin> input [I]. x> input [I]. y; array [I] = I; // You must assign an initial value to solution, which may be null in solution [I] = I;} double minCost; double tempNumber = 0; for (int I = 0; I <pointNumber-1; I ++) tempNumber + = getDistance (input [array [I]. x, input [array [I]. y, input [array [I + 1]. x, input [array [I + 1]. y); minCost = tempNumber; while (next_permutation (array, Array + pointNumber) {// calculate the cost of the corresponding full arrangement tempNumber = 0; for (int I = 0; I <pointNumber-1; I ++) {tempNumber + = getDistance (input [array [I]. x, input [array [I]. y, input [array [I + 1]. x, input [array [I + 1]. y);} if (tempNumber <minCost) {minCost = tempNumber; memcpy (solution, array, sizeof (array ));}} cout <"************************************ * ********************* "<endl; cout <"Network #" <count ++ <endl; for (int I = 0; I <p OintNumber-1; I ++) {double cost = getDistance (input [solution [I]. x, input [solution [I]. y, input [solution [I + 1]. x, input [solution [I + 1]. y); printf ("Cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", (int) input [solution [I]. x, (int) input [solution [I]. y, (int) input [solution [I + 1]. x, (int) input [solution [I + 1]. y, cost + 16);} printf ("Number of feet of cable required is %. 2lf. \ n ", minCost + 16 * (poi NtNumber-1);} return 0;} double getDistance (double x1, double y1, double x2, double y2) {return sqrt (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);} # include <iostream> # include <algorithm> # include <cmath> # include <cstdio> # include <cstring> using namespace std; struct point {int x; int y;} input [10]; // record Coordinate double getDistance (double, double); int main () {int pointNumber; int count = 1; while (cin> pointNumber, PointNumber! = 0) {int solution [8]; // stores the ordered int array [8] after each update; for (int I = 0; I <pointNumber; I ++) {cin> input [I]. x> input [I]. y; array [I] = I; // You must assign an initial value to solution, which may be null in solution [I] = I;} double minCost; double tempNumber = 0; for (int I = 0; I <pointNumber-1; I ++) tempNumber + = getDistance (input [array [I]. x, input [array [I]. y, input [array [I + 1]. x, input [array [I + 1]. y); minCost = tempNumber; while (next_permutation (array, array + pointNumber) {// calculate the cost of the corresponding full arrangement: tempNumber = 0; for (int I = 0; I <pointNumber-1; I ++) {tempNumber + = getDistance (input [array [I]. x, input [array [I]. y, input [array [I + 1]. x, input [array [I + 1]. y);} if (tempNumber <minCost) {minCost = tempNumber; memcpy (solution, array, sizeof (array ));}} cout <"************************************ * ********************* "<endl; cout <"Network #" <count ++ <endl; for (int I = 0; I <pointNumber-1; I ++) {double cost = getDistance (input [solution [I]. x, input [solution [I]. y, input [solution [I + 1]. x, input [solution [I + 1]. y); printf ("Cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", (int) input [solution [I]. x, (int) input [solution [I]. y, (int) input [solution [I + 1]. x, (int) input [solution [I + 1]. y, cost + 16);} printf ("Number of feet of cable required is %. 2lf. \ n ", minCost + 16 * (pointNumber-1);} return 0;} double getDistance (double x1, double y1, double x2, double y2) {return sqrt (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2 ));}