hdu4463 Outlets 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4463
很裸的一道题目,稍微处理一下输入即可
代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 60
int n;
double sumweight;
int tol;
int cnt;
int p,q;
class node
{
public:
int from;
int to;
double w;
};
class point
{
public:
int x;
int y;
};
point address[maxn];
node edge[maxn*maxn];
int parent[maxn*maxn];
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void addEdge(int u,int v)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].w=dis(address[u],address[v]);
if((u==p && v==q)||(u==q && v==p))
edge[tol].w=;
tol++;
}
void UFset()
{
for(int i=;i<maxn*maxn;i++)
parent[i]=-;
}
int Find(int x)
{
int s;
for(s=x;parent[s]>=;s=parent[s]);//我总是把parent[s]>=0 写为s>=0;
while(s!=x)
{
int tmp=parent[x];
parent[x]=s;
x=tmp;
} return s;
}
void Union(int u,int v)
{
int r1=Find(u);
int r2=Find(v);
int tmp=parent[r1]+parent[r2];
if(parent[r1]>parent[r2])
{
parent[r1]=r2;
parent[r2]=tmp;
}
else
{
parent[r2]=r1;
parent[r1]=tmp;
}
}
bool cmp(node a ,node b)
{
return a.w < b.w;
}
void Kruskal()
{
int u,v;
UFset();
cnt=;
for(int i=;i<tol;i++)
{
u=edge[i].from;
v=edge[i].to;
if(Find(u)!= Find(v))
{
sumweight+=edge[i].w;
cnt++;
Union(u,v);
if(cnt==n-) break;
}
} }
int main()
{
while(scanf("%d",&n)&&n)
{
tol=; scanf("%d%d",&p,&q);
int x,y;
for(int i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
address[i].x=x;
address[i].y=y;
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
addEdge(i,j);
}
sumweight=dis(address[p],address[q]);
sort(edge,edge+tol,cmp);
Kruskal();
printf("%.2f\n",sumweight); } return ;
}
hdu4463 Outlets 最小生成树的更多相关文章
- HDU—4463 Outlets 最小生成树
In China, foreign brand commodities are often much more expensive than abroad. The main reason is th ...
- hdu 4463 Outlets(最小生成树)
Outlets Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submi ...
- 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】最小生成树(prim,kruscal都可)
以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...
- HDU 4463 Outlets 【最小生成树】
<题目链接> 题目大意: 给你一些点的坐标,要求你将这些点全部连起来,但是必须要包含某一条特殊的边,问你连起这些点的总最短距离是多少. 解题分析: 因为一定要包含那条边,我们就记录下那条边 ...
- hdu 4463 Outlets(最小生成树)
题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 pri ...
- Outlets
Outlets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 最小生成树(Kruskal算法-边集数组)
以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...
随机推荐
- Hadoop/Spark开发环境配置
修改hostname bogon 为localhost 查看ip地址 [training@bogon ~]$ sudo hostname localhost [training@bogon ~]$ h ...
- 设置ssh只允许用户从指定的IP登陆
假设 我们公司的固定IP是 183.21.89.249 连接上我们自己进行管理的服务器 然后编辑ssh的配置文件默认 /etc/ssh/sshd_config 在文件最后面另起一行添加 ...
- 老李推荐:第5章7节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles
老李推荐:第5章7节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles poptest是国内唯一一家培养测试开 ...
- JAVA加密算法系列-DESCoder
package ***; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher ...
- 使用Docker分分钟启动常用应用
前言 Docker是目前比较火的一个概念,同时也是微服务中比较关键的一个容器化技术.但是,单从理论上好难看出Docker的优势,因此,我希望在这篇文章中提供一些Docker的使用示例,希望从实际应用上 ...
- Win7+CentOS双系统(二)
在之前的文章中我们实现了Win7+CentOS6.3双系统的安装和使用,不过比较不幸的是在CentOS6.4版本时其安装文件大小已经超过了FAT文件系统所能容纳的单个文件大小4G.我们使用FAT文件系 ...
- JavaScript分支语句if, else if, switch 案例详解
if语句主要是在需要判断,或者在可知有多少种情形时使用的语句.A==B?"A等于B";"A不等于B"; 基本结构: if(判断条件){ ...
- a标签去掉默认样式并自定义样式
a { text-decoration: none;//去掉下划线 color: inherit; -webkit-user-select: none; -moz-user-select: none; ...
- 关于Java中Arrays.sort()方法TLE
最近一直在练用Java写题,今天无意发现一道很简单的二分题(链接),我一开始是直接开int[]数组调用Arrays.sort()去排序,没想到TLE了,原来是因为jdk中对于int[]的排序是使用快速 ...
- CF CROC 2016 Intellectual Inquiry
题目链接:http://codeforces.com/contest/655/problem/E 大意是Bessie只会英文字母表中的前k种字母,现在有一个长度为m+n的字母序列,Bessie已经知道 ...