uva 216 Getting in Line

timing window file中的S L T指什么意思 我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧? 阅读详情

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


题目描述:


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.


题意:

给出一系列坐标位置的点,让你用一条线把他们都串起来,然后问怎样串,这条线的总长度最小。


题解:

一、利用stl中的next_permutation来模拟全排列的枚举,算出最小的情况。

二、利用暴力DFS把最短路径搜索出来。

开始做这题的时候有点过于注意那个2decimal了 ,结果看第一个样例数据的各个子段相加并不等于总长度,结果纠结了半天。。。。还因为double的精度问题wa了3次,现在搞明白就是那个总距离是各段还没有精确2位数的子段的double相加,而不是精确过后相加的。这里有可能会让某些人理解错题目的意思。


为什么不能用最小生成树,想了下,感觉是这样: 因为这个问题情境只能是串起来的点,就是说不是只要联通就行了,而是要一个接一个地串起来。讲具体点就是,这个问题情境的点的度数 只能是1 或者2 不可能有3 4 这种度数,而一般的最小生成树恰好是允许的,我想应该有特殊的最小生成树算法允许这样。。。。。


代码:

next_permutation:

/*

topcoder codeforce acm

uva 10474 - Where is the Marble?

2 decimal , the problem do not means  2 decimal in every pairs path length in the sum of the MinSum
so the MinSum is added in every not 2 decimal double datas.



*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#define INF 99999999
using namespace std;


typedef struct Points
{
	int x;
	int y;
	Points()
	{
		x=0;
		y=0;
	}
}Points,*PointsLink;

Points List[10+5];
int ListLink[10+5];//the index address or index pointers to the List,that is to say the ListLink's value is  List's index
int N=0;// the number of the point
double MinSum=INF;
double Sum=0;

int SaveLink[10+5];
double PathLenth[10+5];//the length is the SaveLink's  divide 1

const char * OutputStr="Cable requirement to connect";
const char * Asterisks="**********************************************************";
int cases=0;

/*get the distance between the two points*/
double Distance(Points A,Points B)
{
	double dis=0;
	double X=abs(A.x-B.x);
	double Y=abs(A.y-B.y);
	dis=sqrt(X*X+Y*Y);
	dis=((int)(dis*100+0.5))/100.0+16;//2 decimal
	return(dis);//X=7 and Y=8 the double dis 2 decimal have a bug about it , you kan test it on yourself
}

/*calculate the sum path length*/
int CalcLenth(Points List[],int ListLink[])
{
	int i=0;
	for(i=0;i<=N-2;i++)
	{
		Sum+=Distance(List[ListLink[i]],List[ListLink[i+1]]);
	}
	Sum=((int)(Sum*100+0.5))/100.0;
	return(0);
}

/*get the distance between the two points*/
//double Distance1(Points A,Points B)
//{
//	double dis=0;
//	double X=abs(A.x-B.x);
//	double Y=abs(A.y-B.y);
//	dis=sqrt(X*X+Y*Y);
//	//dis=((int)(dis*100+0.5))/100.0+16;//2 decimal
//	dis+=16;
//	return(dis);//X=7 and Y=8 the double dis 2 decimal have a bug about it , you kan test it on yourself
//}

double Distance1(Points A,Points B)
{
	return(sqrt(((double)A.x-(double)B.x)*((double)A.x-(double)B.x)+((double)A.y-(double)B.y)*((double)A.y-(double)B.y))+16);
}

/*calculate the sum path length*/
int CalcLenth1(Points List[],int ListLink[])
{
	int i=0;
	for(i=0;i<=N-2;i++)
	{
		Sum+=Distance1(List[ListLink[i]],List[ListLink[i+1]]);
	}
	//Sum=((int)(Sum*100+0.5))/100.0;
	return(0);
}

/*copy the linkpath*/
int LinkCpy(int SaveLink[],int ListLink[])
{
	int i=0;
	for(i=0;i<=N-1;i++)
	{
		SaveLink[i]=ListLink[i];
	}
	return(0);
}

/*save the many two pairs path*/
int SavePathLenth(int SaveLink[])
{
	int i=0;
	for(i=0;i<=N-2;i++)
	{
		PathLenth[i]=Distance(List[SaveLink[i]],List[SaveLink[i+1]]);
	}
	return(0);
}

/*save the many two pairs path*/
int SavePathLenth1(int SaveLink[])
{
	int i=0;
	for(i=0;i<=N-2;i++)
	{
		PathLenth[i]=Distance1(List[SaveLink[i]],List[SaveLink[i+1]]);
	}
	return(0);
}

