题目链接: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. win10专业版激活方法

    slmgr.vbs /upk 此时弹出窗口显未“已成功卸载了产品密钥”. slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX 弹出窗口提示:“成功的安装了产品密钥”. s ...

  2. Java项目性能持续优化中……

     尽量使用StringBuilder和StringBuffer进行字符串连接, 参考链接: Java编程中“为了性能”尽量要做到的一些地方

  3. 搭建交叉调试环境 arm-linux-gdb配合gdbserver

        在嵌入式开发中,有时候需要进行源码级别的调试,可以设置断点,单步执行,相比于每步打印printf或者printk来说,更加友好.下面就来介绍这种调试方法.     gdb交叉调试类似于网络浏览 ...

  4. 有关maven不能加载ojdbc14.jar解决方法

    首先下载ojdbc14-10.2.0.4.0.jar这个包,然后在cmd下输入以下 mvn install:install-file -DgroupId=com.oracle -DartifactId ...

  5. VBscript实现开机自动启动,自动复制原件后启动

    set fso = createobject("scripting.filesystemobject") set ws = createobject("wscript.s ...

  6. PDF文件转换成Excel表格的操作技巧

    我们都知道2007以上版本的Office文档,是可以直接将文档转存为PDF格式文档的.那么反过来,PDF文档可以转换成其他格式的文档吗?这是大家都比较好奇的话题.如果可以以其他格式进行保存,就可以极大 ...

  7. Openlayers 3计算长度和面积

    1.比较粗糙的计算方式 计算长度 var length = lineFeature.getGeometry().getLength(); if (length > 1000) { length ...

  8. tomcat设置debug模式

    1.设置 编辑catalina.bat,在 rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not & ...

  9. 查询SQL阻塞语句

    SELECT SPID=p.spid, DBName = convert(CHAR(),d.name), ProgramName = program_name, LoginName = convert ...

  10. 关联Left Outer Join的第一条记录

    数据准备 CREATE TABLE person (person_id ), lastname )) / INSERT ALL INTO person (person_id, firstname, l ...