hdu1875 畅通工程再续 暴力+基础最小生成树
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = ;
const int L = ;
const int R = ;
int t, n, m, ans;
double total;
struct node
{
int x, y;
int cost;
}pot[maxn];
int fa[maxn];
int px[maxn], py[maxn]; int cal()
{
int i, j, tx, ty, len, pos;
pos = ;
for (i = ; i<n; i++)
{
for (j = i + ; j<n; j++)
{
tx = px[i] - px[j];
ty = py[i] - py[j];
len = tx*tx + ty*ty;
if (len >= L&&len <= R)
{
pot[pos].x = i;
pot[pos].y = j;
pot[pos].cost = len;
pos++;
}
}
}
return pos;
} void init()
{
total = 0.0;
ans = n - ;
for (int i = ; i <= n; i++)
{
fa[i] = i;
}
} bool cmp(node a, node b)
{
return a.cost<b.cost;
} int find(int x)
{
if (x != fa[x])
return fa[x] = find(fa[x]);
return fa[x];
} int join(int x, int y)
{
int flag = ;
if (x != y)
{
ans--;
fa[x] = y;
flag = ;
}
return flag;
} int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
init();
for (int i = ; i<n; i++)
{
scanf("%d %d", &px[i], &py[i]);
}
int len = cal();
if (len<n - )
{
printf("oh!\n");
continue;
}
sort(pot, pot + len, cmp);
for (int i = ; i<len; i++)
{
int fx, fy;
fx = find(pot[i].x);
fy = find(pot[i].y);
if (join(fx, fy) == )
{
total += sqrt(pot[i].cost*1.0);
}
}
if (ans != )
{
printf("oh!\n");
continue;
}
printf("%.1lf\n", total * );
}
return ;
}
hdu1875 畅通工程再续 暴力+基础最小生成树的更多相关文章
- HDU1875——畅通工程再续(最小生成树:Kruskal算法)
		畅通工程再续 Description相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当 ... 
- hdu1875  畅通工程再续 最小生成树并查集解决---kruskal
		http://acm.hdu.edu.cn/showproblem.php?pid=1875 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ... 
- HDU 1875 	畅通工程再续 (prim最小生成树)
		B - 畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ... 
- HDU1875 畅通工程再续 (并查集)
		畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ... 
- HDU1875 畅通工程再续                                                                                            2017-04-12 19:52             48人阅读              评论(0)              收藏
		畅通工程再续 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ... 
- HDU-1875 畅通工程再续(最小生成树+判断是否存在)
		http://acm.hdu.edu.cn/showproblem.php?pid=1875 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛 ... 
- hdu1875 畅通工程再续 并查集/最小生成树
		相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ... 
- HDU1875 畅通工程再续【最小生成树】
		题意: 在这些小岛中建设最小花费的桥,但是一座桥的距离必须在10 -- 1000之间. 思路: 用最小生成树解决吧,就那两个算法. 代码: prim #include <iostream> ... 
- HDU1875 畅通工程再续
		相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ... 
随机推荐
- Tomcat配置,Myeclipse破解和各种设置
			转自:http://www.cnblogs.com/tyjsjl/archive/2006/11/14/2156111.html 根据tomcat来配置eclipse和MyEclipse结合使用起来, ... 
- 如何删除ini文件中的内容
			1.删除子项值:::WritePrivateProfileString(分区名称, 子项名称, "", ini文件路径); 2.删除子项(名称和值):::WritePrivateP ... 
- Qt5官方demo解析集13——Qt Quick Particles Examples - Image  Particles
			本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文 Qt5官方demo解析集12--Qt Quic ... 
- XML-RPC  JSON-RPC  RPC是实现思路
			XML-RPC - Wikipedia https://en.wikipedia.org/wiki/XML-RPC JSON-RPC - Wikipedia https://en.wikipedia. ... 
- VS1053 datasheet 解读笔记
			pdf 10 pdf 11 ` 左右声道 负载,注意这里的负载指LEFT 到GBUF,RIGHT到GBUF.所以后面会有说明GBUF一定不能连接到GND. pdf 14 pdf 14 从上面的引脚定 ... 
- 如何使用git 生成patch 和打入patch【转】
			本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ... 
- include <ctype.h> 头文件包含函数总结
			里面包含的函数主要是: 1.字符测试函数,函数原型一般为:int isXXXX( int ); 参数为int, 只能正确处理[0, 127]. 2.字符映射函数,函数原型一般为:int toXXXX( ... 
- POJ1077  Eight —— 反向BFS
			主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ... 
- svn服务器搭建与迁移
			2016-11-21更新: 今天被svn的钩子搞了半天,网上找解决方法都无效,下午被我试出来了,特此记录. 在svn的钩子中可以使用update来更新配置文件,比如ansible的,puppet的,具 ... 
- Spring Boot2.0之 整合Redis事务
			Redis事物 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命 ... 
