题意:给出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【枚举排列】的更多相关文章

  1. UVA 216 - Getting in Line

    216 - Getting in Line Computer networking requires that the computers in the network be linked. This ...

  2. uva 216 Getting in Line 最短路,全排列暴力做法

    题目给出离散的点,要求求出一笔把所有点都连上的最短路径. 最多才8个点,果断用暴力求. 用next_permutation举出全排列,计算出路程,记录最短路径. 这题也可以用dfs回溯暴力,但是用最小 ...

  3. UVA - 10098 - Generating Fast (枚举排列)

    思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...

  4. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  5. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  6. 蓝桥杯 2014本科C++ B组 六角填数 枚举排列

    标题:六角填数 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填 ...

  7. poj 2585 Window Pains 暴力枚举排列

    题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ...

  8. Getting in Line UVA 216

     Getting in Line  Computer networking requires that the computers in the network be linked. This pro ...

  9. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

随机推荐

  1. JavaScript中Math常用方法

    title: JavaScript中Math常用方法 toc: false date: 2018-10-13 12:19:31 Math.E --2.718281828459045,算数常量e Mat ...

  2. Kettle和ETL的基本构成

    不多说,直接上干货! 这里,我说的通俗易懂点,好方便大家的理解. ETL解决方案就像业务流程一样,具有输入.输出,以及一个或多个工作环节,处理步骤.同样的,这些步骤也具有输入和输出,并可以执行将一个输 ...

  3. AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe

    首先说明,以前我用eventBus的jar包写得项目demo,前几天就写了一个EventBus的实例,这次我没用jar包,直接用gradle引用的,可是demo写完了,报错: its super cl ...

  4. html5plus 从相册选择图片后获取图片的大小

    plus.gallery.pick(function (filePath) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) ...

  5. php获取js里的参数

    php获取js的值有如下方式: 1.php echo出js文件得到返回值,在gamemap.js文件中输出参数. echo '<script type="text/javascript ...

  6. 优动漫PAINT绘制紫阳花教程

    紫阳花是插画.漫画很常见的绘画画材.这个教程非常好懂.而且很方便就能绘制出漂亮的效果.因为这种花一个月内能变化三种颜色,故而人们赋予它的花语是善变.背叛. 教程是简单,呃.... 没有优动漫PAINT ...

  7. 玩转HTML5移动页面(优化篇)

    标签:h5 页面优化收藏 热门分享 网页设计师必备的 酷站收藏网站 2013年不容错过的app ui素材 40个漂亮的扁平化网页设计欣赏 国内网页设计公司网站欣赏 55套网页设计常用的psd格式UI ...

  8. 当relative遇上z-index,阻断事件捕获

    今天在调试前端页面的时候,发现addflowrank这个元素的click事件不能触发了.下图是它的元素结构. 用开发者工具试图定位这个元素,看看它到底怎么了.发现:无论我怎么用光标定位这个元素都定位不 ...

  9. springboot --> web 应用开发-CORS 支持

    一.Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 CORS 与 JSONP 相比 1. JSONP 只能实现 GET 请求,而 CORS 支持所有类型的 HTT ...

  10. BNUOJ 34990 Justice String

    Justice String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...