uva 10397 Connect the Campus kruskal 算法变形

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstring>

#define INF 100000000
using namespace std;
int n,m;
int x[1005];
int y[1005];
int fa[1005];

struct node{
	int x,y,w;

	bool operator < (const node& a)const{
		return w > a.w;
	}
};

int fun(int x){
	return fa[x] == x ? x : fa[x] = fun(fa[x]);
}
int main(){
	while(cin >> n){
		for(int i = 1;i <= n;i++){
			scanf("%d%d",&x[i],&y[i]);
		}
		priority_queue<node> que;

		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= n;j++){
				if(i == j) continue;
				node a;
				a.x = i;
				a.y = j;
				a.w = (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
				que.push(a);

			}
		}

		for(int i = 1;i <= n;i++){
			fa[i] = i;
		}
		int m;
		cin >> m;

		for(int i = 0;i < m;i++){
			int a,b;

			scanf("%d%d",&a,&b);
			fa[fun(a)] = fun(b);
		}
		double ret = 0;

		while(!que.empty()){
			node a = que.top();
			que.pop();
		 	if(fun(a.x) != fun(a.y)){
				ret += sqrt(a.w);
				fa[fun(a.x)] = fun(a.y);
		 	}
		}

		printf("%.2f\n",ret);

	}
	return 0;
}

时间: 2024-09-30 16:27:00

uva 10397 Connect the Campus kruskal 算法变形的相关文章

uva 10397 Connect the Campus kruskal || prim

uva上的题目总是要难一些的...总是会拐弯抹角,这道题目给出有的建筑之间已经建好了光缆,让求最小生成树,我还是比较喜欢用kruskal算法,但是这道题目肯定用prim比较快,prim复杂度是n^2,kruskal复杂度eloge. 用kruskal时需要预先用并查集处理一下已经建好的光缆,让他们属于同一个祖先: 用prim算法的时候需要把他们的边置为0,这样算sum的时候就不会加上了. 代码:(kruskal) #include<iostream> #include<cstdio>

uva 10397 - Connect the Campus

Problem E Connect the Campus Input: standard input Output: standard output Time Limit: 2 seconds Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and

uvaoj 10397 - Connect the Campus【最小生成树】

uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been

Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0.然后进行一遍prim(2)Kruskal算法+并查集 代码: //prim写法 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17167    Accepted Submission(s): 4335 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

UVA 10369(求第K长的边,kruskal算法模板)

Arctic Networks Time Limit: 3000 MS The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will ha

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e