/*for test*/
int test()
{
	return(0);
}

/*main process*/
int MainProc()
{
	while(scanf("%d",&N)!=EOF&&N>0)
	{
		int i=0;
		cases++;
		MinSum=INF;
		for(i=0;i<=N-1;i++)
		{
			scanf("%d%d",&List[i].x,&List[i].y);
			ListLink[i]=i;//initialize the pointer array
		}
		do
		{
			if(ListLink[0]==2&&ListLink[1]==1&&ListLink[2]==4&&ListLink[3]==3&&ListLink[4]==0)
			{
				int ks=1;
			}
			Sum=0;
			//CalcLenth(List,ListLink);//calculate the sum path length
			CalcLenth1(List,ListLink);//calculate the sum path length
			if(Sum<MinSum)
			{
				MinSum=Sum;
				LinkCpy(SaveLink,ListLink);
				//SavePathLenth(SaveLink);
				SavePathLenth1(SaveLink);
			}
		}while(next_permutation(ListLink,ListLink+N));
		printf("%s\n",Asterisks);
		printf("Network #%d\n",cases);
		for(i=0;i<=N-2;i++)
		{
			printf("%s",OutputStr);
			printf(" (%d,%d) to (%d,%d) is %.2lf feet.\n",List[SaveLink[i]].x,List[SaveLink[i]].y,List[SaveLink[i+1]].x,List[SaveLink[i+1]].y,PathLenth[i]);
		}
		printf("Number of feet of cable required is %.2lf.\n",MinSum);
	}
	return(0);
}

int main()
{
	MainProc();
	return(0);
}




DFS:

/*

topcoder codeforce acm

uva 10474 - Where is the Marble?

not next_permutation  we use brute  DFS with per point as the start point and then DFS


think about that why not use Minimum spanning tree?????????????????

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#define INF 99999999
using namespace std;

typedef struct Points
{
	double x;
	double y;
}Points,*PointsLink;

Points List[10+5];
int Link[10+5]={0};//the n-1 link path
int N=0;
bool Vis[10+5]={false};//true means that we have used the point
int MinLink[10+5]={0};//n-1 link path
double MinSum=INF;
double Sum=0;
int cases=0;

/*get distance between two points*/
double Dis(Points a,Points b)
{
	return(sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))+16);
}

/*DFS the graph*/
int DFS(int cur,int cnt)
{
	if(cnt==N)
	{
		Link[cnt-1]=cur;
		if(Sum<MinSum)
		{
			MinSum=Sum;
			memcpy(MinLink,Link,sizeof(Link));
		}
		return(0);
	}
	else
	{
		if(Sum>=MinSum)
		{
			return(0);
		}
		int i=0;
		for(i=0;i<=N-1;i++)
		{
			if(!Vis[i])
			{
				Vis[i]=true;
				Link[cnt-1]=cur;//recursive's structure is very important, good knowledge of it's level structure
				Sum+=Dis(List[cur],List[i]);
				DFS(i,cnt+1);
				Vis[i]=false;
				//Link
				Sum-=Dis(List[cur],List[i]);
			}
		}
	}
	return(0);
}

/*for test*/
int test()
{
	return(0);
}

/*main process*/
int MainProc()
{
	while(scanf("%d",&N)!=EOF&&N>0)
	{
		int i=0;
		cases++;
		for(i=0;i<=N-1;i++)
		{
			scanf("%lf%lf",&List[i].x,&List[i].y);
		}
		MinSum=INF;
		for(i=0;i<=N-1;i++)//let the List[i] as the start point
		{
			memset(Vis,false,sizeof(Vis));
			Sum=0;
			Vis[i]=true;
			DFS(i,1);
		}
		printf("**********************************************************\n");
		printf("Network #%d\n",cases);
		for(i=0;i<=N-2;i++)
		{
			printf("Cable requirement to connect (%.0lf,%.0lf) to (%.0lf,%.0lf) is %.2lf feet.\n",List[MinLink[i]].x,List[MinLink[i]].y,List[MinLink[i+1]].x,List[MinLink[i+1]].y,Dis(List[MinLink[i]],List[MinLink[i+1]]));
		}
		printf("Number of feet of cable required is %.2lf.\n",MinSum);
	}
	return(0);
}

int main()
{
	MainProc();
	return(0);
}











