hdu4081 Qin Shi Huang's National Road System

题目大意就是说,n个城市,修成一棵树,其中有一条边是可以用魔法的,这条边是不需要计费的,每个城市有住人,问修完所有的边的总路程是B,那条魔法修建边链接的两个城市的人口是A,求A/B的最大值。

分析:一开始就能想到的是B小、A大。因为是修成树,所以B开始就直接是最小生成树的值了,同时记录两点路径上的最大边权。在就是枚举修建的魔法边,然后删掉两点间的最大边权值(前有做记录),这是直接算就是了,取最大的A/B。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 1010;
Point p[N];
int A[N];
int Pre[N], Mark[N];
double Path[N][N], Dist[N];
double G[N][N];
int n, m;
double Prim() {
	double ret = 0.0;
	memset(Dist, INF,sizeof Dist);
	memset(Path, 0,sizeof Path);
	memset(Mark, 0,sizeof Mark);
	Dist[0] = 0;
	Mark[0] = true;
	for (int i = 1;i <= n;++i) {
		Dist[i] = G[0][i];
		Pre[i] = 0;
	}
	for (int i = 1;i < n;++i) {
		int u = -1;
		for (int j = 0;j < n;++j) {
			if (Mark[j]) continue;
			if (u == -1 || Dist[j] < Dist[u]) {
				u = j;
			}
		}
		if (u == -1) return -1;
		Mark[u] = true;
		ret += Dist[u];
		for (int j = 0;j < n;++j) {
			if (Mark[j] && j != u) Path[j][u] = Path[u][j] = max(Path[j][Pre[u]], Dist[u]);
			if (!Mark[j]) if (Dist[j] > G[u][j]) {
				Dist[j] = G[u][j];
				Pre[j] = u;
			}
		}
	}
	return ret;
}
void solve() {
	double ans = Prim();
	double answer = 0.0;
	for (int i = 0;i < n;++i) {
		for (int j = i + 1;j < n;++j) {
			answer = max(answer, (A[i] + A[j])*1.0 / (ans - Path[i][j]));
		}
	}
	printf("%.2lf\n", answer);
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		for (int i = 0;i < n;++i)
			scanf("%lf%lf%d",&p[i].x,&p[i].y,&A[i]);
		m = 0;
		memset(G, INF, sizeof G);
		for (int i = 0;i < n;++i) {
			G[i][i] = 0;
			for (int j = i + 1;j < n;++j) {
				G[i][j] = G[j][i] = Length(p[i] - p[j]);
			}
		}
		solve();
	}
	return 0;
}





你可能感兴趣的:(dp,MST,暴力)