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函数真是好用. 比较蛋疼的就是题目的输入了.. ...
随机推荐
- ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用!因此,ES transport client可以同步调用也可以异步(不过底层的socket必然是异步实现)
ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用! 因此,ES tra ...
- xmanager使用
Xmanager全称Netsarang Xmanager,是国外一套非常优秀的远程监控软件.在UNIX/Linux和Windows网络环境中,Xmanager是最好的连通解决方案.我推 荐大家下载En ...
- CENTOS修改操作系统字符集
[root@localhost sysconfig]# cd /etc/sysconfig/ [root@localhost sysconfig]# vi i18n 修改并保存退出(:wq) LANG ...
- POJ 1945 暴搜+打表 (Or 暴搜+判重)
思路: 呃呃 暴搜+打表 暴搜的程序::稳稳的TLE+MLE (但是我们可以用来打表) 然后我们就可以打表过了 hiahiahia 可以证明最小的那个数不会超过200(怎么证明的我也不知道),然后就直 ...
- Android 实现下拉刷新和上拉加载更多的RECYCLERVIEW和SCROLLVIEW
PullRefreshRecyclerView.java /** * 类说明:下拉刷新上拉加载更多的RecyclerView * Author: gaobaiq * Date: 2016/5/9 18 ...
- jquery动态表格,动态添加表格行
转载收藏于:https://www.cnblogs.com/zhangqs008/archive/2013/05/09/3618459.html 效果图: Html:<html> &l ...
- STM8S103 STVD编译空间不足
关于text空间(理解为代码空间)不足问题 # 关于.bsct和.ubsct问题(着重参考http://www.waveshare.net/article/STM8-3-1-10.htm) map文件 ...
- Docker中免去sudo的设置方法
Add the docker group if it doesn't already exist: sudo groupadd docker Add the connected user " ...
- How Javascript works (Javascript工作原理) (二) 引擎,运行时,如何在 V8 引擎中书写最优代码的 5 条小技巧
个人总结: 一个Javascript引擎由一个标准解释程序,或者即时编译器来实现. 解释器(Interpreter): 解释一行,执行一行. 编译器(Compiler): 全部编译成机器码,统一执行. ...
- [arc076f]Exhausted? - 贪心
题意: 给你m个椅子可以坐人,初始坐标为正整数1~m,有n个人,每个人希望坐的位置$\leq L_i$或者$\geq R_i$,可以添加若干个椅子在任意的实数位置,求最少要添加多少椅子使得所有人都有位 ...