(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 ...
随机推荐
- hdu4536-XCOM Enemy Unknown(爆搜)
XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...
- 【HDU 4612 Warm up】BCC 树的直径
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...
- Python Open Flash Chart (pyOFC2) — Home
Python Open Flash Chart (pyOFC2) - Home pyOFC2 Python Open Flash Chart 2
- nyoj 36 最长公共子序列
描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subseque ...
- XSS第二节,XSS左邻右舍
昨天的文章中引用了OWASP2013年的江湖排名,今天来看一下TOP中XSS的左邻右舍都是谁,先看一下他们的大名,再进一步介绍 [以下主要翻译自https://www.owasp.org/index. ...
- Linux 终端訪问 FTP 及 上传下载 文件
今天同事问我一个问题,在Linux 下訪问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上. google 一下. ...
- Oracle 10g轻量级客户端安装[转]
http://www.oracle.com/technetwork/cn/topics/winsoft-095945-zhs.html oracle技术官方网 http://www.oracle.co ...
- [RxJS] Using Observable.create for fine-grained control
Sometimes, the helper methods that RxJS ships with such as fromEvent, fromPromise etc don't always p ...
- fedora 20下安装vim的C++补全插件clang_complete
1.安装clang yum install clang 2.安装clang_complete插件 git clone https://github.com/Rip-Rip/clang_complete ...
- 使用 Require.js 引用第三方框架时遇到的一些情况
使用 Require.js 引用第三方框架时遇到的一些情况 在使用Require.js解析依赖的时候,会出现以下几种情况: 程序中的依赖关系 当前程序 依赖于 B包, B包 依赖于 A包 A包与B包两 ...