Language:
Default
Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22113   Accepted: 6187

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate
ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. 



After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary
channels to bring water to all the villages, which means there will be only one way to connect each village to the capital. 



His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel
between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that
each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line. 



As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.

Input

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the
position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1.000

Source

题意:将n个村庄连在一起,告诉每一个村庄的三维坐标,村庄之间的距离为水平方向上的距离。花费为垂直方向上的高度差。求把村庄连接起来的最小的花费与长度之比为多少。

思路:经典的01分数规划问题,參考这位大神的解说应该就能明确了:http://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005; double x[maxn],y[maxn],z[maxn];
double dist[maxn],mp[maxn][maxn],len[maxn][maxn],cost[maxn][maxn];
bool vis[maxn];
int pre[maxn];
int n; double Dis(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
} double prim(double r)
{
int i,j,now;
double mi,c=0,l=0;
for (i=0;i<n;i++)
{
dist[i]=INF;
for (j=0;j<n;j++)
{
mp[i][j]=cost[i][j]-r*len[i][j];
}
}
for (i=0;i<n;i++)
{
dist[i]=mp[i][0];
pre[i]=0;
vis[i]=false;
}
dist[0]=0;
vis[0]=true;
for (i=1;i<n;i++)
{
mi=INF;now=-1;
for (j=0;j<n;j++)
{
if (!vis[j]&&mi>dist[j])
{
mi=dist[j];
now=j;
}
}
if (now==-1) break;
vis[now]=true;
c+=cost[pre[now]][now];
l+=len[pre[now]][now];
for (j=0;j<n;j++)
{
if (!vis[j]&&dist[j]>mp[now][j])
{
dist[j]=mp[now][j];
pre[j]=now;
}
}
}
return c/l;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j;
while (sf(n))
{
if (n==0) break;
for (i=0;i<n;i++)
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
len[i][j]=Dis(i,j);
cost[i][j]=fabs(z[i]-z[j]);
}
}
double r=0,rate; //r迭代初值为0
while (1)
{
rate=r;
r=prim(r);
if (fabs(r-rate)<eps) break;
}
printf("%.3f\n",r);
}
return 0;
}

最新文章

  1. PhotoSwipe插件的使用
  2. Android:Toast
  3. [转] Redis系统性介绍
  4. 处理 InterruptedException——Brian Goetz
  5. 软件工程(GZSD2015)第二次作业文档模板
  6. XP下安装MAC OS虚拟系统
  7. BugZilla的安装过程简明教程
  8. Android: ScrollView监听滑动到顶端和底端
  9. PHP获取指定页面的指定内容
  10. 参考用bat文件
  11. 初学python之路-day11
  12. MyBatis中&lt;if test=&quot; &quot;&gt;标签条件不起作用
  13. WCF 寄宿Windows以及控制台启动
  14. Python-文件操作—_19
  15. spring cloud配置中心属性加密处理
  16. python里面的xlrd模块
  17. git clone项目
  18. 2018.11.24 poj2774Long Long Message(后缀数组)
  19. ACE Editor在线代码编辑器简介及使用引导
  20. 1. K-Means原理解析

热门文章

  1. C-基础:C语言为什么不做数组下标越界检查
  2. python爬虫---从零开始(一)初识爬虫
  3. BZOJ1232: [Usaco2008Nov]安慰奶牛cheer(最小生成树)
  4. 第1节 flume:7、flume的监控文件夹,实现数据收集到hdfs上
  5. UVA-1625-Color Length(DP LCS变形)
  6. Css选择器和JQuery基本编程接口
  7. nginx的配置和基本使用命令
  8. spring-mvc junit测试
  9. Python通过Openpyxl包汇总表格,效率提升100倍
  10. Python 函数的初识