【九度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. EXCEL-排名前三名显示小红旗,后三名显示小黑旗

    总结(用的WPS):第一步:用=IF(RANK(数值,引用范围) <=3,"小红旗",IF(RANK(数值,引用,1) <=3,"小黑旗",&quo ...

  2. LInkedList总结及部分底层源码分析

    LInkedList总结及部分底层源码分析 1. LinkedList的实现与继承关系 继承:AbstractSequentialList 抽象类 实现:List 接口 实现:Deque 接口 实现: ...

  3. flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去

    1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...

  4. 记录一下使用MySQL的left join时,遇到的坑

    # 现象 left join在我们使用mysql查询的过程中可谓非常常见,比如博客里一篇文章有多少条评论.商城里一个货物有多少评论.一条评论有多少个赞等等.但是由于对join.on.where等关键字 ...

  5. 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题

    在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...

  6. Linux(CentOS 7)使用gcc编译c,c++代码

    安装gcc: 1.使用 yum -list gcc* 查询 centos 官方gcc的所有包: 可安装的软件包 gcc.x86_64 gcc-c++.x86_64 gcc-gfortran.x86_6 ...

  7. 「Python实用秘技01」复杂zip文件的解压

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的新系列文章「Python实用秘技」的第1 ...

  8. ANTLR 简介

    <ANTLR 4权威指南>由机械工业出版社出版,有兴趣的读者推荐购买阅读. 本专题大多内容来源于我读<ANTLR 4权威指南>的随手笔记以及个人实践,仅供参考学习,请勿用于任何 ...

  9. 了解LINQ

    本文主要的是泛谈LINQ是啥?以及常见的用法大纲如下: LINQ的那些根基 LINQ的一些基本用法 LINQ的根基 IEnumerable和IEnumerator 为啥能够被foreach? 实际上, ...

  10. Python绘制饼图

    Python绘制饼图 1.1 对应代码如下图所示 import matplotlib.pyplot as pltfrom pylab import mplmpl.rcParams['font.sans ...