UVA216 Getting in Line

Source: Internet
Author: User

 


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 ));}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.