uva 216 Getting in Line 最短路,全排列暴力做法
题目给出离散的点,要求求出一笔把所有点都连上的最短路径。
最多才8个点,果断用暴力求。
用next_permutation举出全排列,计算出路程,记录最短路径。
这题也可以用dfs回溯暴力,但是用最小生成树要小心一点,最小生成树求的是最小连通图,而不是连成一条,不能用Kruscal,Prim算法修改一下也可以使用,改成选点时仅考虑头尾两点即可。
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 10; int p[maxn], rec[maxn], n;
double sum, x[maxn], y[maxn]; double dis(double ax, double ay, double bx, double by) {
double dx, dy;
dx = ax - bx;
dy = ay - by;
return sqrt(dx * dx + dy * dy) + 16;
} void solve(void) {
double tmp = 0;
for (int i = 0; i < n - 1; i++)
tmp += dis(x[p[i]], y[p[i]], x[p[i + 1]], y[p[i + 1]]);
if (tmp < sum) {
sum = tmp;
for (int i = 0; i < n; i++)
rec[i] = p[i];
}
} int main() {
int cnt = 0;
while (scanf("%d", &n) && n) {
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &x[i], &y[i]);
p[i] = i;
}
sum = 0xffffff;
do {
solve();
} while (next_permutation(p, p + n));
printf("**********************************************************\n");
printf("Network #%d\n", ++cnt);
for (int i = 0; i < n - 1; i++)
printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", int(x[rec[i]]), int(y[rec[i]]), int(x[rec[i + 1]]), int(y[rec[i + 1]]), dis(x[rec[i]], y[rec[i]], x[rec[i + 1]], y[rec[i + 1]]));
printf("Number of feet of cable required is %.2lf.\n", sum);
}
return 0;
}
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【枚举排列】
题意:给出n个点的坐标,(2<=n<=8),现在要使得这n个点连通,问最小的距离的和 因为n很小,所以可以直接枚举这n个数的排列,算每一个排列的距离的和, 保留下距离和最小的那个排列就可以 ...
- Getting in Line UVA 216
Getting in Line Computer networking requires that the computers in the network be linked. This pro ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA - 1416 Warfare And Logistics (最短路)
Description The army of United Nations launched a new wave of air strikes on terroristforces. The ob ...
- UVa 1599 (字典序最小的最短路) Ideal Path
题意: 给出一个图(图中可能含平行边,可能含环),每条边有一个颜色标号.在从节点1到节点n的最短路的前提下,找到一条字典序最小的路径. 分析: 首先从节点n到节点1倒着BFS一次,算出每个节点到节点n ...
- UVA - 140 Bandwidth(带宽)(全排列)
题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...
- 【每日一题】 UVA - 1599 Ideal Path 字典序最短路
题解:给一个1e5个点2e5条边,每个边有一个值,让你输出一条从1到n边的路径使得:条数最短的前提下字典序最小. 题解:bfs一次找最短路(因为权值都是1,不用dijkstra),再bfs一次存一下路 ...
- UVa 10537 The Toll! Revisited (最短路)
题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...
随机推荐
- 选择一本C++教材
从上周开始写如何使用C++编程以后,我发现这不是一个容易的题目.因此,我认真的看了一下C++相关的材料,发现现在为止,比较好的材料还是这些: 初学者: Accelerated C++,这是一本学习起来 ...
- iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid
iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid 在执行iisapp.vbs时,可能会提示如下错误:Windows Script Component - f ...
- UI:UITableView表视图
表视图 UITableView,iOS中最重要的视图,随处可⻅见. 表视图通常⽤用来管理⼀一组具有相同数据结构的数据. UITableView继承⾃自UIScrollView,所以可以滚动,表视图的每 ...
- CountDownLatch和CyclicBarrier的区别
[CountDownLatch.CyclicBarrier和Semaphore]http://www.cnblogs.com/dolphin0520/p/3920397.html [CountDo ...
- Json.Net学习笔记
http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http ...
- maven中如何打包源代码
http://yanghaoyuan.iteye.com/blog/2032406 使用Maven对项目部署太方便了,特别是依赖关系,最近学习使用Maven,为了备忘和技术的分享特意注册个账号记录到博 ...
- canvas绘制直线和多边形基本操作
<!DOCTYPE HTML> <html lang="en"> <body> <canvas id="canvas" ...
- 【M35】让自己习惯于标准C++语言
1.最近一些年C++语言增加的特性有: a.RTTI,namespace,bool,关键字mutable和explicit,enums,以及const static int可以直接初始化. b.扩充了 ...
- 【M11】禁止异常流出析构方法之外
1.在两种情况下,调用析构方法:a.在正常状态下被销毁,栈上的对象离开作用域或者堆上的对象执行delete:b.抛出异常,堆栈回滚,栈上已经构造好的对象,也就是抛出异常之前的代码,自动调用析构方法.注 ...
- cdoj 25 点球大战(penalty) 模拟题
点球大战(penalty) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/2 ...