题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短。

析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生成树,写就OK了。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int p, q, n;
int x[55], y[55];
int f[55];
int Find(int x){ return x == f[x] ? x : f[x] = Find(f[x]); }
struct node{
int u, v;
double d;
bool operator < (const node &p) const{
return d < p.d;
}
};
node a[55*55]; double dist(int i, int j){
return 1.0*(x[i] - x[j]) * (x[i] - x[j]) + 1.0*(y[i] - y[j]) * (y[i] - y[j]);
} int main(){
while(scanf("%d", &n) == 1 && n){
scanf("%d %d", &p, &q);
if(p > q) swap(p, q);
for(int i = 1; i <= n; ++i){
scanf("%d %d", &x[i], &y[i]);
f[i] = i;
}
int cnt = 0;
double ans = 0.0;
for(int i = 1; i <= n; ++i)
for(int j = i+1; j <= n; ++j){
//if(i == j) continue;
if(i == p && j == q){
int x = Find(p);
int y = Find(q);
f[y] = x;
ans += sqrt(dist(p, q));
continue;
}
a[cnt].u = i;
a[cnt].v = j;
a[cnt++].d = sqrt(dist(i, j));
}
sort(a, a+cnt); for(int i = 0; i < cnt; ++i){
int x = Find(a[i].u);
int y = Find(a[i].v);
if(x != y){
f[y] = x;
ans += a[i].d;
}
}
printf("%.2lf\n", ans);
}
return 0;
}

HDU 4463 Outlets (最小生成树)的更多相关文章

  1. HDU—4463 Outlets 最小生成树

    In China, foreign brand commodities are often much more expensive than abroad. The main reason is th ...

  2. hdu 4463 Outlets(最小生成树)

    Outlets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submi ...

  3. 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)

    以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...

  4. HDU 4463 Outlets(最小生成树给坐标)

    Problem Description In China, foreign brand commodities are often much more expensive than abroad. T ...

  5. HDU 4463 Outlets 【最小生成树】

    <题目链接> 题目大意: 给你一些点的坐标,要求你将这些点全部连起来,但是必须要包含某一条特殊的边,问你连起这些点的总最短距离是多少. 解题分析: 因为一定要包含那条边,我们就记录下那条边 ...

  6. hdu 4463 Outlets(最小生成树)

    题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 pri ...

  7. hdu 4463 Outlets

    #include<bits/stdc++.h> using namespace std; double x[100+5],y[100+5]; double e[100+5][100+5]; ...

  8. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  9. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

随机推荐

  1. parseInt和valueOf

    .parseInt和valueOf.split static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf( ...

  2. 原创-兼容IE8的placeholder

    !function (o) { o.fn.extend({ PlaceHolder: function () { var _isEmpty = function (val) { return (val ...

  3. UVa 12661 (单源最短路) Funny Car Racing

    题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...

  4. UVa 12186 Another Crisis

    题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...

  5. Codeforces Round #272 (Div. 2)

    A. Dreamoon and Stairs 题意:给出n层楼梯,m,一次能够上1层或者2层楼梯,问在所有的上楼需要的步数中是否存在m的倍数 找出范围,即为最大步数为n(一次上一级),最小步数为n/2 ...

  6. facebook海量图片存储系统与淘宝TFS系统比较

    本篇论文的原文可谓通俗易懂.行云流水.结构清晰.图文并茂……正如作者所说的——"替换Facebook的图片存储系统就像高速公路上给汽车换轮子,我们无法去追求完美的设计……我们花费了很多的注意 ...

  7. 【C#学习笔记】窗口隐藏、最小化、最大化、正常化

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. <九>面向对象分析之UML核心元素之设计类,类,属性,方法,可见性

    设计类

  9. spring aop expression简单说明

    <aop:config> <aop:pointcut id="userDAO" expression="execution(public * cn.da ...

  10. ECshop 二次开发模板教程1

    本教程适用于了解 ECshop 和 ECshop模板DIY 以及它们的日常使用,在查看前阁下需要至少会使用一种编辑器(exp:Dreamweaver, editplus, emacs, vi, ee  ...