因为题目要求最多8台电脑,所以可以枚举全排列,然后依次计算距离进行比较,枚举量8!=40320并不大,但这种方法不如回溯法好,当数据再大一些枚举就显得笨拙了,所以这个题我用回溯法做的,回溯有一个好处是一边生成序列一边判断,当判断这种情况下不可能满足要求时就停止向下递归,而返回上一层调用,减少运算量。

输出的时候用到了固定小数点后几位数输出的技巧,不过我还是给忘了,翻了一下以前写的博客迅速找到了,忽然切身体会到建一个自己的技术博客是多么的有意义!哈哈!

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath> using namespace std; struct computer
{
double x,y;
};
int n,C[8],Result[8];
double Min;
computer input[8];
bool vis[8]; double caldistance(int p1,int p2)
{
return 16+sqrt((input[p1].x-input[p2].x)*(input[p1].x-input[p2].x)+(input[p1].y-input[p2].y)*(input[p1].y-input[p2].y));
}
void dfs(int cur,double sum)
{
if (cur>1)
sum=sum+caldistance(C[cur-2],C[cur-1]);
if (cur==n)
{
if (sum<Min)
{
Min=sum;
for (int i=0;i<n;i++)
Result[i]=C[i];
}
}
else if (sum>=Min) return; //已经不符合的及时返回
else
for (int i=0;i<n;i++)
{
if (!vis[i])
{
C[cur]=i;
vis[i]=1;
dfs(cur+1,sum);
vis[i]=0; //一定要复位!!!!勿忘!
}
}
}
int main()
{
int col=0;
while(cin>>n&&n)
{
col++;
Min=1500;
memset(vis,0,sizeof(vis));
for (int i=0;i<n;i++)
cin>>input[i].x>>input[i].y;
dfs(0,0);
cout<<"**********************************************************"<<endl;
cout<<"Network #"<<col<<endl;
for (int i=0;i<n-1;i++)
{
cout<<"Cable requirement to connect ("<<fixed<<setprecision(0)<<input[Result[i]].x<<","<<input[Result[i]].y<<") to ("<<input[Result[i+1]].x<<","<<input[Result[i+1]].y<<") is "<<fixed<<setprecision(2)<<caldistance(Result[i],Result[i+1])<<" feet."<<endl; }
cout<<"Number of feet of cable required is "<<fixed<<setprecision(2)<<Min<<"."<<endl;
}
return 0;
}

uva216 c++回溯法的更多相关文章

  1. 回溯法解决N皇后问题(以四皇后为例)

    以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...

  2. leetcode_401_Binary Watch_回溯法_java实现

    题目: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bot ...

  3. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  4. 实现n皇后问题(回溯法)

    /*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...

  5. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  6. HDU 2553 n皇后问题(回溯法)

     DFS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  7. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 八皇后问题-回溯法(MATLAB)

    原创文章,转载请注明:八皇后问题-回溯法(MATLAB) By Lucio.Yang 1.问题描述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后,使其不能 ...

  9. 使用回溯法求所有从n个元素中取m个元素的组合

    不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...

随机推荐

  1. Tombstone crash

    首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...

  2. Keep the Customer Satisfied

    题意: n个订单,每个订单有完成需要的天数,和限制的天数,求最多能完成多少订单 分析: 先按限制日期升序排列,若当前订单不能完成,和上面已选中的订单中需要天数中最大的比较,若比它小,则替换他. #in ...

  3. 个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑

    Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑: 此坑出现的条件: 1.RelativeLayout布局的layout_heigh ...

  4. DzzOffice添加动态壁纸例子-Bing每日壁纸

    Bing每日壁纸介绍:bing网站每天会更新一张不同的精选图片. 此压缩包内的程序,可以自动同步更新cn.bing.com网站每天更新的图片,作为dzzoffice的壁纸使用.实现自动每天更换不同的云 ...

  5. hashtable,hashMap,vector和ArrayList

    关于vector,ArrayList,hashMap和hashtable之间的区别 vector和ArrayList:  线程方面:  vector是旧的,是线程安全的  ArrayList是java ...

  6. Oauth2 接口api

    weibo:http://open.weibo.com/wiki/API weixin:http://mp.weixin.qq.com/wiki/home/index.html   qq开发平台: 1 ...

  7. sqlserver 中的GUID 全局唯一标识 -摘自网络

    --简单实用全局唯一标识 DECLARE @myid uniqueidentifierSET @myid = NEWID()PRINT 'Value of @myid is: '+ CONVERT(v ...

  8. linux 命令 之chomd

    chmod用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法.  1. 文字设定法 语法:chmo ...

  9. android sdk manager 闪退 打不开问题

    android sdk manager 闪退 打不开问题 环境 win8系统 如果访问不了  dl-ssl.google.com 网址,在C:\Windows\System32\Drivers\etc ...

  10. vb调用exe文件

    vb调用exe文件 函数:Call Shell(PathName,WindowStyle) 或 a = Shell(PathName,WindowStyle) ,不需要声明. 注解:PathName ...