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. Looper、MessageQueue、Message、Handler的关系

    1.快速复习 1.1 基本装置 类 装置名 作用 线程中数量 Looper 消息分发装置 从消息队列中取出一个消息,交给对应的Handler处理消息. 1 MessageQueue 消息队列 保存所有 ...

  2. Java中的流(4)InputStream,InputStreamReader,BufferedReader关系

    InputStream是字节流,InputStreamReader将字节流转成字符流,BufferedReader将字符流转成字符缓冲,开始读字符. 1.InputStream.OutputStrea ...

  3. java 字符串截取的几种方式

    1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时. String str = " ...

  4. 自定义Image HtmlHelper

    public static void Image(this HtmlHelper helper, string src, string alt = null, object htmlAttribute ...

  5. 使用 JSX 描述 UI 信息

    这一节我们通过一个简单的例子讲解 React.js 描述页面 UI 的方式.把 src/index.js 中的代码改成: import React, { Component } from 'react ...

  6. JS中的逻辑运算符&&、||,位运算符|,&

    1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...

  7. java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to

    在做android解析服务器传来的json时遇到的错误. 服务器传来的数据格式 [{"," id":"7ef6815938394fce88a5873312b66 ...

  8. Compiler 1.6.5 —1.6.7

    Compiler  1.6.5 —1.6.7 Dynamic Scope Technically, any scoping policy is dynamic if it is based on fa ...

  9. markdown表格中怎么插入逻辑或符号|?

    | 73向下投票接受 | 如果你删除反引号(****),使用|`黑客作品 a | r ------------|----- `a += x;` | r1 a |= y; | r2 并产生以下输出 在这 ...

  10. 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(四)-----mapActions

    介绍mapActions辅助函数: Action提交的是Mutation,不能够直接修改state中的状态,而Mutations是可以直接修改state中状态的:Action是支持异步操作的,而Mut ...