HDU—4463 Outlets 最小生成树
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.
InputThere 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.
OutputFor 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 题意:给你n个点,最小的价值使得所有的点连通,但是p,q一定是直连的。
这是一道比较的模板的最小生成树的题,但是要保证有一条边一定在这颗树内,我们可以使用Kruskal算法的时候,直接把ans先设置为p,q之间距离的值,然后在加边的时候把值设置为0,那么根据Kruskal算法的思想,
这个边最小肯定是最先加进来了,那么其他的就和其他的没有区别了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=;
int n,p,q;
int cnt;
struct Point
{
int x,y;
}point[maxn];
struct Node
{
int from,to;
double value;
}node[maxn*maxn];
int fa[maxn];
bool cmp(Node a,Node b)
{
return a.value<b.value;
}
void init()
{
for(int i=;i<maxn;i++)
fa[i]=i;
}
int findd(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=findd(fa[x]);
}
double getdist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Kruskal()
{
double ans=getdist(point[p],point[q]);
for(int i=;i<=cnt;i++)
{
int fx=findd(node[i].from);
int fy=findd(node[i].to);
if(fx!=fy)
{
ans+=node[i].value;
fa[fx]=fy;
}
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
scanf("%d %d",&p,&q);
for(int i=;i<=n;i++)
scanf("%d %d",&point[i].x,&point[i].y);
cnt=;
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
cnt++;
node[cnt].from=i;node[cnt].to=j;
node[cnt].value=getdist(point[i],point[j]);
if((i==p&&j==q)||(i==q&&j==p))
node[cnt].value=;
}
}
init();
sort(node+,node+cnt+,cmp);
double sum=Kruskal();
printf("%.2f\n",sum);
}
return ;
}
HDU—4463 Outlets 最小生成树的更多相关文章
- hdu 4463 Outlets(最小生成树)
Outlets Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submi ...
- 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)
以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...
- HDU 4463 Outlets(最小生成树给坐标)
Problem Description In China, foreign brand commodities are often much more expensive than abroad. T ...
- HDU 4463 Outlets (最小生成树)
题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短. 析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生 ...
- HDU 4463 Outlets 【最小生成树】
<题目链接> 题目大意: 给你一些点的坐标,要求你将这些点全部连起来,但是必须要包含某一条特殊的边,问你连起这些点的总最短距离是多少. 解题分析: 因为一定要包含那条边,我们就记录下那条边 ...
- hdu 4463 Outlets(最小生成树)
题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 pri ...
- hdu 4463 Outlets
#include<bits/stdc++.h> using namespace std; double x[100+5],y[100+5]; double e[100+5][100+5]; ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树
题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...
随机推荐
- poj 3525Most Distant Point from the Sea【二分+半平面交】
相当于多边形内最大圆,二分半径r,然后把每条边内收r,求是否有半平面交(即是否合法) #include<iostream> #include<cstdio> #include& ...
- C# Web页面打印网页
<style media=print type="text/css"> .noprint{display:none} </style> 在打印时 ...
- python 匿名函数的使用(并没有那么简单)
以下为几种匿名函数的使用方式:x=[(lambda x:x**2)(x) for x in range(10)]print(x)y=[x**2 for x in range(10)]print(y)i ...
- 《linux就该这么学》学习笔记
本篇文章是根据刘遄老师的<linux就该这么学>中个人易忘知识点的读书笔记,结合的是个人弱点,可能不适合广大的网友同学,并在此声明本篇文章只是用于学习之用,绝无侵犯版权之意 linux就该 ...
- Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法
Specifying the Code to Run on a Thread 上一课 下一课 1.This lesson teaches you to Define a Class that Im ...
- Toasts官方教程
Toasts IN THIS DOCUMENT The Basics Positioning your Toast Creating a Custom Toast View 在其它线程中启动Toast ...
- border-1px的实现(stylus)
当样式像素一定时,因手机有320px,640px等.各自的缩放比差异,所以设备显示像素就会有1Npx,2Npx.为保设计稿还原度,解决就是用media + scale. // stylus语法 bor ...
- Radis
http://www.redis.cn/ http://try.redis.io/ http://www.redisdoc.com/en/latest/ Redis 命令参考¶ 本文档是 Redis ...
- Partial(部分方法,局部方法),virtual(虚方法),abstract(抽象方法)
Partial 部分方法顾明思议是方法的一部分,不完整的,在ide编译时候,会将所有部分方法加载到一起统一编译,如果分部方法没有被实现,编译器就不会.对他们进行编译. 局部类型的限制 (1) 局部类型 ...
- 微软最新的Web服务器Katana发布了版本3
Katana 项目入门 Howard Dierking 当 ASP.NET 首次在 2002 年发布时,时代有所不同. 那时,Internet 仍处于起步阶段,大约有 5.69 亿用户,每个用户平均每 ...