题目链接: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. linux中iptables的用法

    iptables基本操作笔记 一.基本操作 #启动防火墙 service iptables start #停止防火墙 service iptables stop #重启防火墙 service ipta ...

  2. JavaScript 编码小技巧

    三元操作符 如果使用if...else语句,那么这是一个很好节省代码的方式. Longhand: const x = 20; let answer; if (x > 10) { answer = ...

  3. Java学习之——Java Serializable

    1.什么是Serializable接口? http://en.wikipedia.org/wiki/Serialization Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个 ...

  4. Android SDK Content loader has encountered a problem” “parseSdkContent Failed ”

    昨天做了一个Android的小程序,调试的时候连接真实的手机,而不是用模拟器.今天早上打开eclipse的时候报错:“Android SDK Content loader has encountere ...

  5. iOS: 解决某些第三方库因为ARC不能使用的问题

    1.在target下面的build phases下有一个compile source,下面有很多待编译文件.可以看到一个compile flag,可以针对某些文件进行arc设置.这样,某些框架不能使用 ...

  6. 8 -- 深入使用Spring -- 8...1 Spring提供的DAO支持

    8.8.1 Spring提供的DAO支持. DAO模式是一种标准的Java EE设计模式,DAO模式的核心思想是,所有的数据库访问都通过DAO组件完成,DAO组件封装了数据库的增.删.查.改等原子操作 ...

  7. 8 -- 深入使用Spring -- 1...4 重写占位符配置器

    8.1.5 重写占位符配置器 (PropertyOverrideConfigurer) PropertyOverrideConfigurer是Spring提供的另一个容器后处理器.PropertyOv ...

  8. office系列调节背景主题

    更改背景主题可以参考:https://jingyan.baidu.com/article/ff42efa9332adec19e220200.html 但是这种方法只是改变了整个软件外框架的背景颜色.以 ...

  9. IIS6独立用户建立网站的方法,提高网站安全性

    在Windows server 2003系统下,用IIS架设Web服务器,合理的为每个站点配置独立的Internet来宾账号,这样可以限制Internet 来宾账号的访问权限,只允许其可以读取和执行运 ...

  10. linux系统cpu和内存占用率

    1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...