题目链接

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. linux下拷贝文件夹的时候排除其中的一些目录

    http://blog.csdn.net/wind19/article/details/8960574 使用find cd /usr find ./tmp/ | grep -v tmp/dirc | ...

  2. PKUWC 2019&WC 2019爆零记

    PKUWC 2019&WC 2019爆零记 毕竟过了很久了,杂七杂八的东西就不写了,并且除成绩之外的内容不保证其正确性. Day1 T1:看到这道题很舒服啊,枚举top序算合法图的数量,状压D ...

  3. BZOJ4943 NOI2017蚯蚓排队(哈希+链表)

    能看懂题就能想到正解.维护所有长度不超过k的数字串的哈希值即可,用链表维护一下蚯蚓间连接情况.由于这样的数字串至多只有nk个,计算哈希值的总复杂度为O(nk),而分裂的复杂度为O(ck^2),询问复杂 ...

  4. Dapper 介绍

    转载:http://***/html/itweb/20130918/125194_125199_125210.htm .NET 轻量级 ORM 框架 - Dapper 介绍 Dapper简单介绍: D ...

  5. python构建bp神经网络_鸢尾花分类(一个隐藏层)__1.数据集

    IDE:jupyter 目前我知道的数据集来源有两个,一个是csv数据集文件另一个是从sklearn.datasets导入 1.1 csv格式的数据集(下载地址已上传到博客园----数据集.rar) ...

  6. 【POI每日题解 #8】DYN-Dynamite

    你问蒟蒻为什么一天写两篇每日题解? 难道每日坚果你不能一天吃两包吗? 题目链接 哇…这道题第一反应就是二分答案[太明显了 枚举答案 就那个“关键节点到这些点中距离的最小值的最大值”[蒟蒻读了好几遍…… ...

  7. UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 9: illegal multibyte sequence

    最近对爬虫有点着迷, 在用bs4模块时,遇到报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 9: illeg ...

  8. suoi08 一收一行破 (tarjanLca+树状数组)

    用一个差分树状数组维护一下每个深度的和,然后每次拿着路径端点和lca加一加减一减就行了 #include<bits/stdc++.h> #define pa pair<int,int ...

  9. Spring Cloud(四) --- config

    Spring Cloud Config 随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的 ...

  10. A1053. Path of Equal Weight

    Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...