UVa 216 Getting in Line【枚举排列】
题意:给出n个点的坐标,(2<=n<=8),现在要使得这n个点连通,问最小的距离的和
因为n很小,所以可以直接枚举这n个数的排列,算每一个排列的距离的和,
保留下距离和最小的那个排列就可以了(这个地方和带宽那题有点像,用memcpy将整个排列保留下来)
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int p[maxn],bestp[maxn]; struct node{
int x,y;
} a[maxn]; double dis(int x1,int y1,int x2,int y2){
return sqrt((double)(x2 - x1) * (x2 - x1) +(double) (y2 - y1) * (y2 - y1));
} int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
double ans,minn=;
int kase=;
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++) {
cin>>a[i].x>>a[i].y;
if(i!=) minn+=dis(a[i].x,a[i].y,a[i-].x,a[i-].y);
} for(int i=;i<n;i++) p[i]=i;
memcpy(bestp,p,sizeof(p)); while(next_permutation(p,p+n)){
ans=;
for(int i=;i<n;i++)
ans+=dis(a[p[i]].x,a[p[i]].y,a[p[i-]].x,a[p[i-]].y); // printf("ans=%lf\n",ans); if(ans<minn){
minn=ans;
memcpy(bestp,p,sizeof(p));
}
} printf("**********************************************************\n");
printf("Network #%d\n",++kase);
for(int i = ; i < n; ++i)
printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n",(int)a[bestp[i-]].x,
(int)a[bestp[i-]].y,(int)a[bestp[i]].x,(int)a[bestp[i]].y,16.0 + dis(a[bestp[i-]].x,a[bestp[i-]].y,a[bestp[i]].x,a[bestp[i]].y));
printf("Number of feet of cable required is %.2lf.\n",minn + (n - ) * 16.0);
}
return ;
}
UVa 216 Getting in Line【枚举排列】的更多相关文章
- UVA 216 - Getting in Line
216 - Getting in Line Computer networking requires that the computers in the network be linked. This ...
- uva 216 Getting in Line 最短路,全排列暴力做法
题目给出离散的点,要求求出一笔把所有点都连上的最短路径. 最多才8个点,果断用暴力求. 用next_permutation举出全排列,计算出路程,记录最短路径. 这题也可以用dfs回溯暴力,但是用最小 ...
- UVA - 10098 - Generating Fast (枚举排列)
思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- 蓝桥杯 2014本科C++ B组 六角填数 枚举排列
标题:六角填数 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填 ...
- poj 2585 Window Pains 暴力枚举排列
题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ...
- Getting in Line UVA 216
Getting in Line Computer networking requires that the computers in the network be linked. This pro ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
随机推荐
- 固定执行计划-使用coe_xfr_sql_profile
一.历史执行计划固定 历史的执行计划找到一个合理的执行计划进行绑定 1. 存在多个执行计划的语句,按照索引是比较合适的,FULL SCAN不合适 select * from scott.emp whe ...
- P3809 【模版】后缀排序
题目背景 这是一道模版题. 题目描述 读入一个长度为 nn 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编 ...
- 解决win8.1下sql配置iis的问题
在配置iis8.5时,ISAPI和CGI限制中没有ASP.NET v4.0.30319, 所以要注册.net 4.0 注册方法为在“运行”中输入cmd,然后在命令行中输入: C:\WINDOWS\Mi ...
- size_type类型
由string类类型和vector类类型定义的类型,用以保存任意string对象或vector对象的长度,标准库类型将size_type定义为unsigned类型 string::size_type它 ...
- select选中值,传this
<select onChange = "a(this)"></select> function a(obj){ $(obj).find("opti ...
- 简洁的MVC思想框架——Nancy(Session的使用)
前文提到关于Nancy中GET和POST以及外部引用图片,css和JS的文件等操作.今天所讲的是Nancy关于Session相关操作. Session作为web开发中极其重要的一部分,而Nancy中S ...
- Base64编码字符串时数据量明显变大
那就是当把byte[]通过Convert.ToBase64String转换成Base64编码字符串时数据量明显变大 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需 ...
- UNIX系统高级编程——第六章-系统数据文件和信息-总结
口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...
- jQuery第三课 点击按钮 弹出层div效果
jQuery 事件方法 事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. 触发实例: $("button#demo").click() 上面的例子将触发 id= ...
- Go语言的前景分析
本文为原创文章,转载注明出处,asdfasdfasdf 欢迎扫码关注公众号flysnow_org或者网站http://www.flysnow.org/,第一时间看后续精彩文章.觉得好的话,顺手分享到朋 ...