[BZOJ1626] [Usaco2007 Dec]Building Roads 修建道路
来源:互联网 发布:文章cms 编辑:程序博客网 时间:2024/05/21 01:46
传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=1626
题目大意
给你n个点的坐标,m条边所连的两点,输出最小连通代价
题解
最小生成树,把已经连上的点先连上,注意算两点间距离爆longint
var fa:array[0..1000]of longint; x:array[0..1000,0..1000]of longint; y:array[0..1000,1..2]of longint; z:array[0..2000000]of double; t:array[0..2000000,1..2]of longint; i,j,k:longint; n,m:longint; a,b,len:longint; ans:double;procedure sort(l,r:longint);var i,j,c:longint; a,b:double;begin i:=l; j:=r; a:=z[(l+r) div 2]; repeat while z[i]<a do inc(i); while a<z[j] do dec(j); if not(i>j) then begin b:=z[i]; z[i]:=z[j]; z[j]:=b; c:=t[i,1]; t[i,1]:=t[j,1]; t[j,1]:=c; c:=t[i,2]; t[i,2]:=t[j,2]; t[j,2]:=c; inc(i); dec(j); end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r);end;function get(a:longint):longint;begin if fa[a]=a then exit(a); fa[a]:=get(fa[a]); exit(fa[a]);end;begin readln(n,m); for i:=1 to n do readln(y[i,1],y[i,2]); fillchar(x,sizeof(x),0); for i:=1 to n do fa[i]:=i; for i:=1 to m do begin readln(a,b); x[a,b]:=1; fa[get(a)]:=get(b); end; len:=0; for i:=1 to n do for j:=1 to n do begin if (i=j)or(x[i,j]=1) then continue; inc(len); z[len]:=sqrt(int64(y[i,1]-y[j,1])*int64(y[i,1]-y[j,1])+int64(y[i,2]-y[j,2])*int64(y[i,2]-y[j,2])); t[len,1]:=i; t[len,2]:=j; end; sort(1,len); {z[i]} ans:=0; for i:=1 to len do begin a:=get(t[i,1]); b:=get(t[i,2]); if a<>b then begin ans:=ans+z[i]; fa[a]:=b; end; end; writeln(ans:0:2);end.
0 0
- bzoj1626[Usaco2007 Dec]Building Roads 修建道路
- [BZOJ1626] [Usaco2007 Dec]Building Roads 修建道路
- bzoj1626【Usaco2007 Dec】Building Roads 修建道路
- bzoj1626 [Usaco2007 Dec]Building Roads 修建道路(kruskal裸题)
- [Usaco2007 Dec]Building Roads 修建道路
- [Usaco2007 Dec]Building Roads 修建道路
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路
- 1626: [Usaco2007 Dec]Building Roads 修建道路 (生成树)
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路
- bzoj 1626[Usaco2007 Dec]Building Roads 修建道路
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路 kruskal
- Cpp环境【BZOJ1626】【Usaco2007 Dec】【Vijos 1693】 修建道路
- [bzoj1626][Usaco2007 Dec][最小生成树]修建道路
- 最小生成树-BZOJ-1626-[Usaco2007 Dec]Building Roads 修建道路
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路(最小生成树)
- USACO 建造道路 Building Roads
- USACO 2007 Dec Silver 2.Building Roads
- 道路修建
- iOS 数据请求 get、post
- Java开发环境的配置(Windows)
- 大道至简:朴素贝叶斯分类器
- 基础-输入输出1
- 利用数组实现大数的阶乘
- [BZOJ1626] [Usaco2007 Dec]Building Roads 修建道路
- git学习笔记 -创建仓库、更新代码
- 动态代理
- 编辑神器sublime中使用Emmet的实例
- BestCoder Round #59 (div.2) HDU5499 SDOI 排序+贪心
- Jsoup中getElementsByClass中className有空格处理
- 迭代
- Slmgr.vbs使用参数详解
- Objective-C中的SEL