题目链接:http://ac.jobdu.com/problem.php?pid=1144

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1144 Freckles.cpp
// Jobdu
//
// Created by PengFei_Zheng on 18/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 110 using namespace std; int tree[MAX_SIZE]; int findRoot(int x){// find the root of x
if(tree[x] == -) return x;
else{
int tmp = findRoot(tree[x]);
tree[x] = tmp;
return tmp;
}
} struct Edge{// define the edge which have line a and line b
int a, b;
double cost; // the length of point a to point b
bool operator < (const Edge &A) const{
return cost < A.cost;
}
}; struct Point{//define the point
double x, y;//(x,y) the position of this dot
double getDistance(Point A){//get the lenght between (x,y) and (A.x, A.y)
double tmp = (x-A.x)*(x-A.x) + (y-A.y)*(y-A.y);
return sqrt(tmp);
}
}; int n; int main(){
while(scanf("%d",&n)!=EOF){ Edge edge[n*(n-)/+];
Point point[n+]; for(int i = ; i <= n ; i++){
scanf("%lf%lf",&point[i].x,&point[i].y);//input the pos of (x,y)
tree[i]=-;//init the node i
}
int line_id = ;//define the id of the line
for(int i = ; i <= n ; i++){
for(int j = i+ ; j <= n ; j++){
edge[line_id].a = i;
edge[line_id].b = j;
edge[line_id].cost = point[i].getDistance(point[j]);//cal the length point[i] to point[y]
line_id++;
}
} sort(edge,edge+line_id);// from low to high double ans = ; for(int i = ; i < line_id ; i++){
int a = findRoot(edge[i].a);
int b = findRoot(edge[i].b);
if(a!=b){//merge the a node to b‘s aggregate
tree[a] = b;
ans += edge[i].cost;
}
}
printf("%.2lf\n",ans);
}
return ; }
/**************************************************************
Problem: 1144
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/

题目1144:Freckles(最小生成树进阶)的更多相关文章

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

    [九度OJ]题目1144:Freckles 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1144 题目描述: In an ...

  2. UVA 10034 Freckles 最小生成树

    虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...

  3. Freckles (最小生成树)

    #include<iostream> #include<cstring> #include<stdio.h> #include<queue> #incl ...

  4. hdu 4253(经典题目:二分+最小生成树)

    题意:就是说有A.B两个公司要修路,有m条路,可能是属于A修的,也可能是属于B修的,现在要求所有路都联通的情况下的最小权值,并且A公司必须要修k条路. 同: 代码: #include<iostr ...

  5. UVA 1151 买还是建(最小生成树)

    买还是建 紫书P358 [题目链接]买还是建 [题目类型]最小生成树 &题解: 这题真的心累,看了3天,最后照着码还是wa,先放lrj代码,以后再看吧 &代码: // UVa1151 ...

  6. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

  7. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

  8. 软工+C(2017第1期) 题目设计、点评和评分

    // 下一篇:分数和checklist 如何设计题目 教学中的一个问题是老师出题太简单了,题目设计一开始上来就不紧凑,我认为一个好的课程应该上来就给你紧凑感,而不是先上来"轻松2-3周&qu ...

  9. JVM菜鸟进阶高手之路十四:分析篇

    转载请注明原创出处,谢谢! 题目回顾 JVM菜鸟进阶高手之路十三,问题现象就是相同的代码,jvm参数不一样,表现的现象不一样. private static final int _1MB = 1024 ...

随机推荐

  1. jmm 和线程安全

    Java的内存模型JMM Java的内存模型JMM(Java Memory Model)JMM主要是为了规定了线程和内存之间的一些关系.根据JMM的设计,系统存在一个主内存(Main Memory), ...

  2. 如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录。

    如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录. 主要用到DATEADD函数,下面是详细语句 取最近3天 select * from 表名where rq> ...

  3. Thinkphp5模板继承

    代码 application\index\controller\index.php <?php namespace app\index\controller; use app\index\con ...

  4. Node.js 模块之 morgan中间件记录日志

    NodeJs中Express框架使用morgan中间件记录日志 Express中的app.js文件已经默认引入了该中间件var logger = require('morgan'); 使用app.us ...

  5. Node.js之操作Mongodb

    // mongoose 链接 var mongoose = require('mongoose'); var db = mongoose.createConnection('mongodb://127 ...

  6. C#特性文章学习

    http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html http://www.cnblogs.com/liuxinxin/artic ...

  7. redis 的set数据类型

    相关命令 1.SADD SADD key-name item1 [item 2…] 将一个或多个成员元素加入到集合中 2.SREM SMEMBERS  key-name item1 [item 2…] ...

  8. MysqlHelper使用反射机制智能推算数据类型以及属性名称

    public class MySqlHelper { private string ConnString; public MySqlHelper(string connectionString) { ...

  9. Visual Studio 2012中使用GitHub

    前言 一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等 一系列工具进行Copy,然后回家才能继续在原来的基础上作业 ...

  10. 使用apache的poi实现导入导出excel

    1.jar包:poi-3.14-20160307.jar.poi-ooxml-3.14-20160307.jar 2.导入(本例实现了解析excel生成List): @Override public ...