$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;
}
随机推荐
- Android自己定义圆角ImageView
我们常常看到一些app中能够显示圆角图片.比方qq的联系人图标等等,实现圆角图片一种办法是直接使用圆角图片资源,当然假设没有圆角图片资源.我们也能够自己通过程序实现的,以下介绍一个自己定义圆角Imag ...
- 用NHibernate处理带属性的多对多关系
1.引言 老谭在面试开发者的时候,为了考察他们的数据库开发能力,经常祭出我的法宝,就是大学数据库教程中讲到的一个模式:学生选课.这个模式是这种: 在这个模式中,学生(Student)和课程(Cours ...
- TreeSet实现Comparator接口的排序算法的分析
为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...
- OOalv 实现带出栏位描述
.类定义 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR E ...
- css盒子模型详解一
什么是CSS的盒子模式呢?为什么叫它是盒子?先说说我们在网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS盒子模式都具备这些属 ...
- JavaScript函数调用规则
1. [代码][JavaScript]代码 JavaScript函数调用规则一 (1)全局函数调用:function makeArray( arg1, arg2 ){ return [t ...
- [Selenium] waitUntilAllAjaxRequestCompletes
private static final String JQUERY_ACTIVE_CONNECTIONS_QUERY = "return $.active == 0;"; pri ...
- 【ZJOI 2008】树的统计
[题目链接] 点击打开链接 [算法] 树链剖分模板题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 3000 ...
- 【Codeforces 632D】 Longest Subsequence
[题目链接] 点击打开链接 [算法] 设取的所有数都是k的约数,则这些数的lcm必然不大于k. 对于[1, m]中的每个数,统计a中有多少个数是它的约数即可. [代码] #include<bit ...
- codevs1258关路灯
传送门 1258 关路灯 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他 ...