HDU4463-Outlets,简单最小生成树。1A水过~~
Outlets
Time Limit: 2000/1000
MS (Java/Others)
Memory
Limit: 32768/32768 K (Java/Others)
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.
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.
4
2 3
0 0
1 0
0 -1
1 -1
0
3.41
题意不难,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水过~~的更多相关文章
- hdu4463 Outlets 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4463 很裸的一道题目,稍微处理一下输入即可 代码: #include<iostream> ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- ytu 1304:串的简单处理(水题)
串的简单处理 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 39 Solved: 11[Submit][Status][Web Board] Desc ...
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- poj 2395 Out of Hay(最小生成树,水)
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...
- HDU 4463 Outlets(最小生成树给坐标)
Problem Description In China, foreign brand commodities are often much more expensive than abroad. T ...
- HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题
分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...
- HDU 4463 Outlets (最小生成树)
题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短. 析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生 ...
- 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)
以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...
随机推荐
- vue项目导出电子表格
方法一: 一.安装依赖(前面基本一样) npm install file-saver --save npm install xlsx --save npm install script-loader ...
- d3学习笔记
(1)使用enter()函数. 当要创建新的绑定数据的元素,必须使用enter().这个方法会分析当前选择的DOM元素和传给它的数据,如果数据值比对应的DOM元素多,就创建一个新的占位元素.然后把这个 ...
- 415 Add Strings 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和 ...
- Kali linux 2016.2(Rolling) 的详细安装(图文教程)附安装VMare Tools 增强工具
写在前面的话 因读研期间,实验室团队需要,所以,接触上了Kali Linux,需去获得网络安全方面的数据,即数据和信息收集.以便为后续的数据处理和分析,准备! 用到hadoop和spark.机器学习等 ...
- [转]为革命保护视力 --- 给 Visual Studio 换颜色
本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...
- SugarCRM安装踩雷(一)
安装SugarCRM前置条件: 1.找对平台.正确版本的安装包 2.APACHE + MYSQL + TOMCAT环境先确保OK 坑1: 进入安装参数设置步骤的MYSQL用户密码——这里根据Mysql ...
- git --版本对比
比较暂存区域和工作目录 -git diff 分别拷贝暂存区和工作目录的文件到a和b文件夹 --- //表示旧文件 暂存区的 +++ //表示新文件 工作目录的 F 一页一页往下移 B 一 ...
- bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?
echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...
- codeforces_B. Forgery
http://codeforces.com/contest/1059/problem/B 题意: For simplicity, the signature is represented as an ...
- Android原生方式获取经纬度
两种定位方式:GPS定位.WiFi定位优劣: 如果项目定位要求较高还是建议使用三方地图库 GPS定位相比Wifi定位更精准且可在无网络情况下使用,但在室内基本暴毙无法使用WiFi定位没有室内外限制也不 ...