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了一发。。。。蠢哭

/* ***********************************************
Author :111qqz
Created Time :2016年04月04日 星期一 13时49分48秒
File Name :code/bzoj/1626.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <iomanip>
12#include <cstdlib>
13#include <ctime>
14#define fst first
15#define sec second
16#define lson l,m,rt<<1
17#define rson m+1,r,rt<<1|1
18#define ms(a,x) memset(a,x,sizeof(a))
19typedef long long LL;
20#define pi pair < int ,int >
21#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E3+7;
 7int f[N];
 8int n,m;
 9bool conc[N][N];
10struct node
11{
12    long double x,y;
1    void input()
2    {
3	cin>>x>>y;
4    }
1    long double dis(node p)
2    {
3	long double res;
4	res = (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);
5	res = sqrt(res);
6	return res;
7    }
8}p[N];
1struct edge
2{
3    int u,v;
4    long double  w;
1    bool operator < (edge b)const
2    {
3	return w<b.w;
4    }
1}e[N*N];
2int root (int x)
3{
4    if (x!=f[x]) f[x] = root (f[x]);
5    return f[x];
6}
 1void merge( int x,int y)
 2{
 3//    cout<<"x:"<<x<<" y:"<<y<<endl;
 4    int rx = root (x);
 5    int ry = root (y);
 6    if (rx!=ry)
 7    {
 8	f[rx]=ry;
 9    }
10}
11int main()
12{
13	#ifndef  ONLINE_JUDGE 
14	freopen("code/in.txt","r",stdin);
15  #endif
 1	cin>>n>>m;
 2	for ( int i = 1 ;i <= n ; i++) p[i].input();
 3        for ( int i = 1 ; i < N ; i++) f[i] =  i;	
 4	ms(conc,false);
 5	int total = 0 ;
 6	for ( int i = 1 ; i <= m ; i++)
 7	{
 8	    int u,v;
 9	    cin>>u>>v;
10	    conc[u][v] = true;
11	    conc[v][u] = true;
12	    if (root(u)==root(v)) continue;
13	    merge(u,v);
14	    total++;
15	}
 1	int cnt = 0 ;
 2	for ( int i = 1 ; i <= n ; i++)	
 3	    for ( int j = i+1 ; j <= n ; j++)
 4		if (!conc[i][j])
 5		{
 6		    ++cnt;
 7		    e[cnt].u = i;
 8		    e[cnt].v = j;
 9		    e[cnt].w = p[i].dis(p[j]);
10		}
 1	sort(e+1,e+cnt+1);
 2//	cout<<"cnt:"<<cnt<<endl;
 3	long double res = 0 ;
 4	for ( int i = 1 ; i <= cnt ;i++)
 5	{
 6	    if (root(e[i].u)==root(e[i].v)) continue;
 7	    merge(e[i].u,e[i].v);
 8	    total++;
 9	    res +=e[i].w;
10	  //  cout<<"res:"<<res<<endl;
	    if (total>=n-1) break;
	}

	cout<<fixed<<setprecision(2)<<res<<endl;
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}