最小生成树应用uvalive5713

主要就是库鲁斯卡儿后边会变得有序如果你是用邻接表连得图顺序就乱了然后建的表也木用了,所以不采用邻接表建图克鲁斯卡尔的时候。易错点!!!!!

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
struct edgee
{
	int from, to;
	double value;
	bool operator<(edgee b)
	{
		return value < b.value;
	}
};
edgee edge[2000000];
int edgetot;
void addedge(int a, int b,double value)
{
	edge[edgetot].from = a;
	edge[edgetot].value = value;
	edge[edgetot].to = b;
	edgetot++;
}
struct pointt
{
	double x, y; int population;
	pointt(double x, double y, int p) :x(x), y(y), population(p)
	{}
	pointt()
	{}
};
double dist(pointt a, pointt b)
{
	return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
pointt point[1050];
vector<int>tree[1050];
vector<int>stack;
int fa[1050];
int n;
void init()
{
	for (int i = 0; i <= n; i++)fa[i] = i;
}
int find(int x)
{
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int visit[1050];
double map[1050][1050];
void dfs(int num)
{ 
	visit[num] = 1;
	stack.push_back(num);
	for (int i = 0; i < tree[num].size(); i++)
	{
		int to = tree[num][i]; double value = dist(point[to], point[num]);
		if (visit[to] == 1)continue;
		for (int j = 0; j < stack.size(); j++)
		{
			map[to][stack[j]] = map[stack[j]][to] = max(value,map[num][stack[j]]);
		}
		dfs(to);
	}
}
double kruscal()
{
     double  ans = 0;
	sort(edge, edge + edgetot);
	for (int i = 0; i < edgetot; i++)
	{
		int from = edge[i].from; int to = edge[i].to;
		int x = find(from);
		int y = find(to);
		if (x == y)continue;
		fa[x] = y;
		ans += edge[i].value;
		tree[from].push_back(to);
		tree[to].push_back(from);
	}
	return ans;
}
double getans()
{
	double all=kruscal();
	double ans = -1000000000;
	dfs(1);
	for (int i = 0; i < edgetot; i++)
	{
		//if (i > (i ^ 1))continue;
		int from = edge[i].from; int to = edge[i].to;
		if (ans < (point[from].population + point[to].population) / (all - map[from][to]))
		{
			//cout << "from:" << from << " " << "to:" << to << endl;
		//	cout << "最小生成树:" << all << " " << "两点最长边:" << map[from][to] << endl;
			//cout << "from:x" << point[from].x << " " << "from:y:" << point[from].y << " " << "to:x" << point[to].x << " " << "to:y" << point[to].y<< endl;
			ans = max(ans,(point[from].population + point[to].population) / (all - map[from][to]));
			//cout << "population:" << point[from].population + point[to].population << " " << "ans:" << ans << endl;
		}
	}
	
	return ans;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		//n = 1000;
		init();
		for (int i = 1; i <= n; i++)
		{
			 tree[i].clear(),visit[i]=0;
			for (int j = i; j <= n; j++)
			{
				map[i][j] = map[j][i] = -1;
			}
		}
		edgetot = 0; stack.clear();
		for (int i = 1; i <=n; i++)
		{
			double x, y; int p;
			//x = rand() % 1000; y = rand() % 1000; p = rand() % 99999+1;
			scanf("%lf%lf%d", &x, &y, &p);
			point[i] = pointt(x, y, p);
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = i+1; j <= n; j++)
			{
				double value = dist(point[i], point[j]);
				addedge(i, j, value);
			}
		}

		double ans = getans();
		//cout << "test:" << endl;
		//cout << "from:" << point[1].x << " " << point[1].y << " " << "to:" << point[2].x << " " << point[2].y <<"map:"<<map[1][2]<< "population:"
			//<<point[1].population+point[2].population<<endl;
		printf("%.2lf\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值