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. TIME-April

    一转眼四月份又过了三分之一,现在才开始计划自己的四月还真是对自己太过放松了呀!不过前一段时间都在搞学生会的五四评优答辩,索然不是我喜欢的过程,但是结果还比较令人欢喜.翻掉过去的篇章,展开新的一页. 四 ...

  2. 51nod 1088 最长回文子串

    1088 最长回文子串 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一 ...

  3. magento 用程序生成优惠劵码

    参考自http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code 上面的代码只能生成C ...

  4. 214 Shortest Palindrome 最短回文串

    给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...

  5. 有关LD_LIBRARY_PATH与ld.so.conf

    我之前写过一篇关于LD_LIBRARY_PATH与gcc/g++ -L的关系的文章,于是我自己用CPACK制作了一个Debian安装包,然后我在/home/.bashrc里添加了export LD_L ...

  6. Linux终端下对话

    1.首先查看当前账户 [hadoop@weekend01 ~]$ who hadoop   tty1         2016-11-14 09:31 (:0) hadoop   pts/0      ...

  7. html添加css——样式选择器

    如何给html添加样式.两种方法: 一.新建立一个css样式表,与原html同目录,然后通过link标签链接.如:<link type="text/css" rel=&quo ...

  8. android ListView 分析(一)

    需要了解的内容 1. listview中的getItemAtPosition与Adapter的getItem的position的区别          listView中的getItemAtPosit ...

  9. TCP/UDP套接字 java socket编程实例

    网络协议七层结构: 什么是Socket? socket(套接字)是两个程序之间通过双向信道进行数据交换的端,可以理解为接口.使用socket编程也称为网络编程,socket只是接口并不是网络通信协议. ...

  10. html中 accept 属性

    1.HTML <input> 标签的 accept 属性 在文件上传中使用 accept 属性,本例中的输入字段可以接受 GIF 和 JPEG 两种图像: <form> < ...