科研问答:医药类本科生(临床、中医、中药与护理)本科阶段是否需要开展科研学习? 医药类本科生(临床、中医、中药与护理)本科阶段是否需要开展科研学习? 阅读详情

相关推荐

手把手教你为STM32设备生成自定义MAC地址(基于芯片ID的5种算法对比)

本文详细介绍了基于STM32芯片ID生成自定义MAC地址的5种算法,包括直接截取、异或折叠、CRC32哈希、自定义混合运算及SHA-256摘要。通过对比分析各算法的计算复杂度、唯一性及适用场景,帮助嵌入式开发者根据项目规模与资源,选择最合适的方案,确保设备MAC地址的唯一性与合规性。

weixin_29170327的博客 527

UVA 216 Getting in Line

UVA 216 Getting in Line 深搜回溯,找出最短的路径。。搜索过程中保存下路径。最后输出。。 其实也可以用全排列的next_permutation 暴力找出最短。。 #include #include #include int n; double x[10], y[10]; int vis[10]; int lu[10]; int minlu[10];

Remilia's 1186

深入解析PCIe Retimer:工作原理与典型应用场景

本文深入解析了PCIe Retimer的工作原理与核心价值。作为高速信号的中继再生器,Retimer通过时钟数据恢复和信号再生技术,有效解决长距离或复杂通道中的信号衰减与完整性问题,从而极大地扩展了PCIe链路的有效传输距离。文章详细阐述了其内部架构、自动与手动两种工作模式,并重点介绍了在长距离板间互连、复杂背板设计等典型应用场景中的关键作用。

weixin_29195615的博客 506

UVa216 Getting in Line

给你几个点,要求一条线长度最短,数据量较小,直接暴力了,next_permutation()遍历所有可能。#include #include #include #include #include using namespace std; const int N = 10; const double INF = 1<<29; int number; //例子次序 int test; /

江枭的专栏 561

SAP公司间外协业务流程以及配置说明

SAP公司间外协业务流程以及配置说明

竹大的博客 7169

解决RocketMQ中@Bean配置DefaultMQProducer时的MQClientException问题

本文针对Spring Boot整合RocketMQ时,因在@Bean配置中手动调用DefaultMQProducer的start()方法而引发的MQClientException问题,深入剖析了其根本原因在于生命周期管理冲突。文章提供了简洁有效的解决方案——移除手动启动,让Spring容器自动管理,并给出了完整的配置示例与生产环境最佳实践。

rnn9storyteller的博客 688

UVA 216 Getting in Line

