UVa 216 – Getting in Line, 暴力與回溯

來源:互聯網
上載者:User
216 - Getting
in Line
7259 37.24% 2463 72.39%

題目連結:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=152、

題目類型: 暴力,回溯法

題目:

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 except 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 should be connected into such a chain to minimize the total amount of cable needed. In the installation being constructed, the cabling will run beneath the
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 ease 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.




範例輸入:

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




範例輸出:

**********************************************************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.





題目大意:
電腦網路需要網路中的電腦都相連。這是一個有關“線性網路”的問題。所有電腦被連成一條線。
兩台電腦之間由一條纜線串連, 纜線的長度除了這兩點間的直線長度,還要額外加上16米長。
因為串連的順序方案不同,會產生不同的花費。 
題目要求我們求出花費最小的串連方案。




分析與總結:

(一) 暴力法
首先,不同的方案就是指不同的串連順序。 所有,最直接也容易想到的就是暴力枚舉所有的方案數, 比較每種方案的花費,最後取其中最小的一種。

枚舉方案也很容易, 設各個點為1,2,3……N, 那麼根據串連順序,只需要產生1~N的全排列即可。產生全排列,可以利用STL的next_permutation,
可以十分方便的實現。 而題目所給的資料量非常小,最多隻有8台電腦,8的全排列的複雜度完全就是小意思。



暴力的代碼:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define MAXN 200using namespace std;int arr[MAXN],ans[MAXN];double x[MAXN], y[MAXN];// 計算兩點的距離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) && n){        for(int i=0; i<n; ++i)            scanf("%lf %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;}   // 一個簡單的最佳化,但是提高的效率很明顯            }            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;}



(二) 回溯法
雖然暴力很容易想到,但是也不太靈活,也不那麼“優雅”,而且並不是所有都適用的。
而回溯法是更常用的方法,也更加靈活,更難掌握。
回溯法就是深搜(DFS)的一點點改變。 一般深搜是要訪問所有的解答樹的,而回溯也是把問題分成若干步驟並遞迴求解,但是如果當前步驟沒有合法選擇的
話,就不繼續遞迴下去,而是返回上一及的遞迴調用。這樣就可以節省很多的時間,而不必徒勞去訪問那些“不歸路”。


回溯法代碼:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define MAXN 200using 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(arr));        }        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; // 回溯後清除上次的狀態,這個是重點    }}int main(){#ifdef LOCAL    freopen("input.txt","r",stdin);#endif    int cas=1;    while(~scanf("%d", &n) && n){        for(int i=0; i<n; ++i)            scanf("%lf %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;}



——  生命的意義,在於賦予它意義。 


          原創 http://blog.csdn.net/shuangde800 , By
  D_Double  (轉載請標明)












聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.