2015
04-16

# Qin Shi Huang’s National Road System

During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China —- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty —- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:
There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang.
Would you help Qin Shi Huang?
A city can be considered as a point, and a road can be considered as a line segment connecting two points.

The first line contains an integer t meaning that there are t test cases(t <= 10).
For each test case:
The first line is an integer n meaning that there are n cities(2 < n <= 1000).
Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city.
It is guaranteed that each city has a distinct location.

The first line contains an integer t meaning that there are t test cases(t <= 10).
For each test case:
The first line is an integer n meaning that there are n cities(2 < n <= 1000).
Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city.
It is guaranteed that each city has a distinct location.

2
4
1 1 20
1 2 30
200 2 80
200 1 100
3
1 1 20
1 2 30
2 2 40

65.00
70.00

1.由于结果是A/B，A的变化会引起B的变化，两个制约，无法直接贪心出最大的A/B，故要通过枚举

3.可以枚举两点，找出边，也可以枚举边，找出点，我是用后者，感觉比较容易写也好理解

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
#define N 1050
#define INF 1000000
struct point
{
double x,y;
int num;
}p[N];
int n;
double map[N][N],dist[N],A,B;
vector<int>vec[N];
double getdistance(point aa,point bb)
{
return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
int pre[N];
void prim()
{
for(int i=1;i<=n;++i)
dist[i]=INF*1.0;
double min_edge;
int min_p,now=1;
for(int i=1;i<n;++i)// 这里dist 表示到达已经更新好的点集的最小距离
{
min_edge=INF*1.0;
for(int i=1;i<=n;++i)
if(map[now][i]>0)
{
if(dist[i]>map[now][i])
dist[i]=map[now][i],pre[i]=now;
}
dist[now]=-1.0;
for(int i=1;i<=n;++i)
if(dist[i]>0&&dist[i]<min_edge)
{
min_edge=dist[i];
min_p=i;
}
vec[pre[min_p]].push_back(min_p);  // 最小边有可能来自之前更新过的，这里WA
vec[min_p].push_back(pre[min_p]);
now=min_p;
B+=min_edge;
}
}
int vis[N];
int dfs(int u,int fa)
{
int max_num=p[u].num;
int ret=u;int tt;
for(int i=0;i<vec[u].size();++i)
if(vec[u][i]!=fa)
{
tt=dfs(vec[u][i],u);
if(max_num<p[tt].num)
max_num=p[tt].num,ret=tt;
}
return ret;
}

int main ()
{
//freopen("aa.txt","r",stdin);
//freopen("bb.txt","w",stdout);
int test;scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%lf%lf%d",&p[i].x,&p[i].y,&p[i].num);

for(int i=1;i<=n;++i)
{
for(int j=i+1;j<=n;++j)
map[i][j]=map[j][i]=getdistance(p[i],p[j]);
map[i][i]=0.0;
}
for(int i=0;i<=n;++i)
vec[i].clear();
B=0.0;
memset(pre,-1,sizeof(pre));
prim();
double ans=-1.0;
double res;int t1,t2;
for(int i=1;i<=n;++i)
for(int j=0;j<vec[i].size();++j)  // 枚举边，对于这条割边两侧的连通图分别找出点权最大的点
{
t1=dfs(i,vec[i][j]);
t2=dfs(vec[i][j],i);
res=(p[t1].num+p[t2].num)/(B-map[i][vec[i][j]]);
if(res>ans)
ans=res;
}
ans+=(1e-8);
printf("%.2lf\n",ans);
}
//system("pause");
return 0;
}

1. 很高兴你会喜欢这个网站。目前还没有一个开发团队，网站是我一个人在维护，都是用的开源系统，也没有太多需要开发的部分，主要是内容整理。非常感谢你的关注。