题目链接

Problem Description

Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.

Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?

Input

The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.

Sample Input

3

1.0 1.0

2.0 2.0

2.0 4.0

Sample Output

3.41

分析:

好久没有敲最小生成树的代码,有点生疏了····

完全的裸的最小生成树,题目要求将所有的点连接起来所需要的最短的路径的长度,也就相当于图的最小生成树。唯一的就是图上给出的是点的坐标,需要将任意的两点之间的距离求出来。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
struct Node
{
double x;
double y;
} node [101];
double tu[101][101],sum;//图,存储的是任意的两点之间的距离
double dis[101];//到该点的最短距离
int vis[101],n;//标记数组,标记一个点是否已经放到最小生成树的集合中
double fun(const Node a,const Node b)//求两点之间的距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void prim()//裸的prim算法
{
double Min;
sum=0.0;
int k;
//默认以0点作为最小生成树的起点
for(int i=0; i<n; i++)
{
dis[i]=tu[0][i];//保存0点到任一点的距离
vis[i]=0;
}
vis[0]=1;//0这个点已经放到最小生成树里面
for(int i=1; i<n; i++)//构建n-1条边就行了
{
Min=0x3f3f3f3f;
for(int j=0; j<n; j++)//找到最小生成树之外的最小的那条边
{
if(vis[j]==0&&dis[j]<Min)
{
Min=dis[j];
k=j;
}
}
//if(Min==0x3f3f3f3f)
// break;
vis[k]=1;
sum+=Min;
//printf("%.2lf\n",sum);
for(int j=0; j<n; j++)
{
if(vis[j]==0&&dis[j]>tu[k][j])
dis[j]=tu[k][j];
}
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%lf%lf",&node[i].x,&node[i].y);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
tu[i][j]=0x3f3f3f3f;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i==j) continue;
else tu[i][j]=min(tu[i][j],fun(node[i],node[j]));
}
}
prim();
printf("%.2lf\n",sum);
}
return 0;
}

HDU 1162 Eddy's picture (最小生成树 prim)的更多相关文章

  1. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1162 Eddy's picture (最小生成树)(java版)

    Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...

  3. hdu 1162 Eddy's picture (最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. hdu 1162 Eddy's picture (Kruskal 算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  5. hdu 1162 Eddy's picture (prim)

    Eddy's pictureTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  7. HDU 1162 Eddy's picture (最小生成树 普里姆 )

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  8. hdu 1162 Eddy's picture(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...

  9. 题解报告:hdu 1162 Eddy's picture

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

随机推荐

  1. 洛谷 P4294 [WC2008]游览计划

    题目链接 不是很会呢,但似乎抄了题解后有点明白了 sol:状态DP显然,其实是要构建一棵最小生成树一样的东西,我自己的理解(可能不是很对哦希望多多指教)f[x][y][zt]就是到x,y这个点,状态为 ...

  2. BZOJ3110[Zjoi2013]K大数查询——权值线段树套线段树

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...

  3. Java生成多数值二元运算结果集

    看之前大学写过的24点程序中用到的核心计算算法——计算四个值能否计算出24,当时用的c++写的,现用Java重写一遍 程序实现了多个数值(可重复),每个数值只能运算一次,二元运算的条件下获得所有结果集 ...

  4. MT【17】利用柯西不等式求三角的最大值

    评:此题也可以设$1+cos\theta=t$,平方后变成$t$的单变量利用均值去做. 柯西平衡系数法其实就是待定系数法,利用等号取到的条件.

  5. java将一个javabean转化为另一个javabean

    公司的项目是用webservice来进行前后台对接,启动后台后需要刷服务才能在前台生成对应的代码,但是有一个很恶心的地方,它给每个service都生成了一个model,于是出现后台只有一个javabe ...

  6. 【LOJ#6041】事情的相似度(后缀自动机)

    [LOJ#6041]事情的相似度(后缀自动机) 题面 LOJ 题解 \(\mbox{YCB}\)搬了这道题目...\(\mbox{QwQ}\) 还是用到\(lcp\)就是\(parent\)树上的\( ...

  7. 树莓派上使用Pi-FM-RDS工具打造FM调频电台

    安装Pi-FM-RDS 安装依赖.sudo apt-get install libsndfile1-dev 克隆Pi-FM-RDS到本地.git clone https://github.com/Ch ...

  8. 【loj3054】【hnoi2019】鱼

    题目 描述 ​ 难以描述.......慢慢看..: ​ https://loj.ac/problem/3054 范围 ​ $6 \le n \le 1000  ,  1 \le |x| , |y| \ ...

  9. Unity3d-AngryBots实例解读

    最近粗略研究了下Unity3d自带的例子AngryBots,记录一下,部分内容摘自http://oulehui.blog.163.com/blog/static/7961469820125251051 ...

  10. 洛谷【P1523】旅行商的背包(算法导论 15-1) 题解

    P1523 旅行商简化版 题目背景 欧几里德旅行商\((Euclidean Traveling Salesman)\)问题也就是货郎担问题一直是困扰全世界数学家.计算机学家的著名问题.现有的算法都没有 ...