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 ...
随机推荐
- python之tkinter使用-二级菜单
# 菜单功能说明:二级菜单 import tkinter as tk from tkinter import messagebox root = tk.Tk() root.title('菜单选择') ...
- List泛型集合
List和数组 相同点: 都可以控制元素类型 不同点: List的长度是可变的,所以list比数组更容易掌控 List属性 1.Count 获取集合中实际包含的元素个数 2.Capcity 集合中可以 ...
- Code First NotMapped
转载:http://www.cnblogs.com/libingql/p/3352058.html 不需要映射的字段,添加 NotMapped 6.非数据库字段属性 在类中,如果有一些属性不需要映射到 ...
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...
- 2017ACM/ICPC亚洲区沈阳站-重现赛
HDU 6222 Heron and His Triangle 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6222 思路: 打表找规律+大数运算 首先我 ...
- Codeforces Round #337 (Div. 2) C. Harmony Analysis
题目链接:http://codeforces.com/contest/610/problem/C 解题思路: 将后一个矩阵拆分为四个前一状态矩阵,其中三个与前一状态相同,剩下一个直接取反就行.还有很多 ...
- 自学Aruba4.2-Aruba AC基础配置(1)
点击返回:自学Aruba之路 自学Aruba4.2-Aruba AC基础配置(1) 管理员登陆(admin/saic_admin): Cli Web 管理帐号 控制器基础设置: 控制器恢复出厂设置 查 ...
- android handler msg的使用 实现进度条
package com.app.threadtest; import android.app.Activity; import android.os.Bundle; import android.os ...
- 【Luogu4719】动态dp
题面 洛谷 题解 等下发链接 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- 【BZOJ2246】[SDOI2011]迷宫探险(搜索,动态规划)
[BZOJ2246][SDOI2011]迷宫探险(搜索,动态规划) 题面 BZOJ 洛谷 题解 乍一看似乎是可以求出每个东西是陷阱的概率,然而会发现前面走过的陷阱是不是陷阱实际上是会对当前状态产生影响 ...