【九度OJ】题目1144:Freckles 解题报告

标签(空格分隔): 九度OJ


原题地址:http://ac.jobdu.com/problem.php?pid=1144

题目描述:

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture 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 amount of 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 a sequence of connected lines from any freckle to any other freckle.

输入:

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

输出:

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:

3
1.0 1.0
2.0 2.0
2.0 4.0

样例输出:

3.41

Ways

最小生成树问题。求最小生成树,首先对边进行排序,然后要遍历所有的边,看这个边是否已经添加到了生成树上,如果没有就把这个边加上去,同时生成树的最终的代价要更新。

这里出错的地方是size的问题,注意其他的循环是从0开始的,但是包含size的循环是从0开始的,包括排序,所以不要搞错。

另外就是如何确定边的问题,没必要非要和顶点扯上关系,只要记录是第几个顶点和第几个顶点之间的边即可。

#include<stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define N 101
int Tree[N]; struct Edge {
int a, b;
double cost; bool operator<(const Edge &A) const {
return cost < A.cost;
}
} edge[6000]; struct Point {
double x, y; double getDistance(Point A) {
double tmp = (x - A.x) * (x - A.x) + (y - A.y) * (y - A.y);
return sqrt(tmp);
}
} point[101]; int findRoot(int x) {
if (Tree[x] == -1) {
return x;
} else {
int temp = findRoot(Tree[x]);
Tree[x] = temp;
return temp;
}
} int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &point[i].x,
&point[i].y);//double的输入方式是lf
}
int size = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
edge[size].a = i;//注意,记录的是第几条,不是具体的值
edge[size].b = j;
edge[size].cost = point[i].getDistance(point[j]);
size++;
}
}
sort(edge, edge + size);//从0开始,因为size从0开始
for (int i = 1; i < N; i++) {
Tree[i] = -1;
}
double answer = 0;
for (int i = 0; i < size; i++) {//从0开始,因为size从0开始
int aRoot = findRoot(edge[i].a);
int bRoot = findRoot(edge[i].b);
if (aRoot != bRoot) {
Tree[aRoot] = bRoot;
answer += edge[i].cost;
}
}
printf("%.2lf\n", answer);
} return 0;
}

Date

2017 年 3 月 10 日

【九度OJ】题目1144:Freckles 解题报告的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

  10. 九度oj 题目1024:畅通工程

    题目描述:     省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...

随机推荐

  1. [linux] 非root安装Python2及其模块

    需求 系统自带的python2版本太低,且没有想要的模块,非root用户无法安装.有些模块是python2写的,无法用python3,所以自己下载一个高版本的python2,可以自由下载模块. 实现 ...

  2. zabbix监控php状态

    环境介绍: php /usr/loca/php nignx /usr/loca/nginx  配置文件都是放在extra中 修改php-fpm的配置文件启动状态页面 pm.status_path = ...

  3. 如何使用csapp文件

    深入理解操作系统中有个csapp.h的头文件 以下来介绍下如何使用它: 该头文件下载地址为http://download.csdn.net/detail/tzasd89812/4206284 在Ubu ...

  4. char*,string,char a[], const char *,之间的转换

    1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可.      EX: const char* tmp = "tsinghu ...

  5. 这份github上被14万人点赞的Java教程太强了

    前几天有个小伙伴加我之后问了下面的这个问题.我看到后是一脸懵逼的状态,jcombobox?实话说,我已经完全忘了在Java中还有这么个东西. 在网上一番搜索后,才发现原来它是 swing 中的下拉列表 ...

  6. 从for循环到机器码

    def p(*x): print(x) p(type(range), dir(range)) r = range(2); i = iter(r) try: p(next(i)); p(next(i)) ...

  7. day10 ajax的基本使用

    day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...

  8. day09 文件属性

    day09 文件属性 昨日回顾 yum底层原理: 第一步:执行yum install nginx安装命令 第二步:yum去/etc/yum.repos.d这个目录中 第三步:根据/etc/yum/re ...

  9. linux网络相关命令之脚本和centos启动流程

    nice 功用:设置优先权,可以改变程序执行的优先权等级.等级的范围从-19(最高优先级)到20(最低优先级).优先级为操作系统决定cpu分配的参数,优先级越高,所可能获得的 cpu时间越长. 语法: ...

  10. 【Linux】【Shell】【text】Vim

    文本编辑器: 文本:纯文本,ASCII text:Unicode: 文本编辑种类: 行编辑器:sed 全屏编辑器:nano, vi vi: Visual Interface vim: Vi IMpro ...