BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路 (MST)
1626: [Usaco2007 Dec]Building Roads 修建道路
Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1362 Solved: 541 [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,并且这是所有方案中道路 总长最小的一种。
思路:最小生成树,先把给的边处理了,然后添加所有没有给的边作为备选边,做最小生成树。
因为算距离往写sqrt 而WA了一发。。。。蠢哭
1/* ***********************************************
2Author :111qqz
3Created Time :2016年04月04日 星期一 13时49分48秒
4File Name :code/bzoj/1626.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <iomanip>
18#include <cstdlib>
19#include <ctime>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34const int N=1E3+7;
35int f[N];
36int n,m;
37bool conc[N][N];
38struct node
39{
40 long double x,y;
41
42 void input()
43 {
44 cin>>x>>y;
45 }
46
47 long double dis(node p)
48 {
49 long double res;
50 res = (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);
51 res = sqrt(res);
52 return res;
53 }
54}p[N];
55
56struct edge
57{
58 int u,v;
59 long double w;
60
61 bool operator < (edge b)const
62 {
63 return w<b.w;
64 }
65
66}e[N*N];
67int root (int x)
68{
69 if (x!=f[x]) f[x] = root (f[x]);
70 return f[x];
71}
72
73void merge( int x,int y)
74{
75// cout<<"x:"<<x<<" y:"<<y<<endl;
76 int rx = root (x);
77 int ry = root (y);
78 if (rx!=ry)
79 {
80 f[rx]=ry;
81 }
82}
83int main()
84{
85 #ifndef ONLINE_JUDGE
86 freopen("code/in.txt","r",stdin);
87 #endif
88
89 cin>>n>>m;
90 for ( int i = 1 ;i <= n ; i++) p[i].input();
91 for ( int i = 1 ; i < N ; i++) f[i] = i;
92 ms(conc,false);
93 int total = 0 ;
94 for ( int i = 1 ; i <= m ; i++)
95 {
96 int u,v;
97 cin>>u>>v;
98 conc[u][v] = true;
99 conc[v][u] = true;
100 if (root(u)==root(v)) continue;
101 merge(u,v);
102 total++;
103 }
104
105 int cnt = 0 ;
106 for ( int i = 1 ; i <= n ; i++)
107 for ( int j = i+1 ; j <= n ; j++)
108 if (!conc[i][j])
109 {
110 ++cnt;
111 e[cnt].u = i;
112 e[cnt].v = j;
113 e[cnt].w = p[i].dis(p[j]);
114 }
115
116 sort(e+1,e+cnt+1);
117// cout<<"cnt:"<<cnt<<endl;
118 long double res = 0 ;
119 for ( int i = 1 ; i <= cnt ;i++)
120 {
121 if (root(e[i].u)==root(e[i].v)) continue;
122 merge(e[i].u,e[i].v);
123 total++;
124 res +=e[i].w;
125 // cout<<"res:"<<res<<endl;
126
127 if (total>=n-1) break;
128 }
129
130 cout<<fixed<<setprecision(2)<<res<<endl;
131
132
133
134 #ifndef ONLINE_JUDGE
135 fclose(stdin);
136 #endif
137 return 0;
138}