Ultraviolet A 216-getting in line, brute force and backtracking

Source: Internet
Author: User
Tags cmath
216-getting
In line
7259 37.24% 2463 72.39%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 152,

Question type: brute force, backtracking

Question:

Computer networking requires that the computers in the network be linked.

This problem considers a ''linear "network in which the computers are chained together so that each is connected to exactly two others tables t for the two computers on the ends of the chain which are connected
To only one other computer. A picture is shown below. here the computers are the black dots and their locations in the network are identified by planar coordinates (relative to a coordinate system not shown in the picture ).

Distances between linked computers in the network are shown in feet.

For various reasons it is desirable to minimize the length of cable used.

Your problem is to determine how the computers shocould be connected into such a chain to minimize the total amount of cable needed. In the installation being constructed, the cabling will run beneath
Floor, so the amount of cable used to join 2 adjacent computers on the network will be equal to the distance between the computers plus 16 additional feet of cable to connect from the floor to the computers and provide some slack for installation of installation.

The picture below shows the optimal way of connecting the computers shown above, and the total length of cable required for this configuration is (4 + 16) + (5 + 16) + (5.83 + 16) + (11.18 + 16) = 90.01 feet.




Sample input:

65 1955 2838 10128 62111 8443 116511 2784 99142 8188 3095 383132 7349 8672 1110




Sample output:

**********************************************************Network #1Cable requirement to connect (5,19) to (55,28) is 66.80 feet.Cable requirement to connect (55,28) to (28,62) is 59.42 feet.Cable requirement to connect (28,62) to (38,101) is 56.26 feet.Cable requirement to connect (38,101) to (43,116) is 31.81 feet.Cable requirement to connect (43,116) to (111,84) is 91.15 feet.Number of feet of cable required is 305.45.**********************************************************Network #2Cable requirement to connect (11,27) to (88,30) is 93.06 feet.Cable requirement to connect (88,30) to (95,38) is 26.63 feet.Cable requirement to connect (95,38) to (84,99) is 77.98 feet.Cable requirement to connect (84,99) to (142,81) is 76.73 feet.Number of feet of cable required is 274.40.**********************************************************Network #3Cable requirement to connect (132,73) to (72,111) is 87.02 feet.Cable requirement to connect (72,111) to (49,86) is 49.97 feet.Number of feet of cable required is 136.99.





Question:
The computer network must be connected to all the computers in the network. This is a problem with linear networks. All computers are connected together.
The two computers are connected by a cable. In addition to the line length between the two computers, the cable length is also 16 meters in length.
Because the connection sequence scheme is different, it will produce different costs.
The question requires us to find the connection solution with the minimum cost.




Analysis and Summary:

(1) Violence Law
First, different solutions refer to different connection sequence. All, the most direct and easy to think of is to enumerate the number of all solutions, compare the cost of each solution, and finally take the smallest one.

It is also easy to set the enumeration scheme to 1, 2, 3 ...... N, you only need to generate 1 ~ N is in full order. Generate a full arrangement, you can use the next_permutation of STL,
It can be easily implemented. The data volume given by the questions is very small, with a maximum of eight computers. The complexity of the 8-bit full arrangement is totally insignificant.



Brute force code:
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <cmath> # define maxn 200 using namespace STD; int arr [maxn], ans [maxn]; Double X [maxn], Y [maxn]; // calculate the distance between two points: Double DIS (double X1, double Y1, double X2, double Y2) {return POW (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2), 0.5);} int main () {# ifdef local freopen ("input.txt ", "r", stdin); # endif int N, CAS = 1; while (~ Scanf ("% d", & N) {for (INT I = 0; I <n; ++ I) scanf ("% lf ", & X [I], & Y [I]); For (INT I = 0; I <n; ++ I) Arr [I] = I; double min = 2147483645; do {double sum = 0; bool flag = false; For (INT I = 1; I <n; ++ I) {double T = DIS (X [arr [I], Y [arr [I], X [arr [I-1], Y [arr [I-1]); sum + = T; If (sum> min) {flag = true; break;} // a simple optimization, but the improved efficiency is obvious} If (FLAG) continue; if (sum <min) {min = sum; memcpy (ANS, arr, sizeof (ARR) ;}} while (next_permutation (ARR, arr + n )); printf ("************************************* * ******************** \ n "); printf ("Network # % d \ n", CAS ++); For (INT I = 1; I <n; ++ I) {double T = DIS (X [ans [I], Y [ans [I], X [ans [I-1], Y [ans [I-1]); printf ("cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", (INT) x [ans [I-1], (INT) y [ans [I-1], (INT) x [ans [I], (INT) Y [ans [I], T + 16);} printf ("Number of feet of cable required is %. 2f. \ n ", min + (n-1) * 16);} return 0 ;}



(2) backtracking
Although violence is easy to think of, it is not flexible and not so "elegant", and not all of them apply.
Backtracking is a more common method, more flexible and more difficult to grasp.
Backtracking is a little change in Deep Search (DFS. Generally, Deep Search accesses all the answer trees, and backtracking divides the problem into several steps and recursively solves the problem. However, if the current step has no legal choice
Instead of recursion, the previous and recursive calls are returned. In this way, you can save a lot of time without having to access those "not returning" in vain ".


Backtracking code:
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <cmath> # define maxn 200 using namespace STD; int N, arr [maxn], ANS [maxn]; double minsum, X [maxn], Y [maxn]; bool vis [maxn]; double DIS (double X1, double Y1, double X2, double Y2) {return POW (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2), 0.5);} void DFS (INT cur, double sum) {If (cur = N) {If (sum <minsum) {minsum = sum; memcpy (ANS, arr, sizeof (Ar R);} return;} If (sum> = minsum) return; For (INT I = 0; I <n; ++ I) {If (vis [I]) continue; vis [I] = true; arr [cur] = I; If (cur = 0) DFS (cur + 1, 0 ); else {double T = DIS (X [arr [cur], Y [arr [cur], X [arr [cur-1], Y [arr [cur-1]); DFS (cur + 1, sum + T + 16);} vis [I] = false; // clear the last state after backtracking. This is the focus} int main () {# ifdef local freopen ("input.txt", "r", stdin ); # endif int CAS = 1; while (~ Scanf ("% d", & N) {for (INT I = 0; I <n; ++ I) scanf ("% lf ", & X [I], & Y [I]); For (INT I = 0; I <n; ++ I) Arr [I] = I; minsum = 2147483646; memset (VIS, 0, sizeof (VIS); DFS (0, 0 ); printf ("************************************* * ******************** \ n "); printf ("Network # % d \ n", CAS ++); For (INT I = 1; I <n; ++ I) {double T = DIS (X [ans [I], Y [ans [I], X [ans [I-1], Y [ans [I-1]); printf ("cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", (INT) x [ans [I-1], (INT) y [ans [I-1], (INT) x [ans [I], (INT) Y [ans [I], T + 16);} printf ("Number of feet of cable required is %. 2f. \ n ", minsum);} return 0 ;}



-- The meaning of life is to give it meaning.


Original Http://blog.csdn.net/shuangde800 ,
D_double(For details, refer)












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.