Problem Description
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.
 
Input
There 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.
 
Output
For 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
 
题意:要求一棵最小生成树,要求nike店与apple店直接相连,Input给的是各点的坐标。用坐标转化出距离,即转化为最小生成树裸题。
 
 #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; int fa[]; struct node
{
int u,v;
double c;
}a[]; struct point
{
double x,y;
}Point[]; bool cmp(node b,node d)
{
return b.c<d.c;
} int find(int t)
{
return fa[t]==t ? t:fa[t]=find(fa[t]);
} double dis(point b,point d)
{
double res;
res=sqrt((b.x-d.x)*(b.x-d.x)+(b.y-d.y)*(b.y-d.y));
return res;
} int main()
{ int n,m;
double ans;
int p,q;
while(~scanf("%d",&n)&&n)
{
ans=0.0;
m=;
scanf("%d%d",&p,&q);
for(int i=;i<=n;i++)
{
scanf("%lf%lf",&Point[i].x,&Point[i].y);
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
a[m].u=i;
a[m].v=j;
a[m++].c=dis(Point[i],Point[j]);
}
sort(a,a+m,cmp);
for(int i=;i<=n;i++)
fa[i]=i;
fa[p]=q;
ans+=dis(Point[p],Point[q]);
for(int i=;i<m;i++)
{
int p1=find(a[i].u),q1=find(a[i].v);
if(p1!=q1)
{
fa[p1]=q1;
ans+=a[i].c;
}
}
printf("%.2lf\n",ans); }
return ;
}

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 (最小生成树)

    题意:给定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. SEND EMAIL SO_DOCUMENT_SEND_API1

    FUNCTION zcrm_send_email_and_attach . *"------------------------------------------------------- ...

  2. Spring ApplicationListener使用方法及问题

    使用场景 在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载.初始化缓存.特定任务的注册等等.这个时候我们就可以使用Spring提供的ApplicationListener来 ...

  3. Hadoop---hu-hadoop1: mv: cannot stat `/home/bigdata/hadoop-2.6.0/logs/hadoop-root-datanode-hu-hadoop1.out.4': No such file or directory

    hu-hadoop1: mv: cannot stat `/home/bigdata/hadoop-2.6.0/logs/hadoop-root-datanode-hu-hadoop1.out.4': ...

  4. Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql) (文档 ID 1064487.1)

    the article from :http://m.blog.itpub.net/31393455/viewspace-2130875/ Script to Collect Log File Syn ...

  5. 自签名证书说明——自签名证书的Issuer和Subject是一样的。不安全的原因是:没有得到专业SSL证书颁发的机构的技术支持?比如使用不安全的1024位非对称密钥对,有效期设置很长等

    一般的数字证书产品的主题通常含有如下字段:公用名称 (Common Name) 简称:CN 字段,对于 SSL 证书,一般为网站域名:而对于代码签名证书则为申请单位名称:而对于客户端证书则为证书申请者 ...

  6. Python3各种进制之间的转换方法

    一.2/8/10/16进制互转 1.1 2/8/10/16进制赋值 # 二进制赋值以0b打头 a = 0b1000 # 八进制赋值以0o打头,第一个是数字0第二个是字母o b = 0o1100 # 十 ...

  7. listener.ora和tnsnames.ora格式解析

    listener.ora是oracle数据库服务端的监听配置文件,包括协议.IP地址和端口等内容:tnsnames.ora是oracle数据库客户端的连接配置文件,也是对应的协议.IP地址和端口等内容 ...

  8. I/O复用(select)——回声服务器端/客户端

    一.select 使用select函数可以将多个文件描述符集中到一起统一监视,监视事件如下: 是否存在待读取数据. 是否可传输无阻塞传输数据. 是否发生异常. 将关心上述3种事件的文件描述发分别注册到 ...

  9. python 自然语言处理(三)____条件频率分布

    条件频率分布就是频率分布的集合,每个频率分布有一个不同的“条件”,这个条件通常是文本的类别.当语料文本分为几类(文体,主题,作者等)时,可以计算每个类别独立的频率分布,这样,就可以通过条件频率分布研究 ...

  10. java集合框架图