Outlets

                                                                                                             Time Limit: 2000/1000
MS (Java/Others)                                                               

                                                                                                             Memory
Limit: 32768/32768 K (Java/Others)                                                                                         

                                                                            



->打开链接<-

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
 
Source

题意不难,n个商店吧,给出这n个商店的坐标,其中p和q号商店必须相连,求联通所有商店的最小路长;

数据范围都很小,显然是一个最小生成树问题,先把所有商店间的距离求出来,然后就是一个纯的kruskal了,只不过我们在用并查集的时候先见p和q号商店连接起来;

<span style="font-size:24px;">#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=2500+10;
int t,n,m,k,a[N],b[N],f[N];
double s;
struct node
{
int x,y;//联通的两个点;
double w;//存两点间的距离;
}c[N];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
return f[x]==-1?x:f[x]=find(f[x]);
}
double ks()
{
int i;
memset(f,-1,sizeof(f));
int x=find(n),y=find(m);
f[x]=y;
sort(c,c+k,cmp);
for(i=0;i<k;i++)
{
int x=find(c[i].x);
int y=find(c[i].y);
if(x!=y)
{
s+=c[i].w;
f[x]=y;
}
}
return s;
}
int main()
{
int i,j;
while(~scanf("%d",&t)&&t)
{
scanf("%d%d",&n,&m);
s=0,k=0;
for(i=1;i<=t;i++)
{
scanf("%d%d",&a[i],&b[i]);
for(j=1;j<i;j++)
{
c[k].x=j,c[k].y=i;//将两个点也存起来;
c[k++].w=hypot(a[j]-a[i],b[j]-b[i]);//紫书第62页有这个函数的用法,可以去看看,很方便;
if((i==n&&j==m)||(i==m&&j==n))
s+=hypot(a[j]-a[i],b[j]-b[i]);
}
}
printf("%.2f\n",ks());
}
}

HDU4463-Outlets,简单最小生成树。1A水过~~的更多相关文章

  1. hdu4463 Outlets 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4463 很裸的一道题目,稍微处理一下输入即可 代码: #include<iostream> ...

  2. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  3. ytu 1304:串的简单处理(水题)

    串的简单处理 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 39  Solved: 11[Submit][Status][Web Board] Desc ...

  4. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  5. poj 2395 Out of Hay(最小生成树,水)

    Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...

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

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

  7. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

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

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

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

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

随机推荐

  1. 洛谷 P2048 [NOI2010]超级钢琴 || Fantasy

    https://www.luogu.org/problemnew/show/P2048 http://www.lydsy.com/JudgeOnline/problem.php?id=2006 首先计 ...

  2. 《effective java》中文第2版 note

    ,第15条[66]: 为不可变类提供静态工厂, eg : Integer/BigInteger 使用了静态工厂缓存了一些常用的实例, 通常 Integer -128 ~ +127. BigIntege ...

  3. 学习笔记 第十章 使用CSS美化表单

    第10章   使用CSS美化表单 [学习重点] 正确使用各种表单控件 熟悉HTML5新增的表单控件 掌握表单属性的设置 设计易用性表单页面 10.1  表单的基本结构 表单包含多个标签,由很多控件组成 ...

  4. 免费大数据搜索引擎 xunsearch 实践

    以前在IBM做后端开发时,也接触过关于缓存技术,当时给了n多文档来学习,后面由于其他紧急的项目,一直没有着手去仔细研究这个技术,即时后来做Commerce的时候,后台用了n多缓存技术,需要build ...

  5. rem手机端页面自适应布局(待修正下一篇完美布局)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vscode前端开发软件配搭好用的插件

    使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...

  7. OpenMP入门教程(三)

    承接前面两篇,这里直接逐一介绍和使用有关OpenMP的指令和函数 Directives 1.for 作用:for指令指定紧随其后的程序的循环的迭代必须由团队并行执行,只是假设已经建立了并行区域,否则它 ...

  8. bindtextdomain - 设置 包括 消息条目 的 路径

    总览 (SYNOPSIS) #include <libintl.h> char * bindtextdomain (const char * domainname, const char ...

  9. fabric的安装

    https://blog.csdn.net/lepton126/article/details/79148027

  10. Java IO(二)--RandomAccessFile基本使用

    RandomAccessFile: 翻译过来就是任意修改文件,可以从文件的任意位置进行修改,迅雷的下载就是通过多个线程同时读取下载文件.例如,把一个文件分为四 部分,四个线程同时下载,最后进行内容拼接 ...