In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup. 
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores. 

InputThere are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0. 
OutputFor each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point. 
Sample Input

4
2 3
0 0
1 0
0 -1
1 -1
0

Sample Output

3.41

题意:给你n个点,最小的价值使得所有的点连通,但是p,q一定是直连的。
这是一道比较的模板的最小生成树的题,但是要保证有一条边一定在这颗树内,我们可以使用Kruskal算法的时候,直接把ans先设置为p,q之间距离的值,然后在加边的时候把值设置为0,那么根据Kruskal算法的思想,
这个边最小肯定是最先加进来了,那么其他的就和其他的没有区别了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=;
int n,p,q;
int cnt;
struct Point
{
int x,y;
}point[maxn];
struct Node
{
int from,to;
double value;
}node[maxn*maxn];
int fa[maxn];
bool cmp(Node a,Node b)
{
return a.value<b.value;
}
void init()
{
for(int i=;i<maxn;i++)
fa[i]=i;
}
int findd(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=findd(fa[x]);
}
double getdist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Kruskal()
{
double ans=getdist(point[p],point[q]);
for(int i=;i<=cnt;i++)
{
int fx=findd(node[i].from);
int fy=findd(node[i].to);
if(fx!=fy)
{
ans+=node[i].value;
fa[fx]=fy;
}
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
scanf("%d %d",&p,&q);
for(int i=;i<=n;i++)
scanf("%d %d",&point[i].x,&point[i].y);
cnt=;
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
cnt++;
node[cnt].from=i;node[cnt].to=j;
node[cnt].value=getdist(point[i],point[j]);
if((i==p&&j==q)||(i==q&&j==p))
node[cnt].value=;
}
}
init();
sort(node+,node+cnt+,cmp);
double sum=Kruskal();
printf("%.2f\n",sum);
}
return ;
}

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

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

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

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

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

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

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

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

    题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短. 析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生 ...

  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. linq to EF分组查询 group by 的使用

    第一种:查询表达式语法: IQueryable<EnrollmentDateGroup> data = from student in db.Students group student ...

  2. html title属性

    <table class="table table-hover table-striped"> @foreach (var article in Model) { &l ...

  3. hdu1530 求最大团

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1530 求最大团裸题. 模板:maxx即为所求的最大团的值. #include<iostream& ...

  4. Ruby Encoding类

    Encoding类  内部编码  IO对象内部处理时候的编码   外部编码  IO对象对外输出的时候的编码  输入  外部字符与自己的外部编码对比(没设定的默认                     ...

  5. ODBC数据管理器 SqlServer实时数据同步到MySql

    ---安装安装mysqlconnector http://www.mysql.com/products/connector/ /* 配置mysqlconnector ODBC数据管理器->系统D ...

  6. 使用Docker Compose编排微服务

    一般微服务架构会包含若干个微服务,而每个微服务可以有多个实例,如果每个微服务都有手动启停,那么效率就很低.维护量比较大. 所以我们可以使用Docker Compose来轻松.高效地管理容器. 一.安装 ...

  7. layui配置

    layui是一个全局变量,可以在任何地方访问到 layui.config 方法主配置信息(经测试好像不能添加额外属性) layui.setter读取主配置属性 layui.extend 方法增加主配置 ...

  8. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

  9. 洛谷 P2062 分队问题

    这题太毒了....一开始就是死活想不到,结果看了很多遍题解,重新做的时候还是做不出来.. 好像有一点被错误的题解误导了? #include<cstdio> #include<algo ...

  10. Rooks LightOJ - 1005

    https://vjudge.net/problem/LightOJ-1005 题意:在n*n的矩形上放k个车,使得它们不能互相攻击,求方案数. ans[i][j]表示在i*i的矩形上放j个车的方案数 ...