题意:给出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. gpg 的使用

    GPG入门教程 GpG使用指南 1. 安装 源码编译安装:源码下载地址 ./configure make make install 直接安装编译好的二进制文件 # Debian / Ubuntu 环境 ...

  2. 关于linux下QIODevice类进行读取的几个方法的理解

    Qt中对读写设备的支持力度很大,其都继承与QIODevice类,其中有几个方法是非常值得注意的,不管是在用原始的serial port进行通信还是使用网络的TCP/IP 或者UDP或者HTTP等协议时 ...

  3. MFC补码原码反码转换工具

    /*_TCHAR str[100] = { 0 }; wsprintf(str, _T("%d"),num);*/ ; CString str; m_edit1.GetWindow ...

  4. calender怎么获取每周的周日(给每周的周日特定时间点设置定时)

    获取每周的周日 //如果是周日,特殊处理.老外的周日-周六为一周 calendarTemp.add(Calendar.WEEK_OF_MONTH,1); calendarTemp.set(Calend ...

  5. javascript 异或运算符实现简单的密码加密功能

    写在前面的 当我们需要在数据库中存储用户的密码时,当然是不能明文存储的. 我们就是介绍一下用^运算符来实现简单的密码加密以及解密功能 上代码 首先,回顾一下基础知识. String.fromCharc ...

  6. 模块 –SYS

    模块 –SYS os模块是跟操作系统的交互 sys是跟python解释器的交互 sys.argv 命令行参数List,第一个元素是程序本身路径 返回一个列表 In [218]: sys.argv Ou ...

  7. selenium自动化(一).........................................搭建环境

    一  环境搭建 安装python(建议使用py3) py2和py3在语法上会有一定的差别 第三方插件逐步转向py3,很多py2的插件已经停止维护 本教程的所有代码基于py3 安装selenium插件 ...

  8. POJ 3093 Margaritas on the River Walk(背包)

    题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...

  9. Python: 自定义类对象序列化为Json串

    之前已经实现了Python: Json串反序列化为自定义类对象,这次来实现了Json的序列化. 测试代码和结果如下: import Json.JsonTool class Score: math = ...

  10. caioj 1161 欧拉函数3:可见点数

    (x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...