题目1144:Freckles(最小生成树进阶)
题目链接: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(最小生成树进阶)的更多相关文章
- 【九度OJ】题目1144:Freckles 解题报告
[九度OJ]题目1144:Freckles 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1144 题目描述: In an ...
- UVA 10034 Freckles 最小生成树
虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...
- Freckles (最小生成树)
#include<iostream> #include<cstring> #include<stdio.h> #include<queue> #incl ...
- hdu 4253(经典题目:二分+最小生成树)
题意:就是说有A.B两个公司要修路,有m条路,可能是属于A修的,也可能是属于B修的,现在要求所有路都联通的情况下的最小权值,并且A公司必须要修k条路. 同: 代码: #include<iostr ...
- UVA 1151 买还是建(最小生成树)
买还是建 紫书P358 [题目链接]买还是建 [题目类型]最小生成树 &题解: 这题真的心累,看了3天,最后照着码还是wa,先放lrj代码,以后再看吧 &代码: // UVa1151 ...
- UVA 1395 苗条的生成树(最小生成树+并查集)
苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- 软工+C(2017第1期) 题目设计、点评和评分
// 下一篇:分数和checklist 如何设计题目 教学中的一个问题是老师出题太简单了,题目设计一开始上来就不紧凑,我认为一个好的课程应该上来就给你紧凑感,而不是先上来"轻松2-3周&qu ...
- JVM菜鸟进阶高手之路十四:分析篇
转载请注明原创出处,谢谢! 题目回顾 JVM菜鸟进阶高手之路十三,问题现象就是相同的代码,jvm参数不一样,表现的现象不一样. private static final int _1MB = 1024 ...
随机推荐
- C# 反射(GetType) 获取动态Json对象属性值的方法
之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...
- Yii2 session的使用方法(2)
yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...
- MATLAB出现:错误使用 xlsread (line 251)
背景: matlab 2015b和Excel2003 方法: 改Excel的Com加载项 步骤: 1 2 3 4
- 输出到网页前台js代码中包含单引号的处理方法
描述:后台输出js到前台,如 <script type="text/javascript"> //<![CDATA[ var aStepD ...
- BarTender表单的人性化设计—分组框
BarTender 2016已经支持用户输入信息.从相同位置查询数据库和筛选数据记录,那就是数据输入表单了.这个功能相信迎合了很多用户的需求,主要作用体现在打印时数据输入. 对于表单的设计,不同的客户 ...
- MLE、MAP、贝叶斯三种估计框架
三个不同的估计框架. MLE最大似然估计:根据训练数据,选取最优模型,预测.观测值D,training data:先验为P(θ). MAP最大后验估计:后验概率. Bayesian贝叶斯估计:综合模型 ...
- SDK Manager.exe和AVD Manager.exe缺失,Android SDK Tools在检查java环境时卡住了,未响应卡死!
之前安装Android Studio的时候根据提示安装了Android SDK,但是发现目录下没有SDK Manager.exe和AVD Manager.exe,导致SDK的一些操作很不方便! 不知道 ...
- mysql中查看数据库的版本,什么版本
需求:查看当前使用的数据库是哪个版本的,什么版本 select version(); 查询结果: 备注:通过version()函数查询出来当前使用的数据库版本是5.5.57-log 文档创建时间:20 ...
- repo_folder
-- Create table create table REPO_FOLDER ( UUID ) not null, VALID ) not null, CREATE_TIME ) not null ...
- ios开发之--多个按钮单选效果
开发项目时,有很多场景需要用到按钮单选效果,例如充值页面,选择标签页面等,具体实现代码如下: 1,创建 -(UIView *)headerView { CGFloat width = (Kscreen ...