现在的位置: 首页 > 综合 > 正文

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

2013年08月08日 ⁄ 综合 ⁄ 共 5165字 ⁄ 字号 评论关闭
FILE 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.




样例输入:

6
5 19
55 28
38 101
28 62
111 84
43 116
5
11 27
84 99
142 81
88 30
95 38
3
132 73
49 86
72 111
0




样例输出:

**********************************************************
Network #1
Cable 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 #2
Cable 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 #3
Cable 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 200
using 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 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(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  (转载请标明)












抱歉!评论已关闭.