题目链接

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. MySQL Binlog详解

    MySQL Binlog详解 Mysql的binlog日志作用是用来记录mysql内部增删改查等对mysql数据库有更新的内容的记录(对数据库的改动),对数据库的查询select或show等不会被bi ...

  3. 自学Linux Shell3.4-文件处理命令touch cp mv rm

    点击返回 自学Linux命令行与Shell脚本之路 3.4-文件处理命令touch cp mv rm 1. touch命令 一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将 ...

  4. 结构体练习(C)

    结构体存储学生学号.姓名.总分,动态内存分配增加信息,然后排序 # include <stdio.h> # include <malloc.h> //# include < ...

  5. pyinstaller模块使用

    目前pip install pyinstaller已经成熟 但是还是有一些坑,郁闷了好久,记一下注意点吧. 将py脚本打包成exe文件时,如果导入了非python自带库,则需要将导入的库从site-p ...

  6. 解码(ByteBuffer): CharsetDecoder.decode() 与 Charset.decode() 的不同

    今天测试的时候发现一个问题: ByteBuffer inputBuffer = ByteBuffer.allocate(1024); StringBuilder inputData = new Str ...

  7. Python基础学习(五)

    一.使用模块 已经了解了什么是模块,模块就是一个个文件的体,我们可以做不同的文件中引入各个模块文件,当然如果模块有冲突,还可以给模块文件的上层建立一个目录简称包,包名只能唯一,不能重名. 另外,一旦建 ...

  8. mod(%)之规律(除数与被除数的正负分析)

    首先注意“-9 % 4”,根据运算符优先级,负号运算符优先级大于余数(取模),所以执行的是“(-9) % 4”. 其次 % = mod ,只是在不同地方表示方法不同而已. 被除数无论是正数和负数结果都 ...

  9. advancedsearch.php织梦高级自定义模型字段无法调用解决方案

    advancedsearch.php织梦dedecms 高级自定义模型字段无法调用解决方案 ,具体步骤如下: 1  打开修改puls/advancedsearch.php文件,找到复制代码(不同版本可 ...

  10. make_blobs

    一.make_blobs简介 scikit中的make_blobs方法常被用来生成聚类算法的测试数据,直观地说,make_blobs会根据用户指定的特征数量.中心点数量.范围等来生成几类数据,这些数据 ...