题目大意:本题是中文题,可以直接在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(畅通工程再续——最小生成树)的更多相关文章

  1. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. HDU 1875 畅通工程再续 (prim最小生成树)

    B - 畅通工程再续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  3. hdu 1875 畅通工程再续(prim方法求得最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...

  4. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...

  5. HDU 1875 畅通工程再续(kruskal)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  6. HDU 1875 畅通工程再续 (Prim)

    题目链接:HDU 1875 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现 ...

  7. HDU - 1875 畅通工程再续

    Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问 ...

  8. HDU - 1875 畅通工程再续【最小生成树】

    Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖 ...

  9. hdu 1875 畅通工程再续(最小生成树,基础)

    题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...

随机推荐

  1. POJ-2955括号匹配问题(区间DP)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4834   Accepted: 2574 Descript ...

  2. windows任务栏消失

    windows任务栏消失,快捷键打开任务管理器,新建任务explorer.exe

  3. 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack

    上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...

  4. LINQ to SQL 增,删,改

    添加   InsertOnSubmit(单个对象)  或  InsertAllOnSubmit(集合) 删除   DeleteOnSubmit (单个对象)             DeleteAll ...

  5. CGBitmapContextCreate函数

    CGBitmapContextCreate函数参数详解 函数原型: CGContextRef CGBitmapContextCreate ( void *data,    size_t width, ...

  6. 自增运算a++和++b(1)

    #include<reg52.h> #define uint unsigned int #define uchar unsigned char uchar code f[]={0x3f,0 ...

  7. Hibernate PO对象的状态

    Hibernate的PO对象有三种状态:临时状态(又称临时态).持久状态(又称为持久态)和脱管状态(又称为脱管态.游离态).处理持久态的对象也称为PO,临时对象和脱管对象也称为VO. 1.临时态: 简 ...

  8. [CSAPP笔记][第一章计算机系统漫游]

    计算机系统漫游 我们通过追踪hello程序的生命周期来开始对系统的学习—–从它被程序员创建,到系统上运行,输出简单的消息,然后终止.我们沿着这个程序的生命周期,简要介绍一些逐步出现的概念,专业术语和组 ...

  9. linux基础内容学习一:linux下的分区及安装

    linux看系统版本信息 uname -a 如果显示为i386,i686则为32位系统,如果为x86_64则为64位 一块硬盘最多可以有四个主分区其中一个主分区可以用一个扩展分区替换,在这个扩展分区中 ...

  10. Android----------eclipse常用快捷键

    类级操作:--------------------一个去包,一个导包------------------------------------ Ctrl+shift+O (不是零) 清除没用引用 ctr ...