bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads

kruskal求最小生成树。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 #define re register
 8 using namespace std;
 9 typedef double ld;
10 typedef long long ll;
11 ll sqr(ll a){return a*a;}
12 #define N 1002
13 struct node{ll x,y;}a[N];
14 struct edge{
15     int f,t; ld dis;
16     edge(){}
17     edge(int A,int B,ld C):
18         f(A),t(B),dis(C)
19     {}
20     bool operator < (const edge &tmp) const{return dis<tmp.dis;}
21 }b[N*N];
22 int n,m,tp,fa[N],k; ld ans;
23 ld dist(node A,node B){return sqrt((ld)(sqr(A.x-B.x)+sqr(A.y-B.y)));}
24 int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
25 void uni(int x,int y){
26     int r1=find(x),r2=find(y);
27     if(r1!=r2) fa[r2]=r1;
28 }
29 int main(){
30     scanf("%d%d",&n,&m); int q1,q2;
31     for(re int i=1;i<=n;++i){
32         scanf("%lld%lld",&a[i].x,&a[i].y); fa[i]=i;
33         for(re int j=1;j<i;++j) b[++tp]=edge(j,i,dist(a[j],a[i]));//把可能的边存起来
34     }sort(b+1,b+tp+1);
35     for(re int i=1;i<=m;++i){
36         scanf("%d%d",&q1,&q2);
37         if(find(q1)!=find(q2)) ++k,uni(q1,q2);
38     }
39     for(re int i=1;i<=tp&&k<n-1;++i)
40         if(find(b[i].f)!=find(b[i].t)){
41             uni(b[i].f,b[i].t);
42             ans+=b[i].dis; ++k;
43         }
44     printf("%.2lf",ans);
45     return 0;
46 }

原文地址:https://www.cnblogs.com/kafuuchino/p/9862581.html

时间: 2024-08-29 11:45:59

bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads的相关文章

洛谷——P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms

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

\(problem\) 错的原因是\(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 =

[USACO07DEC]道路建设Building Roads

题目:洛谷P2872.POJ3625. 题目大意:给你n个点的坐标,有些点已经有边连通,现在要你连上剩下的所有点,求这些边的最小长度是多少(不包括原来的边). 解题思路:最小生成树,把所有边处理出来,跑Kruskal即可.注意原来有的边优先级最高且长度不加进答案.由于边的总数是$n^2$级别的,所以时间复杂度$O(n^2\log n^2)$. C++ Code: #include<cstdio> #include<cstring> #include<algorithm>

[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved: 727 [Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连.

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

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连. 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i,

BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

计算距离时平方爆了int结果就WA了一次...... ----------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<cmath

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

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1724  Solved: 725[Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连. 所

HDU 1815, POJ 2749 Building roads(2-sat)

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

hdu 1815 Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 822    Accepted Submission(s): 239 Problem Description Farmer John's farm has N barns, and there are some cows that live in each b