(step6.1.3)hdu 1875(畅通工程再续——最小生成树)
题目大意:本题是中文题,可以直接在OJ上看
解题思路:最小生成树
1)本题的关键在于把二维的点转化成一维的点
for (i = 0; i < n; ++i) {
scanf("%d%d", &point[i].x, &point[i].y);
point[i].id = i;
}
2)可用边的计算
int count = 0;
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
double distances = getDistance(point[i],point[j]); if (distances >= 10.0 && distances <= 1000.0) {
e[count].begin = point[i].id;
e[count].end = point[j].id;
e[count].weight = distances;
count++;
}
}
}
代码如下:
/*
* 1875_3.cpp
*
* Created on: 2013年8月26日
* Author: Administrator
*/ #include <iostream>
#include <math.h>
using namespace std; struct edge {
int begin;
int end;
double weight;
}; struct Point {
int x;
int y;
int id;
}; const int maxn = 6000;
int father[maxn];
edge e[maxn]; int find(int a) {
if (a == father[a]) {
return a;
} father[a] = find(father[a]);
return father[a];
} double kruscal(int count) {
int i;
double sum = 0;
for (i = 0; i < maxn; ++i) {
father[i] = i;
} for (i = 0; i < count; ++i) {
int fx = find(e[i].begin);
int fy = find(e[i].end); if (fx != fy) {
father[fx] = fy;
sum += e[i].weight;
}
}
return sum;
} bool compare(const edge& a, const edge& b) {
return a.weight < b.weight;
} double getDistance(const Point& a, const Point& b) {
return sqrt(
(double) ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
} int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int i, j;
memset(father, 0, sizeof(father));
for (i = 0; i < n; ++i) {
e[i].begin = -1;
e[i].end = -1;
e[i].weight = 0;
}
Point point[n + 1]; for (i = 0; i < n; ++i) {
scanf("%d%d", &point[i].x, &point[i].y);
point[i].id = i;
} int count = 0;
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
double distances = getDistance(point[i],point[j]); if (distances >= 10.0 && distances <= 1000.0) {
e[count].begin = point[i].id;
e[count].end = point[j].id;
e[count].weight = distances;
count++;
}
}
} sort(e,e + count,compare);
double result = kruscal(count); int num = 0;
for (i = 0; i < n; ++i) {
if (father[i] == i) {
num++;
}
} if (num == 1) {
printf("%.1lf\n", result * 100);
} else {
printf("oh!\n");
}
}
}
(step6.1.3)hdu 1875(畅通工程再续——最小生成树)的更多相关文章
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1875 畅通工程再续 (prim最小生成树)
B - 畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- hdu 1875 畅通工程再续(prim方法求得最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...
- HDU 1875 畅通工程再续(kruskal)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1875 畅通工程再续 (Prim)
题目链接:HDU 1875 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现 ...
- HDU - 1875 畅通工程再续
Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问 ...
- HDU - 1875 畅通工程再续【最小生成树】
Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖 ...
- hdu 1875 畅通工程再续(最小生成树,基础)
题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...
随机推荐
- JAVA大整数傻瓜入门
http://blog.csdn.net/skiffloveblue/article/details/7032290..先记着
- ps&&/proc/pid/xxx
ps 如果想看一个进程的启动时间,可以用lstart来看 [root@jiangyi02.sqa.zmf /home/ahao.mah] #ps -eo pid,lstart,etime,cmd |g ...
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带 ...
- debian下编译libev库
系统为Linux debian 2.6.32-5-686.这是裸系统,连xwindows都没有.帐户为root,不是的注意一下权限.这里想说明安装过程及出现的问题,故打印的信息较多,以供出现错误的读者 ...
- 关于NetBeans IDE的配置优化
首先,IDE的版本最好对应着JDK的版本. NetBeans优化的目的是提高NetBeans的启动速度和运行速度.下面介绍的NetBeans优化技巧是在版本6.0beta2上的优化.经过实验,大大提高 ...
- 【SVN Working copy is too old (format 10, created by Subversion 1.6)】解决方式
SVN同步或者提交的时候出现类似错误信息: The working copy needs to be upgraded svn: Working copy 'D:\adt-bundle-windows ...
- html5lib-python doc
http://html5lib.readthedocs.org/en/latest/ By default, the document will be an xml.etree element ins ...
- Qt导出Excel的简单实现
QAxObject对COM对象进行了封装,QAxObject派生自QAxBase,而后者提供了一组API通过IUnknown(不清楚IUnknown的同学可以去看看COM对象模型)指针直接访问COM对 ...
- 《JavaScript 闯关记》之作用域和闭包
作用域和闭包是 JavaScript 最重要的概念之一,想要进一步学习 JavaScript,就必须理解 JavaScript 作用域和闭包的工作原理. 作用域 任何程序设计语言都有作用域的概念,简单 ...
- iOS UIWebView 访问https 绕过证书验证的方法
在文件开始实现 allowsAnyHTTPSCertificateForHost 方法 @implementation NSURLRequest (NSURLRequestWithIgnoreSSL ...