UVA-216题意:给出若干个点,只能用一条线连接,求最小费用(?反正是距离+16.0)。 解题思路:深搜暴力枚举第i个连接的点,计算下总和。注意下输出。漏了一个 . 我WA了2次。/************************************************************************* > File Name: UVA-216.cpp >

QzNarsh的博客 449

别再只盯着报点率了:聊聊电容触摸屏算法里那些不为人知的‘软实力’

本文深入探讨了电容触摸屏算法中常被忽视的‘软实力’,包括环境噪声干扰、信号衰减补偿和多物理场耦合等核心挑战。通过对比消费电子与工业场景的算法需求,揭示了触摸算法在不同应用中的技术分野。文章还提供了实用的算法评估方法和前沿发展趋势,帮助开发者全面了解电容触摸屏技术的隐秘战场。

weixin_42524276的博客 389

UVA 216(Getting in Line)回溯

题目链接:https://vjudge.net/problem/UVA-216题意:给出n个点,求n-1条路连接所有点,路径之和最短注意:要在每条路的距离加上16(题目说的),刚开始想到的是用回溯,毕竟现在在做这个专题,要用一个数组记录路径!回溯的代码玄学WA,找不到原因,求大神指教#include&lt;bits/stdc++.h&gt; #define LL long long #define...

Eknight123的博客 570

CentOS7 内核升级

可在http://ftp.sjtu.edu.cn/sites/elrepo.org/linux/kernel/el7/x86_64/RPMS选择对应架构平台不受影响的内核版本文件下载,然后离线安装,也可通过yum下载包后进行安装。通过 #uname -sr ,可以看到当前的内核版本 Linux 3.10.0-1160.el7.x86_64。首先找台能访问互联网的机器,通过联网的机器进行内核包下载。lt:longterm的缩写:长期维护版;ml:mainline的缩写:最新稳定版;

Jian Sun_的博客 601

飞牛NAS打造成童年经典游戏机

这篇文章介绍如何使用飞牛NAS搭建经典游戏模拟器。首先需开启Docker并下载RetroArch-web镜像,随后创建容器并设置端口映射。启动后上传游戏ROM文件(如NES格式),在模拟器中加载即可游玩。文章以修改版坦克大战为例,详细说明按键配置方法。同时还提到公网访问需配置DDNS或使用ZeroTier内网穿透。通过这套方案,用户可以在NAS上重温红白机等经典游戏体验。

dgnankai的专栏 885

Getting in Line UVA, 216

#include #include #include #include #include using namespace std; struct Node {     int x;     int y; }node[10]; struct Ans {     double goal;     int i; }ans[41000]; //i为全排列中的编号,goal为得

ICDI的博客 393

微信小程序 - 实现客户支付订单连接小票打印机自动打印订单小票功能,微信小程序如何对接小票打印机自动打印小票,小程序收到新订单的时候可以联动商家打印出小票/单据/标签/发货单/电子面单等(详细教程)

微信小程序调用小票打印机,小程序接单后小票机自动打印订单,微信小程序云开发订单微信支付与小票和标签打印的完整高效流程,如何实现微信订单连接小票打印机并自动接单打印,微信小程序连接打印机打印单据小票打印,微信小程序云开发调用小票打印机实现方案,微信小程序怎么配置小票打印机,微信支付电子小票,外卖小程序实战-接单后小票机自动打印订单,微信小程序连接蓝牙打印小票,打印购物小票,调用小票打印机打印小票,微信订单如何连接小票打印机并实现自动接单打印,实现微信订单连接小票打印机并自动接单打印,微信小程序小票打印机打印教

🏆 前端领域优质创作者 1万+

UVa 216 Getting in Line

题目要求在平面上给定n个点(2≤n≤8),将它们连成一条链,使得总电缆长度最小。每段电缆长度为两点欧氏距离加16英尺。输入包含多个数据集,每个数据集给出n个点的坐标。输出要求:对于每个数据集,输出网络编号、最优连接顺序下各段电缆长度及总长度,不同数据集间用"*"分隔。解题思路是枚举所有排列组合,计算每种排列的总长度,取最小值。由于n≤8,全排列枚举可行。代码实现了预处理点对距离、枚举全排列、计算最优解并按要求格式化输出的完整流程。

寂静山林 91

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Uva 216 Getting in Line

枚举给定点的坐标间距离和的最小值~~数据量小,暴力枚举即可~~ #include #include #include #include #include #include using namespace std; typedef struct _Poi

Xiao_Peng 344

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

uva216 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 t

王旻宇 540

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

UVA 216 - Getting in Line

#include #include #include using namespace std; int main() { int num, arr[10][2], brr[10], order[10], count = 0; while (scanf("%d", &num), num) { double min = 1000000; for (int i = 0; i < nu

Bingo 1691

Uva216—— Getting in Line

题目的意思求n个点连起来的最短距离。n最大为8,直接枚举排列的所以情况,然后求最短的距离,最短距离等于连线上的每两个点的距离加上16的和. 点的排序可以多种情况,只要找到最短距离就行。枚举排列用next_permutation函数。 代码: #include #include #include #include using namespace std; int n,

XD 500

UVA 216-Getting in Line

UVA 216-Getting in Line题目大意:给出坐标,求出全部连起来的最短路,每个连线长度要另外加16 解题思路:可以列子集,也可以回溯,子集因为这题情况少可以用,回溯则是比较通用,一下是用回溯的代码#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> #include <string

ZeroLH00的博客 408

uva216 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 exact

keshuqi的博客 375

UVA 216 Getting in Line dfs

点击打开题目链接 平面上有一些点,求一笔将所有点连起来的最短距离(注意每次连都要加上16) 由于最多只有8个点,暴力全排列即可 方法一:dfs #include #include #include #include #include using namespace std; const int INF = 0xfffffff; const int MAXN = 10; in

Houheshuai的专栏 510

[枚举]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 tha

九问的烦恼 1135

uva216 - Getting in Line

一道暴力的题目。。枚举出来就好了。。 一开始用自己写全排列都是WA 。。 可能是字典序什么的问题。。然后用了STL的全排列 题目 Getting in Line Computer networking requires that the computers in the network be linked. This proble...

aiezhen1717的博客 138
上一篇: uva 110 Meta-Loopless Sorts
下一篇: uva 639 Don't Get Rooked
hackerwin7
博客等级 码龄14年 115粉丝 144原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值