HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17913 Accepted Submission(s): 5593
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
2
10 10
20 20
3
1 1
2 2
1000 1000
oh!
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],N,M,NUM;
int MAP[SIZE][SIZE];
struct Node
{
int from,to;
double cost;
}G[SIZE * SIZE];
struct
{
int x,y;
}TEMP[SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
void kruskal(void);
bool comp(const Node &,const Node &);
double dis(int,int,int,int);
int main(void)
{
int t;
double temp; scanf("%d",&t);
while(t --)
{
scanf("%d",&N);
ini();
for(int i = ;i <= N;i ++)
scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
temp = sqrt(dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y));
if(temp >= && temp <= )
{
G[NUM].from = i;
G[NUM].to = j;
G[NUM].cost = temp * ;
NUM ++;
}
}
sort(G,G + NUM,comp);
kruskal();
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(FATHER[n] == n)
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} void kruskal(void)
{
int count = ;
double ans = ; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
count ++;
ans += G[i].cost;
if(count == N - )
break;
}
if(count == N - )
printf("%.1f\n",ans);
else
puts("oh!");
} double dis(int x_1,int y_1,int x_2,int y_2)
{
return pow(x_1 - x_2,) + pow(y_1 - y_2,);
}
HDU 1875 畅通工程再续 (最小生成树)的更多相关文章
- (step6.1.3)hdu 1875(畅通工程再续——最小生成树)
题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...
- hdu 1875 畅通工程再续(prim方法求得最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...
- HDU 1875 畅通工程再续 (prim最小生成树)
B - 畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- 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 畅通工程再续(最小生成树)
d.c个小岛,通过建立桥,使其全部可达.求所有的桥的最小长度和. s.最小生成树,数据改成double就行了 c.Prim算法:cost[a][b]和cost[b][a]都得赋值. /* Prim算法 ...
- hdu 1875 畅通工程再续(最小生成树,基础)
题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...
随机推荐
- java去掉jsp标签内容的方法
//去掉内容的标签 public static String removeTag(String count){ try { int tagCheck=-1; do { i ...
- Linux下如何用vi编辑和保存文件
vi是Linux终端下或控制台下常用的编辑器,基本的操作方式为:vi /路径/文件名 例如,vi /etc/fstab表示显示/etc/fstab文件的内容.使用键盘上的Page Up和Page Do ...
- (1) css的核心基础
css的核心基础 1.css的基本语法在具体使用css之前,请各位兄弟姐妹先思考一个生活中的问题,一般情况下我们是如何描述一个人的呢? 小明{ 民族:汉族: 性格:温柔: 性别:男: 体重:68kg ...
- AVR 定点数运算程序设计及数制转换
AVR 单片机有加法和减法指令,可以直接调用相关指令来达到目的. 这里列出了16位加法.16位带立即数加法. 16位减法.16位带立即数减法. 16位比较.16位带立即数比较程序和16位取补程序. a ...
- PostgreSQL下,对汉字按拼音排序
参考学习此文: http://blog.163.com/digoal@126/blog/static/163877040201173003547236/ 建库 postgres=# \l List o ...
- HDU 5584 LCM Walk 数学
LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...
- GLSL实现Simple Displace Mapping 水仿真流体绘制 【转】
http://blog.csdn.net/a3070173/archive/2008/11/20/3342062.aspx Dislace Mapping其实就是在顶点着色器中 对顶点进行置换偏移,经 ...
- Eclipse使用jre的原理与配置
近期要配置Eclipse环境,Mark当中的一些方法. 下载Eclipse SDK之后我们就要关联JRE,由于Eclipse启动须要JRE. Eclipse启动时寻找JRE的顺序: 1.假设eclip ...
- android安卓 SQLite教程:内部架构及SQLite使用办法
SQLite 介绍 SQLite一个非常流行的嵌入式数据库,它支持SQL语言,并且只利用很少的内存就有很好的性能.由于JDBC不适合手机这种内存受限设备,所以Android开发人员需要学习新的API ...
- 生成html的几种方案
方案1: /// <summary > /// 传入URL返回网页的html代码 /// </summary > /// <param name=&q ...