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 ...
随机推荐
- Kotlin的属性委托:无上下文情况下Android的赋值(KAD 15)
作者:Antonio Leiva 时间:Mar 9, 2017 原文链接:https://antonioleiva.com/property-delegation-kotlin/ 如我们在前面文章中读 ...
- python学习总结----内置函数及数据持久化
抽象基类(了解) - 说明: - 抽象基类就是为了统一接口而存在的 - 它不能进行实例化 - 继承自抽象类的子类必须实现抽象基类的抽象方法 - 示例: from abc import ABC, abs ...
- const char and static const char
部分内容摘自:https://blog.csdn.net/ranhui_xia/article/details/32696669 The version with const char * will ...
- C++STL——队列
一.相关定义 原理:queue 队列也是一个线性存储表,元素数据的插入在表的一端进行,在另一端删除,从而构成了一个先进先出FIFO(First In First Out)表. 队头&队尾:插入 ...
- Docker实战系列一:初识Docker for Windows
windows下安装Docker官网教程Install Docker for Windows Docker配置官网教程Get started with Docker for Windows
- 【bzoj2384】[Ceoi2011]Match 特殊匹配条件的KMP+树状数组
题目描述 给出两个长度分别为n.m的序列A.B,求出B的所有长度为n的连续子序列(子串),满足:序列中第i小的数在序列的Ai位置. 输入 第一行包含两个整数n, m (2≤n≤m≤1000000). ...
- ASP.NET页面之间传值Session(2)
想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽. 优点:1.使用简单,不仅能传递简单数据类型,还能传递对象. 2 ...
- P2168 [NOI2015]荷马史诗
题目描述 追逐影子的人,自己就是影子 ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛&g ...
- P1268 树的重量
题目描述 树可以用来表示物种之间的进化关系.一棵“进化树”是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离,重构相应的“进化树 ...
- (补漏)Springboot2.0 集成shiro权限管理
原文Springboot2.0 集成shiro权限管理 一.关于停止使用外键. 原本集成shiro建立用户.角色.权限表的时候使用了外键,系统自动创建其中两个关联表,用@JoinTable.看起来省事 ...