HDU 1162 Eddy's picture (最小生成树 普里姆 )
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 (最小生成树 普里姆 )的更多相关文章
- hdu 1162 Eddy's picture(最小生成树算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1162 Eddy's picture (最小生成树)(java版)
Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...
- hdu 1162 Eddy's picture (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 图->连通性->最小生成树(普里姆算法)
文字描述 用连通网来表示n个城市及n个城市间可能设置的通信线路,其中网的顶点表示城市,边表示两城市之间的线路,赋于边的权值表示相应的代价.对于n个定点的连通网可以建立许多不同的生成树,每一棵生成树都可 ...
- 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 ...
- 图解最小生成树 - 普里姆(Prim)算法
我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...
- 最小生成树---普里姆算法(Prim算法)和克鲁斯卡尔算法(Kruskal算法)
普里姆算法(Prim算法) #include<bits/stdc++.h> using namespace std; #define MAXVEX 100 #define INF 6553 ...
- HDU 1162 Eddy's picture
坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- hdu 1162 Eddy's picture (prim)
Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Qt Qwdget 汽车仪表知识点拆解8 淡入效果
先贴上效果图,注意,没有写逻辑,都是乱动的 看下面的开始,开始的时候有一个带入的效果,这里有一个坑, 网上大部分都是调用下面这个函数 setWindowOpacity(); 但是,你会发现,在你的子窗 ...
- Qt Qwdget 汽车仪表知识点拆解6 自定义控件
先贴上效果图,注意,没有写逻辑,都是乱动的 这里说一下控件自定义 图中标出的部分都是自定义的控件 这里如果我们有批量类似的功能,就可以使用自定义控件的方式,这里我已下面的自定义控件说一下,上面的在上一 ...
- react实现页面切换动画效果
一.前情概要 注:(我使用的路由是react-router4) 如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升: but the 问题是 ...
- 如何理解流Stream
百度百科: 计算机中的流其实是一种信息的转换.它是一种有序流,因此相对于某一对象,通常我们把对象接收外界的信息输入(Input)称为输入流,相应地从对象向外输出(Output)信息为输出流,合称为输入 ...
- js中迭代元素特性与DOM中的DocumentFragment类型 笔记
JS中迭代元素特性 在需要将DOM结构序列化为XML或者HTML字符串时,多数都会涉及遍历元素的特性,这个时候attributes属性就可以派上用场. 以下代码展示了如何迭代元素的每一个特性,然后将他 ...
- HDU 1005 Wooden Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1051 Problem Description There is a pile of n wooden stick ...
- To Chromium之VS调试追踪
启动的code: for(;;){...WaitForWork()}base.dll!base::MessagePumpForUI::DoRunLoop ...
- 算法(5)Jump Game
题目:非负数的数组,每个数组元素代表这你能最大跨越多少步,初始在0的位置,问,能不能正好调到数组的最后一位! https://leetcode.com/problems/jump-game/#/des ...
- [STL] 遍历删除两个vector中交集
#include <vector> #include <string> #include <algorithm> using namespace std; int ...
- 【bzoj3626】[LNOI2014]LCA 树链剖分+线段树
题目描述 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q次询问,每次询 ...