计算距离时平方爆了int结果就WA了一次......

-----------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 1000 + 5;
 
int n;
 
struct P {
int x , y;
void Read() {
scanf( "%d%d" , &x , &y );
}
};
 
P A[ maxn ];
 
double dist( P a , P b ) {
return sqrt( 1LL * ( a.x - b.x ) * ( a.x - b.x ) + 1LL * ( a.y - b.y ) * ( a.y - b.y ) );
}
 
int p[ maxn ];
 
void UF_init() {
rep( i , n )
   p[ i ] = i;
}
 
int find( int x ) {
return x == p[ x ] ? x : p[ x ] = find( p[ x ] );
}
 
bool unite( int x , int y ) {
x = find( x ) , y = find( y );
p[ x ] = y;
return x != y;
}
 
struct edge {
int u , v;
double d;
bool operator < ( const edge &e ) const {
return d < e.d;
}
};
 
vector< edge > E;
 
void E_clear() {
E.clear();
}
 
double MST() {
double res = 0;
sort( E.begin() , E.end() );
rep( i , E.size() ) {
edge &e = E[ i ];
if( unite( e.u , e.v ) )
   res += e.d;
   
}
return res;
}
 
int main() {
freopen( "test.in" , "r" , stdin );
int m;
cin >> n >> m;
UF_init();
E_clear();
rep( i , n )
   A[ i ].Read();
   
while( m-- ) {
int u , v;
scanf( "%d%d" , &u , &v );
u-- , v--;
unite( u , v );
}
rep( i , n )
   for( int j = i + 1 ; j < n ; ++j ) if( find( i ) != find( j ) )
   
    E.push_back( ( edge ) { i , j , dist( A[ i ] , A[ j ] ) } );
   
printf( "%.2lf\n" , MST() );
return 0;
}

-----------------------------------------------------------------------------------------

1626: [Usaco2007 Dec]Building Roads 修建道路

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1212  Solved: 470
[Submit][Status][Discuss]

Description

Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场)。有些农场之间原本就有道路相连。 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i, Y_i)的点(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),两个农场间道路的长度自然就是代表它们的点之间的距离。现在Farmer John也告诉了你农场间原有的M(1 <= M <= 1,000)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。

Input

* 第1行: 2个用空格隔开的整数:N 和 M

* 第2..N+1行: 第i+1行为2个用空格隔开的整数:X_i、Y_i * 第N+2..N+M+2行: 每行用2个以空格隔开的整数i、j描述了一条已有的道路, 这条道路连接了农场i和农场j

Output

* 第1行: 输出使所有农场连通所需建设道路的最小总长,保留2位小数,不必做 任何额外的取整操作。为了避免精度误差,计算农场间距离及答案时 请使用64位实型变量

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4

输入说明:

FJ一共有4个坐标分别为(1,1),(3,1),(2,3),(4,3)的农场。农场1和农场
4之间原本就有道路相连。

Sample Output

4.00

输出说明:

FJ选择在农场1和农场2间建一条长度为2.00的道路,在农场3和农场4间建一
条长度为2.00的道路。这样,所建道路的总长为4.00,并且这是所有方案中道路
总长最小的一种。

HINT

Source

BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )的更多相关文章

  1. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  2. BZOJ——1626: [Usaco2007 Dec]Building Roads 修建道路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1626 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  3. BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1626 题意: 有n个农场,坐标为(x[i],y[i]). 有m条原先就修好的路,连接农场( ...

  4. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路【最小生成树】

    先把已有的边并查集了,然后MST即可 记得开double #include<iostream> #include<cstdio> #include<algorithm&g ...

  5. 【BZOJ】1626: [Usaco2007 Dec]Building Roads 修建道路(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1626 依旧是水题..太水了.. #include <cstdio> #include & ...

  6. [Usaco2007 Dec]Building Roads 修建道路[最小生成树]

    Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...

  7. bzoj1626[Usaco2007 Dec]Building Roads 修建道路

    Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...

  8. [Usaco2007 Dec]Building Roads 修建道路

    题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...

  9. BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )

    数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...

随机推荐

  1. Ext Store Proxy Ajax

    使用Store ajax的方式来获取数据 <div id="grid1"> </div> <script> Ext.onReady(functi ...

  2. rsyslog 走tcp通讯配置

    发送端: local5.* @@192.168.32.76 front-end:/usr/local/nginx/logs# cat /etc/rsyslog.conf 日志服务器端配置: # Pro ...

  3. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  4. java开发工具比较(16个工具修订版)

    1.JDK (Java Development Kit)Java开发工具集 SUN的Java不仅提了一个丰富的语言和运行环境,而且还提了一个免费的Java开发工具集(JDK).开发人员和最终用户可以利 ...

  5. angular学习(一):动态模板总结

    近期在项目中用到了angular,之前从未用到过此js lib库,因为项目也比較着急,学习的寥寥草草.到眼下为止也仅仅是学会皮毛而已,现将自己学习的知识总结例如以下: 备注1: 版本:1.2.2 备注 ...

  6. 嵌入式系统 Boot Loader

    基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念.软件设计的主要任务以及结构框架等内容.

  7. URAL 1036(dp+高精度)

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  8. 在C++中如何使用C

    如下代码: /*C语言头文件:Max.h*/ #ifndef _MAX_H_ #define _MAX_H_ int Max(int nA,int nB) #endif /*C语言实现文件:Max.c ...

  9. Exponentiation

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  10. MySql 日期字符串类型互转

    1.data_format 日期转字符串 select date_format(Now(), '%Y-%m-%d %H:%i'); 2.str_to_date 字符串转日期 select str_to ...