题目链接

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

分析:

题上要求的是用最短的线将坐标系上的n个点链接起来,这就是简单的最小生成树的问题,将任意两个点之间的距离计算出来,看作他们之间的路径长度,这样的话就能很好的理解为什么是最小生成树的问题了。

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#define INF 1 << 30
double a[101] , b[101] , map[101][101] ;
double dis[101] ;
int used[101] ;
void prim(int n)
{
int c = 0 ;
int i = 0 , j = 0 ;
double sum = 0 ;
dis[1] = 0 ;
for(i = 1 ; i <= n ; i++)
{
double min = INF ;
c = 0 ;
for(j = 1 ; j <= n ; j++)
{
if(!used[j] && dis[j] < min)
{
min = dis[j] ;
c =j ;
}
}
used[c] = 1 ;
for(j = 1 ; j <= n ; j++ )
{
if(!used[j] && dis[j] > map[c][j])
dis[j] = map[c][j] ;
}
} for(i = 1 ; i <= n ; i++)
sum += dis[i] ;
printf("%.2lf\n",sum);
}
int main()
{
int n = 0 ;
while(~scanf("%d",&n))
{
memset(a , 0 , sizeof( a ) ) ;
memset(b , 0 , sizeof( b ) ) ;
int i = 0 , j = 0 ;
for(i = 1 ; i <= n ; i++)
{
for(j = 1 ; j <= n ; j++)
map[i][j] = INF ;
dis[i] = INF ;
used[i] = 0 ;
}
for(i = 1 ; i <= n ; i++)
{
scanf("%lf%lf" , &a[i] , &b[i] );///切记不能向一般的图论的问题,在输入的同时保存路径
}
double m = 0 , x = 0;
for(i = 1 ; i <= n ; i++ )///应该在输入结束之后,将任意两点之间的路径计算出来
{
for(j = 1 ; j <= n ; j++)
{
x = (a[j]-a[i])*(a[j]-a[i])+(b[j]-b[i])*(b[j]-b[i]) ;
m = sqrt( x ) ;
map[i][j] = map[j][i] = m ;
}
}
prim( n );
}
return 0 ;
}

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

  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. 图->连通性->最小生成树(普里姆算法)

    文字描述 用连通网来表示n个城市及n个城市间可能设置的通信线路,其中网的顶点表示城市,边表示两城市之间的线路,赋于边的权值表示相应的代价.对于n个定点的连通网可以建立许多不同的生成树,每一棵生成树都可 ...

  5. 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 ...

  6. 图解最小生成树 - 普里姆(Prim)算法

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  7. 最小生成树---普里姆算法(Prim算法)和克鲁斯卡尔算法(Kruskal算法)

    普里姆算法(Prim算法) #include<bits/stdc++.h> using namespace std; #define MAXVEX 100 #define INF 6553 ...

  8. HDU 1162 Eddy's picture

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

  9. hdu 1162 Eddy's picture (prim)

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

随机推荐

  1. MySQL数据库服务器逐渐变慢分析

    第一步 检查系统的状态 1.1 使用sar来检查操作系统是否存在IO问题 #sar -u 2 10 — 即每隔2秒检察一次,共执行20次. [root@CacheMemCache tester]# s ...

  2. SVN被锁定如何解决?

    报错: “E:/SVN被锁定” 请进行清理操作 解决方式: 1.试着右击进行解锁 没有解决 2.试着进行清理,清理后再做更新操作 好的成功. OK能正常使用了. 2017年10月26日更新 报错: s ...

  3. Selenium驱动Firefox浏览器

    用Maven构建Selenium依赖: <dependency> <groupId>org.seleniumhq.selenium</groupId> <ar ...

  4. python 基础篇 11 函数进阶----装饰器

    11. 前⽅⾼能-装饰器初识本节主要内容:1. 函数名的运⽤, 第⼀类对象2. 闭包3. 装饰器初识 一:函数名的运用: 函数名是一个变量,但他是一个特殊变量,加上括号可以执行函数. ⼆. 闭包什么是 ...

  5. win10激活方法-专业版

    该教程操作思路,Win10家庭版升为专业版,然后激活版本 首先,进入设置→关于看到如下页面: 接着,进入实操阶段: 第一步   在激活版面输入密匙   ( 把家庭版升级为专业版 ) DR9VN-GF3 ...

  6. GBDT && XGBOOST

                                  GBDT && XGBOOST Outline Introduction GBDT Model XGBOOST Model ...

  7. 常见 SQL语句使用 增删改查

    一.常见的增删改查(一).查:1.SELECT 列名称 FROM 表名称,其中列名可以是多个,中间用豆号分开,如SELECT LastName,FirstName FROM Persons: 2.SE ...

  8. HDU 4735 Little Wish~ lyrical step~(DLX搜索)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Description N children are living in a tree with exactly N nodes, on each node there lies either a b ...

  9. 将EXCEL表中的数据轻松导入Mysql数据表

    转载自:http://blog.163.com/dielianjun@126/blog/static/164250113201042310181431/ 在网络上有不较多的方法,在此介绍我已经验证的方 ...

  10. lintcode-98-链表排序

    98-链表排序 在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序. 样例 给出 1->3->2->null,给它排序变成 1->2->3->nu ...