(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 ...
随机推荐
- oracle 12g sqlplus安装
一.下载oracle 12g sqlplus软件 linux 64位操作系统,oracle安装包地址 http://www.oracle.com/technetwork/topics/linuxx86 ...
- Windows Live Writer 代码插件改造
源码和插件都在后面,如果不想看我神神叨叨的可以直接到文章后面下载 一 .找插件 在使用Windows Live Writer 经常要用到插入代码的功能,根据博客园中教程,分别使用了: WindowsL ...
- Subsequence(两个单调队列)
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- CAS原理与协议
SSO英文全称Single Sign On,单点登录. SSO是在多个应用系统中,用户仅仅须要登录一次就能够訪问全部相互信任的应用系统. SSO的解决方式非常多,比方收费的有UTrust.惠普灵动等, ...
- php 依据字符串生成相应数组方法
php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...
- Objective-C(十八、谓语使用及实例说明)——iOS开发基础
结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结.知识点一直在变,仅仅是作为參考,以苹果官方文档为准~ 十八.谓语的使用及 ...
- css学习之color: window和color: currentColor
一.易被忽略的属性 color: currentColor color: window 看完之后感觉眼前一亮,有的我之前根本没有用过,甚至都不知道有color: currentColor这么个东西 ...
- javascript 之 this 用法
参考视频:http://www.imooc.com/video/6430 JavaScript中的this比较灵活,也是让很多初学者摸不到头脑,那么根据在不同的环境下,在同一个函数,不同的调用方式下, ...
- java 去除重复项
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Demo20 { public stati ...
- COMException 依赖服务或组无法启动(0x8007042C)处理办法
问题分析:这个问题主要原因是由于服务列表中的windows management instrumentation这个服务无法启动 问题解决办法: 点击屏幕左下角:开始-运行-输入regedit 打开注 ...