`
473687880
  • 浏览: 535107 次
文章分类
社区版块
存档分类
最新评论

Getting in Line UVA 216

 
阅读更多



Getting in Line

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.

Input

The input file will consist of a series of data sets. Each data set will begin with a line consisting of a single number indicating the number of computers in a network. Each network has at least 2 and at most 8 computers. A value of 0 for the number of computers indicates the end of input.

After the initial line in a data set specifying the number of computers in a network, each additional line in the data set will give the coordinates of a computer in the network. These coordinates will be integers in the range 0 to 150. No two computers are at identical locations and each computer will be listed once.

Output

The output for each network should include a line which tells the number of the network (as determined by its position in the input data), and one line for each length of cable to be cut to connect each adjacent pair of computers in the network. The final line should be a sentence indicating the total amount of cable used.

In listing the lengths of cable to be cut, traverse the network from one end to the other. (It makes no difference at which end you start.) Use a format similar to the one shown in the sample output, with a line of asterisks separating output for different networks and with distances in feet printed to 2 decimal places.

Sample Input

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

Sample Output

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

就是求最小生成树,可以回溯+搜索,因为n<=8所以可以暴力枚举,我在边界条件卡了很久。

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>

using namespace std;

int num[10];
int num1[10];
int n;

class P
{
public:
    int x,y;
}point[10];

double dis()
{
    double sum=0;
    for(int i=1;i<n;i++)
    {
        sum=sum+sqrt((point[num[i]].x-point[num[i-1]].x)*(point[num[i]].x-point[num[i-1]].x)+(point[num[i]].y-point[num[i-1]].y)*(point[num[i]].y-point[num[i-1]].y))+16;
    }
    return sum;
}

int main()
{
    int k=0;
    while(cin>>n&&n)
    {
        memset(point,0,sizeof(point));
        memset(num,0,sizeof(num));
        memset(num1,0,sizeof(num1));
        int i;
        for(i=0;i<n;i++)
            cin>>point[i].x>>point[i].y;
        for(i=0;i<n;i++)
            num[i]=i;
        double minlen=dis();
        memcpy(num1,num,sizeof(num));
        while(next_permutation(num,num+n))
        {
            if(dis()<minlen)
            {
                memcpy(num1,num,sizeof(num));
                minlen=dis();
            }
        }
        cout<<"**********************************************************"<<endl;
        cout<<"Network #"<<++k<<endl;
        for(i=1;i<n;i++)
        {
            double d=sqrt((point[num1[i]].x-point[num1[i-1]].x)*(point[num1[i]].x-point[num1[i-1]].x)+(point[num1[i]].y-point[num1[i-1]].y)*(point[num1[i]].y-point[num1[i-1]].y));
            printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n",point[num1[i-1]].x,point[num1[i-1]].y,point[num1[i]].x,point[num1[i]].y,d+16);
            //cout<<"Cable requirement to connect ("<<point[num1[i-1]].x<<","<<point[num1[i-1]].y<<") to ("<<point[num1[i]].x<<","<<point[num1[i]].y<<") is ";
            //cout<<fixed<<setprecision(2)<<d+16<<" feet."<<endl;
        }
        printf("Number of feet of cable required is %.2lf.\n",minlen);
        //cout<<"Number of feet of cable required is "<<fixed<<setprecision(2)<<minlen<<"."<<endl;
    }
    return 0;
}

因为第一个排列可能最优化,我忘记给他赋值了,找了好久的bug。

分享到:
评论

相关推荐

    Introducing Erlang Getting Started in Functional Programming(2nd) epub

    Introducing Erlang Getting Started in Functional Programming(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Delete:The Virtue of Forgetting

    《Delete:The Virtue of Forgetting》这本书是由Viktor Mayer-Schönberger所著,他是大数据时代的重要作者之一。本书深入探讨了在数字时代,遗忘的美德,即遗忘的重要性及其对于个人隐私和社会价值的贡献。随着...

    Online Continual Learning in Image Classification An Empirical

    在图像分类任务中,连续学习面临的挑战是“灾难性遗忘”(Catastrophic Forgetting),即模型在学习新任务时可能会忘记之前学过的任务。为了解决这个问题,连续学习方法通常采用策略如经验回放(Experience Replay)...

    Getting to Know Vue js

    You’ll also explore how to use Single File Components and the Vue.js Command Line Interface (CLI) to build components in a single file and add in build tools as you see fit. Getting started with a ...

    Getting Real 完整 完全中文版

    Getting Real是一种软件开发的方法论,它强调构建更小型、更快速、更高质量的Web应用。其核心理念是追求精炼,意味着更少的代码、更少的软件、更少的功能和更少的文档工作。Getting Real还倡导精益开发,即保持开发...

    Getting Started With MachineLearning (all in one)_V0.91

    Getting Started With MachineLearning (all in one)_V0.91

    Introducing Elixir - Getting Started in Functional Programming

    本书向读者介绍了Elixir如何将Erlang的强大的功能编程与一个看起来更像Ruby的方法相结合。读者将会了解Elixir如何简化Erlang的一些恶劣的角落,并通过强大的宏功能达到元编程。介绍Elixer是开发人员对于编程的开发...

    Getting started in SAP

    This book shares practical advice about how to get started in a lucrative career working with SAP software. You’ll learn what SAP is, what kinds of jobs there are in the SAP world, and how you can ...

    Getting Real中文版

    那么正是时候Getting Real. Getting Real 是一种更小规模,更 快速,更高质量的软件构建方法。 • Getting Real是关于省略所有表达现实(图表,曲线,矩形,箭头,统计图),而构 建现实。 • Getting real 是追求...

    Getting Started with Processing

    materials available online. Getting Started with Processing is not a programming textbook; as the title suggests, it will get you started. It’s for teenagers, hobbyists, grandparents, and everyone in...

    getting-started-in-technical-analysis —— 技术分析投资的基本知识(非常经典)

    技术分析投资的基本知识(非常经典)

    Introducing Erlang: Getting Started in Functional Programming

    Introducing Erlang: Getting Started in Functional Programming by Simon St. Laurent English | 6 Mar. 2017 | ASIN: B06XHSP5SH | 212 Pages | AZW3 | 1.85 MB If you’re new to Erlang, its functional style...

    Getting+Real中文版

    《Getting Real中文版》:构建高效、精炼的Web应用之道 在当今互联网时代,Web应用的构建不再遵循传统的冗长、复杂流程。《Getting Real中文版》提出了一个全新的视角和方法论,旨在帮助开发者和团队以更小的规模、...

    Getting Started with the Graph Template Language in SAS

    在本文中,我们提到了书籍《Getting Started with the Graph Template Language in SAS®: Examples, Tips, and Techniques for Creating Custom Graphs》, 由Sanjay Matange编著,该书提供了丰富的示例、技巧和技巧...

    Getting Real 中文版

    ### Getting Real 中文版知识点概览 #### 一、Getting Real 概述 - **Getting Real** 是由37signals公司编写的书籍,该书详细介绍了37signals公司的业务哲学、设计理念、编程方法以及市场营销策略等内容。37...

    Online Continual Learning in Image Classification An

    在线持续学习(Online Continual Learning)在图像分类领域中,主要研究如何从一个连续的数据流和任务中学习图像分类,其中任务可能包括不同的类别(类增量)或数据非平稳性(域增量)。这种学习方式的关键挑战之一...

    Getting an Information Security Job For Dummies

    Packed with the latest and most effective strategies for landing a lucrative job in this popular and quickly-growing field, Getting an Information Security Job For Dummies provides no-nonsense ...

    Getting to Yes

    Negotiation book on getting to yes, harvard professors talk about BATNA, when you should exit a negotiation and how to get better outcomes for both parties in a negotiation

Global site tag (gtag.js) - Google Analytics