$P2872\ [USACO07DEC]道路建设Building\ Roads$

错的原因是\(RE\)(大雾 , 时刻谨记 \(N\) 个地方的话 保守开 \(\frac{N^2}{2}\) 大小。 因为是边。
边最多的情况即完全图 : $1+2+3+4...+n = \frac{N*(N-1)}{2} $
所以还是个板子。
忽略丑陋的\(2^{18}\)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL read () { LL res = 0 ;int f (1) ;char ch = getchar ();
while (!isdigit(ch)) { if (ch == '-') f = -1 ;ch = getchar();}
while (isdigit(ch)) res = (res << 1) + (res << 3) + (ch ^ 48) ,ch = getchar(); return res * f ;
}
const int Maxn = 1<<20 ;
int n , m ;
int x[Maxn];
int y[Maxn];
int f[Maxn];
int cnt = 0 ;
int top = 0 ;
double ans = 0.0 ;
struct P{ int x,y; double val;};
P a[Maxn] ;
inline bool cmp_(P a,P b) {
if(a.val==b.val) return a.x<b.x;
return a.val<b.val;
}
inline int find ( int x ) { return x == f[x] ? f[x] : f[x] = find(f[x]) ; }
inline void merge( int x , int y) {
f[ find(x) ] = find (y) ;
}
inline void kruskal() {
int count = 0 ;
sort(a+1,a+cnt+1,cmp_) ;
for(register int i=1;i<=cnt;i++) {
count ++ ;
if(find(a[count].x) != find(a[count].y)) ans += a[i].val , merge(a[i].x,a[i].y) ;
//if(count == n-1) break ;
}
return ;
}
signed main () {
n=read(); m=read();
for(register int i=1;i<=n;i++) {
x[i]=read(),y[i]=read();
}
for(register int i=1;i<=n;i++) {
f[i]=i;
}
for(register int i=1; i<=n; i++)
for(register int j=i+1; j<=n; j++) {
cnt ++ ;
a[cnt].x=i; a[cnt].y=j;
a[cnt].val=sqrt(pow((x[i]-x[j]),2)+pow((y[i]-y[j]),2));
}
for(register int i=1;i<=m;i++) {
int A=read(),B=read();
cnt ++ ;
a[cnt].x = A ; a[cnt].y = B ; a[cnt].val = 0.0 ;
}
kruskal() ;
printf("%.2lf\n",ans) ;
return 0;
}
随机推荐
- HDU 1022 Train Problem I (数据结构 —— 栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- vue 计算属性与侦听器
侦听器:顾名思义,就是用来监听数据变化用的.侦听器在vue实例中,定义变量watch来使用.监听新值newVal和旧值oldVal,具体使用方法如下: <!DOCTYPE html> &l ...
- BZOJ 2244: [SDOI2011]拦截导弹 DP+CDQ分治
2244: [SDOI2011]拦截导弹 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度.并且能够拦截 ...
- HashMap存入大量数据是否要预定义存储空间
按说HashMap的负载极限为0.75,可是,测试程序并看不出这个结果.待探讨 测试程序如下: 根据结果看不出来预定义有什么影响. public class test { public static ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...
- SDUT OJ 2892 A (字典树问题-输出出现次数最多的字符串的出现次数,60ms卡时间,指针+最后运行完释放内存)
A Time Limit: 60ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给出n(1<= n && n <= 2*10^6)个字 ...
- linux初级学习笔记六:linux用户及权限详解!(视频序号:03_4)
本节学习的命令:/etc/passwd,/etc/shadow,/etc/group文件详解 本节学习的技能: 安全上下文 文件与目录的权限管理 影子命令 用户,用户组类别详解 /etc/passwd ...
- myeclipse8.6注册码
loveyLR8ZC-855550-69545856608357821
- Python小练习_将数据库中表数据存到redis里
# ##练习:将xxx数据库中my_user表中数据存到redis里面# 分析: pymysql.json.redis# 1.连接数据库,查到数据库里面所有的数据,游标类型要用pymysql.curs ...
- Vue安装及插件Vue Devtools
vue安装: # 最新稳定版 $ npm install vue # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新 ...