poj_2560 Freckles

news/2024/7/3 12:54:28 标签: float, numbers, each, output, input, 算法

Freckles

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 5248

Accepted: 2720

题目:http://poj.org/problem?id=2560

Description

In an episode of the Dick VanDyke show, little Richie connects the freckles on his Dad's back to form apicture of the Liberty Bell. Alas, one of the freckles turns out to be a scar,so his Ripley's engagement falls through.
Consider Dick's back to be a plane with freckles at various (x,y) locations.Your job is to tell Richie how to connect the dots so as to minimize the amountof ink used. Richie connects the dots by drawing straight lines between pairs,possibly lifting the pen between lines. When Richie is done there must be asequence of connected lines from any freckle to any other freckle.

Input

The first line contains 0 <n <= 100, the number of freckles on Dick's back. For each freckle, a linefollows; each following line contains two real numbers indicating the (x,y)coordinates of the freckle.

Output

Your program prints a singlereal number to two decimal places: the minimum total length of ink lines thatcan connect all the freckles.

Sample Input

3

1.0 1.0

2.0 2.0

2.0 4.0

Sample Output

3.41

Source

Waterloolocal 2000.09.23

题意:

       输入n个点的坐标位置,求走完全部点的最小距离。

解题思路:

       这是一个求图的最小生成树的问题,权值就是两个点之间的距离,用prin()算法来求解——先以一个节点(1)作为最小生成树的初始节点,然后在一次选择与这点相连权值最小的边放在集合中,这样不断的向集合中增加节点,直到每个节点添加完成

代码:

#include <iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#define MAX 103

#define VALUE 0xffffff

using namespace std;

struct coor

{

   float x;

   float y;

};

coor cor[MAX];

float g[MAX][MAX];

bool used[MAX];

float d[MAX];

int ts;

 

float distinct(coor x1,coor x2)

{

   return sqrt((x2.x-x1.x)*(x2.x-x1.x)+(x2.y-x1.y)*(x2.y-x1.y));

}

 

float prim()

{

   int i;

   for(i=0;i<=ts;i++)

    {

       used[i]=false;

       d[i]=VALUE;

    }

   d[1]=0;

   float res=0;

   while(true)

    {

       int t=-1;

       for(i=1;i<=ts;i++)

       {

           if(!used[i] && (t==-1 || d[i]<d[t]))

                t=i;

       }

       if(t==-1)

           break;

       used[t]=true;

       res+=d[t];

       for(i=0;i<=ts;i++)

       {

           if(d[i]>g[t][i])

                d[i]=g[t][i];

       }

    }

   return res;

}

 

 

int main()

{

 

    scanf("%d",&ts);

   //建图

   memset(g,0,sizeof(g));

   for(int i=1;i<=ts;i++)

    {

       scanf("%f%f",&cor[i].x,&cor[i].y);

       for(int j=1;j<i;j++)

       {

           g[i][j]=distinct(cor[i],cor[j]);

           g[j][i]=g[i][j];

       }

 

    }

   printf("%.2f\n",prim());

   return 0;

}

 


http://www.niftyadmin.cn/n/862682.html

相关文章

技术总监谈好的程序员如何写代码 另附:招聘程序员应考察的

要判断一个程序员是不是好的程序员&#xff0c;主要看他写的代码&#xff0c;因为程序员最重要的事是写代码。 即便不去理解代码的意图&#xff0c;只要看一眼&#xff0c;好的程序员写的代码与差的程序员写的代码基本上就可以看出来。好的程序员写的代码&#xff0c;整洁而规…

2021.04.22【RNA-seq流程】丨count值转换为FPKM值优化2.0

优化内容 解决每次转换需要设置样本数和基因数目实现基因count值与length精准匹配摘要 大概半年前&#xff0c;我写过一篇将HTseq生成的基因COUNT值转换为FPKM值文章&#xff0c;用于对count的入门级均一化处理。随着项目越做越多&#xff0c;逐渐发现了之前写的脚本的局限性。…

2021.04.23丨批量提取子目录文件

这是木青的第96篇原创文章&#xff0c;本篇240字&#xff0c;阅读大约需要1分钟 文章目录摘要环境与方法使用代码总结摘要 做项目偶尔会收到一些上游测序企业&#xff0c;把每个样品单独放在一个文件夹内&#xff0c;样品少还可以手动搬运&#xff0c;样品数量大就比较麻烦了。…

poj_1671 Phone List

Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others) Total Submission(s): 4602 Accepted Submission(s): 1557 题目连接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1671 Problem Description Given a list of p…

hdu_4190 DistributingBallot Boxes

DistributingBallot Boxes Time Limit: 20000/10000 MS (Java/Others) Memory Limit:65536/32768 K (Java/Others) Total Submission(s): 310 Accepted Submission(s): 154 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4190 Problem Description Tod…

SQL Server DBA 的工作清单

有许多不同类型的数据库管理员。 一些类型的数据库管理员致力于于开发领域&#xff0c;而其他的一部分更重视数据库性能的调整以及仍然有一部分数据库管理员则致力于管理SQL Server的业务。 依据数据库管理员的工作环境不同&#xff0c;他们将执行一定数量的不同的任务。为了区…

hdu_1251 统计难题

统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit:131070/65535 K (Java/Others) Total Submission(s): 9856 Accepted Submission(s): 3989 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1251 Problem Description Ignatius最近遇到一个…

看看DBA应该具备怎样的素质

近年来&#xff0c;我一直在和数据库管理员打交道&#xff0c;并直接面试了很多DBA职位。本文想概括一下IT行业对DBA的要求&#xff0c;以及国内DBA的新资现状。可以肯定地说&#xff0c;做一个高级DBA是很不错的职业。如果你打算成为一名DBA&#xff0c;那么希望本文起到抛砖引…