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 的,那么最少要带多少,并输出一个解,如果多个解 ...
随机推荐
- apt-get &dpkg
apt-get是ubuntu常用的软件安装工具.他可以很easy的从互联网上下载软件安装包,并实现安装. apt-get比较常用的命令如下: apt-get install packagename ...
- Mysql自增字段
1.关键字 auto_increment 2.自增用法 例: CREATE TABLE animals ( id mediumint not null auto_increment, name cha ...
- sql GROUP BY 分组统计
语句1: SELECT TypeID, COUNT(*) AS [count] FROM GoodsInfo GROUP BY TypeID 得到结果 解析结果:GoodsInfo表有 4条记录, ...
- 用Java实现菱形的打印输出
import java.util.Scanner; public class dengyao2 { public dengyao2() { super(); } public static void ...
- jquery checkbox选中状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS 去字符串空格
str为要去除空格的字符串:去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/g ...
- HTML第三天学习笔记
昨天学的超链接,今天深入学习了下,发现了更多的知识点,而且关于初始新建网页时,由于是初学者,所以还是纯手写代码~ <html> <head> <title>超链接& ...
- uva193 - Graph Coloring
Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...
- 提升jQuery开发技能的教程
iPhone-like Sliding Headers Simple jQuery Spy Effect Simple use of Event Delegation Adding Keyboard ...
- AnimImageView
https://github.com/eltld/AnimImageView