HDU 1162 Eddy's picture (最小生成树 prim)
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)的更多相关文章
- 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 ...
- 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 ...
- hdu 1162 Eddy's picture (prim)
Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 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 (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- hdu 1162 Eddy's picture(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
随机推荐
- 类的三大方法 与__init___
静态方法 存储在类中 实例方法 与类方法都能调用 不需要 传递self 相当于在类里面写了一个函数 类方法 存储在类中 只能是类调用 普通方法 存储在类中 由实例对象使用 在实例化类的时候 就相 ...
- hdu1285
解题思路:拓扑排序+优先队列,每次找入度为零时的点的时候且值最小的...我觉得题目有点问题,刚开始写的时候,以为样例的答案是1 4 2 3,毕竟1和4没输过啊...结果去看了样例一眼,傻了. #inc ...
- 【题解】ID分配
题目描述 您正在处理要为每个客户端分配唯一ID的特定系统的后端.但是,系统是分布式的,并且有许多组件,每个组件都必须能够为客户端分配ID.换句话说,您希望每个组件都使用尽可能少的组件之间的通信来分配I ...
- python numpy 数组拼接
我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2], [3, 4, 5], [6, 7, ...
- # BZOJ5300 [CQOI2018]九连环 题解 | 高精度 FFT
今天做了传说中的CQOI六道板子题--有了一种自己很巨的错觉(雾 题面 求n连环的最少步数,n <= 1e5. 题解 首先--我不会玩九连环-- 通过找规律(其实是百度搜索)可知,\(n\)连环 ...
- SharePoint 2013 Newsfeed 没有出现的解决方法
按照这个guide配置mysite: http://technet.microsoft.com/en-us/library/ee624362(v=office.15).aspx 但是newsfeed页 ...
- 获取get值&DOM例题
获取get值: <body> <form action="9.26获取get值.html" method="get"> ...
- ??? cliquers
解:先推一个式子,然后就是CRT了... 那个阶乘怎么求呢?主要是分母可能有0,这时我们把分母的因子p全部提出来,上下次数相减判断即可. 细节颇多......注意在快速幂开始的时候a %= MO是个好 ...
- (转)git中关于fetch的使用
将远程仓库的分支及分支最新版本代码拉取到本地: 命令:git fetch 该命令执行后,不会将拉取的分支的最新代码合并到当前分支,仅仅是拉取/下载下来到本地仓库中. 首先,我们使用git branch ...
- MyEclipse新建工作空间后的配置详细步骤
1. General --> Workspace --> UTF-8 2. General --> Editors -->File Associations --> J ...