uva 10369 By ACReaper



#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn_b = 755;
const int maxn_e = maxn_b * (maxn_b - 1) /2;
struct pos{
	double x,y;
};
pos A[maxn_b];
int p[maxn_b];
int u[maxn_e];
int v[maxn_e];
int r[maxn_e];
double e[maxn_e];
int find_rep(int u){
	return u == p[u]?u:(p[u]=find_rep(p[u]));
}
int cmp(const int i,const int j){
	return e[i] < e[j];
}
int main(){
	int n,m;
	//#define TEST_IN_PC
	#ifdef TEST_IN_PC
		freopen("uva10397.txt","r",stdin);
	#endif
	while(scanf("%d",&n) != EOF){
		for(int i = 1; i <= n; i++){
			scanf("%lf%lf",&A[i].x,&A[i].y);			
		}
		scanf("%d",&m);
		
		for(int i = 1; i <= maxn_e; i++)
			r[i] = i;
		
		for(int i = 1; i <= n; i++)
			p[i] = i;
		
		for(int i = 1; i <= m; i++){//修改1  
			int uu,vv;
			scanf("%d%d",&uu,&vv);
			int x = find_rep(uu);
			int y = find_rep(vv);
			if(x != y)
				p[y] = x;
		}
		int k = 1;
		for(int i = 1; i <=n; i++){//Build Graph
			for(int j = i + 1; j <= n; j++){
				double d = sqrt((A[i].x  - A[j].x)*(A[i].x  - A[j].x ) + (A[i].y  - A[j].y )*(A[i].y  - A[j].y));
				u[k] = i,v[k] = j,e[k++] = d;
			}
		}
		k--;
		sort(r + 1, r + 1 + k,cmp);//Kurasal
		double ans = 0.0;
		for(int i = 1; i <= k; i++){
			int ee = r[i];
			int uu = find_rep(u[ee]);
			int vv = find_rep(v[ee]);
			if(uu != vv){
				p[uu] = vv;
				ans += e[ee];
			}
			
		}
		printf("%.2lf\n",ans);
	}
	return 0;
}

 
//总结注意
//程序要慢慢写,保证每一步程序的语法,下标不出错。

By ACReaper

2013 05 09

你可能感兴趣的:(uva 10369 By ACReaper)