题目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 ...
随机推荐
- 每天一个linux命令:traceroute命令
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...
- 实现linux服务器之间无密码互访
最近老是在群里看到许多同学问,linux之间无密码互访怎么弄啊,百度的看得我头晕之类的,所以我就写写怎么样在两台linux服务器之间实现无密码互访,也就是让它们互相信任的意思,废话不多说,直接上菜. ...
- 【WP8】同步执行异步代码
微软的StorageFile只支持异步的方式进行文件操作,我之前也封装过一个StorageHelper,但是当所有的方法都是异步的时候也带来一些问题 1.比如我们不能在构造函数调用异步代码(等待), ...
- UNIX环境编程学习笔记(5)——文件I/O之fcntl函数访问已打开文件的性质
lienhua342014-08-29 fcntl 函数可以改变已打开的文件的性质. #include <fcntl.h> int fcntl(int filedes, int cmd, ...
- datatable删除一行方法
t.row($(e).parents('tr')[0]).remove().draw(false); t为定义的datatable对象,row里面传入当前行的DOM元素.
- mybatis启动报错Mapped Statements collection already contains value for com.autoyol.mapper.trans.TransDispatchingMapper解决
1.检查sqlsession配置,在applicationContext文件中.检查mybatis配置文件. 2.检查TransDispatchingMapper.java 是接口类,无注解. 3.T ...
- javascript关于onclick()
1 <html> <head> <title>js1 </title> <style> #content{ margin:0 auto; t ...
- 安装MySQL-python: EnvironmentError:mysql config not found
1执行 sudo yum install python-devel 2 find / -name mysql_config 在/usr/bin/下发现了这个文件 3. 后修改MySQL-python- ...
- go类型系统
https://blog.csdn.net/hittata/article/details/50915496 https://blog.csdn.net/hittata/article/details ...
- Java实现在复制文件时使用进度条
在对大文件操作时,可能会需要些时间,此时为用户提供进度条提示是非常常见的一项功能,这样用户就可以了解操作文件需要的时间信息.本实例为大家介绍了在复制大的文件时使用的进度条提示,需要注意的是,只有在读取 ...