Uva216—— Getting in Line
2016-03-19 11:46
309 查看
题目的意思求n个点连起来的最短距离。n最大为8,直接枚举排列的所以情况,然后求最短的距离,最短距离等于连线上的每两个点的距离加上16的和.
点的排序可以多种情况,只要找到最短距离就行。枚举排列用next_permutation函数。
代码:
点的排序可以多种情况,只要找到最短距离就行。枚举排列用next_permutation函数。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; int n, data[15], x[15], y[15], ans[15]; double mindis; double get_dis(int x1, int y1, int x2, int y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) + 16.0; } void get() { double temp = 0.0; for(int i = 0; i < n - 1; i++) temp += get_dis(x[data[i]], y[data[i]], x[data[i + 1]], y[data[i + 1]]); if(temp <= mindis) { mindis = temp; for(int i = 0; i < n; i++) ans[i] = data[i]; } } int main() { // freopen("1.txt", "r", stdin); int i, t = 0; while(cin >> n) { if(!n) break; mindis = 0xfffffff; for(i = 0; i < n; i++) { cin >> x[i] >> y[i]; data[i] = i; } do { get(); }while(next_permutation(data, data + n)); printf("**********************************************************\n"); printf("Network #%d\n", ++t); for(i = 0; i < n - 1; i++) { printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", x[data[i]], y[data[i]], x[data[i + 1]], y[data[i + 1]], get_dis(x[data[i]], y[data[i]], x[data[i + 1]], y[data[i + 1]])); } printf("Number of feet of cable required is %.2lf.\n", mindis); } return 0; }
相关文章推荐
- knockoutjs十三 focus checked绑定
- C#代码覆盖率实践-vsinstr和OpenCover
- PMD-Java 代码检查工具对 error 和 warning 的配置
- Java IO--创建文件
- TMMi的评估类型
- mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息
- 读历史书看历史剧
- 深入理解Java虚拟机笔记---垃圾收集器
- mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息
- HDU 1890 Robotic Sort(splay)
- 电气配件管理系统总结
- 社会热点
- Java初级工程师面试题精选3
- 动态内存和智能指针
- java一周知识回顾
- 并查集详解
- 整理:统计学习-3 k近邻法
- myeclipse 改java文件后禁止自动重启- 热部署
- Java堆栈详解
- Linux下php安装Redis扩展