hdu 4081 && uva 1494 && LA 5713 Qin Shi Huang's National Road System

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int maxn = 1010;
int n;
struct Point
{
	int x,y;
	int p;
	Point(const int& x = 0,const int& y = 0,const int& p =0 ):x(x),y(y),p(p){}
	Point(const Point& rsh)
	{
		this->x = rsh.x;
		this->y = rsh.y;
		this->p = rsh.p;
	}
};

vector<Point>points;

struct Edge
{
	int from,to;
	double w;
	Edge(const int& from = 0,const int& to = 0,const double& w = 0.0):from(from),to(to),w(w){}
	Edge(const Edge& rsh)
	{
		this->from = rsh.from;
		this->to = rsh.to;
		this->w = rsh.w;
	}
	bool operator < (const Edge& rsh) const
	{
		return this->w < rsh.w;
	}
};

vector<Edge>edges;

double dis(Point& A,Point& B)
{
	return sqrt((double)(A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}

int root[maxn];

void init_set(int n)
{
	for(int i = 0 ; i < n ; i++)
	{
		root[i] = i;
	}
}

int find_set(int x)
{
	if(x != root[x])
	{
		root[x] = find_set(root[x]);
	}
	return root[x];
}

void union_set(int a,int b)
{
	int x = find_set(a);
	int y = find_set(b);
	if(x != y)
	{
		root[x] = y;
	}
}

double Tree[maxn][maxn];
double maxcost[maxn][maxn];
vector<int>nodes;

void init()
{
    init_set(n);
	points.clear();
	edges.clear();
	nodes.clear();
	memset(maxcost,0,sizeof(maxcost));
	memset(Tree,0,sizeof(Tree));
}

void dfs(int u,int fa,double facost)
{
	for(int i = 0 ; i < nodes.size() ; i++)
	{
        int x = nodes[i];
		maxcost[u][x] = maxcost[x][u] = max(maxcost[x][fa],facost);
	}
	nodes.push_back(u);
	for(int i = 0 ; i < n ; i++)
	{
		if(i != fa && Tree[u][i] != 0)
		{
			dfs(i,u,Tree[i][u]);
		}
	}
}

int main()
{
	#ifdef LOCAL
		freopen("in.txt","r",stdin);
	#endif
	int ncase;
	scanf("%d",&ncase);
	while(ncase--)
	{
	    scanf("%d",&n);
		int x,y,p;
		int from,to;
		double w;
		init();
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d %d %d",&x,&y,&p);
			points.push_back(Point(x,y,p));
		}
		for(int i = 0 ; i < n ; i++)
		{
			for(int j = i + 1 ; j < n ; j++)
			{
				from = i;
				to = j;
				w = dis(points[i],points[j]);
				edges.push_back(Edge(from,to,w));
			}
		}
		sort(edges.begin(),edges.end());
		double mst =0;
		for(int i = 0 ; i < edges.size() ; i++)
		{
			from = edges[i].from;
			to = edges[i].to;
			w = edges[i].w;
			if(find_set(from) != find_set(to))
			{
				union_set(from,to);
				mst += w;
				Tree[from][to] = Tree[to][from] = w;
			}
		}
        dfs(0,-1,0);
		double ans = -1;
		for(int i = 0 ; i < n ; i++)
		{
			for(int j = i + 1 ; j < n ; j++)
			{
				ans = max(ans,((double)(points[i].p + points[j].p))/ (mst - maxcost[i][j]));
			}
		}
		printf("%.2lf\n",ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值