http://poj.org/problem?id=2728

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27191   Accepted: 7557

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

 
 
最优比例生成树,对于每条边有两个权值(a,b),求得ans=min( suma / sumb )
二分一个ans,判断 ai-bi*ans与0 的关系
 
 #include <algorithm>
#include <cstdio>
#include <cmath> #define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b) inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const double eps(1e-);
const int N();
struct Node {
int x,y,h;
}city[N];
int n; double L,R,Mid,ans;
struct Edge {
int u,v;
double w;
Edge(int u=,int v=,double w=0.0):u(u),v(v),w(w){}
bool operator < (const Edge&x)const { return w<x.w; }
}road[N*N]; inline double Dis(Node a,Node b)
{
double x=1.0*(a.x-b.x)*(a.x-b.x);
double y=1.0*(a.y-b.y)*(a.y-b.y);
return abs(a.h-b.h)-Mid*sqrt(x+y);
} int fa[N];
int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } inline bool check()
{
double ret=; int cnt=,m=;
for(int i=; i<=n; fa[i]=i++)
for(int j=; j<=n; ++j)
if(i!=j) road[++m]=Edge(i,j,Dis(city[i],city[j]));
std::sort(road+,road+m+);
for(int fx,fy,i=; i<=m; ++i)
{
fx=find(road[i].u),
fy=find(road[i].v);
if(fx==fy) continue;
fa[fx]=fy; ret+=road[i].w;
if(++cnt==n-) return ret<;
}
} int Presist()
{
for(; scanf("%d",&n)&&n; )
{
for(int i=; i<=n; ++i)
{
read(city[i].x),
read(city[i].y),
read(city[i].h),
R=max(R,1.0*city[i].h);
}
for(L=; L+eps<R; )
{
Mid=(L+R)/2.0;
if(check()) R=Mid;
else L=Mid;
}
printf("%.3lf\n",R);
}
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

T掉的Kruskal

 #include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath> #define max(a,b) (a>b?a:b) inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const double INF(10000000.0);
const double eps(1e-);
const int N(); double h[N][N],d[N][N];
struct Node {
int x,y,h;
}city[N];
int n; double L,R,Mid,ans,dis[N];
bool vis[N]; inline double Dis(Node a,Node b)
{
double x=1.0*(a.x-b.x)*(a.x-b.x);
double y=1.0*(a.y-b.y)*(a.y-b.y);
return (double)sqrt(x+y);
} inline bool check()
{
for(int i=; i<=n; ++i) vis[i]=;
for(int i=; i<=n; ++i) dis[i]=h[][i]-Mid*d[][i];
double ret=0.0,minn; vis[]=;
for(int i=,u; i<=n; ++i)
{
minn=INF;
for(int j=; j<=n; ++j)
if(!vis[j]&&minn>dis[j]) minn=dis[u=j];
if(minn==INF) break;
ret+=minn; vis[u]=;
for(int v=; v<=n; ++v)
if(!vis[v]&&dis[v]>h[u][v]-Mid*d[u][v])
dis[v]=h[u][v]-Mid*d[u][v];
}
return ret<=;
} int Presist()
{
for(; scanf("%d",&n)&&n; )
{
for(int i=; i<=n; ++i)
{
read(city[i].x),
read(city[i].y),
read(city[i].h),
R=max(R,city[i].h);
}
for(int i=; i<=n; ++i)
for(int j=i+; j<=n; ++j)
{
d[i][j]=d[j][i]=Dis(city[i],city[j]);
h[i][j]=h[j][i]=abs(city[i].h-city[j].h)*1.0;
}
for(L=; L+eps<R; )
{
Mid=(L+R)/2.0;
if(check()) R=Mid;
else L=Mid;
}
printf("%.3lf\n",R);
}
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

POJ——T 2728 Desert King的更多相关文章

  1. poj 2728 Desert King (最小比例生成树)

    http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  2. poj 2728 Desert King (最优比率生成树)

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

  3. POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25310   Accepted: 7022 Desc ...

  4. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  5. POJ 2728 Desert King (01分数规划)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 Descr ...

  6. POJ 2728 Desert King

    Description David the Great has just become the king of a desert country. To win the respect of his ...

  7. POJ 2728 Desert King(最优比率生成树 01分数规划)

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  8. POJ 2728 Desert King | 01分数规划

    题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...

  9. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

随机推荐

  1. CSS定位内容

    div.h1 或 p 元素常常被称为块级元素.这意味着这些元素显示为    一块内容,即“块框”.与之相反,span 和 strong 等元素称为“行    内元素”,这是因为它们的内容显示在行中,即 ...

  2. 一段js实现复制文本内容到剪切板

    <script type="text/javascript"> function copyUrl2() { var Url2=document.getElementBy ...

  3. 重装系统后,重新搭建Selenium Server+Firefox环境

    摘要:搭建Selenium自动化测试环境其实是非常简单的事情,在态度上我们不要把它当成难事:折腾起来是很愉快的,自然就成功了. 下面把这次安装的过程记录下来,一来是加深印象,二来可以给大家提供参考. ...

  4. 建设一个能承受500万PV/每天的网站如果计算?

    PV是什么: PV是page view的简写.PV是指页面的访问次数,每打开或刷新一次页面,就算做一个pv. 计算模型: 每台服务器每秒处理请求的数量=((80%*总PV量)/(24小时*60分*60 ...

  5. 浅谈CSS中的定位知识

    1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...

  6. 认知升级x

    写作目的 今天公司组织了一场关于认知升级的培训讲座,请的主管运营和发行的VP大神,收获颇多,亟待消化.这篇文章可以作为自己课后的一个笔记,自己可以反思,进而同步至团队,大家共同成长. 对于机会的认识 ...

  7. WebService 生成客户端

    webservice 生成客户端 wsdl2java -encoding UTF-8 -p com.trm -d D:\happywork -client http://localhost/trm-t ...

  8. struts2 前端显示错误信息

    当我们显示错误信息的时候,会发现错误信息会以列表的形式显示,这样就不美观了,达不到我们想要的标准.所以我们可以用另外的方式输出错误信息. 例如我现在增加了两个错误信息: this.addFieldEr ...

  9. Java基础(十一)--Serializable和Externalizable接口实现序列化

    序列化在日常开发中经常用到,特别是涉及到网络传输的时候,例如调用第三方接口,通过一个约定好的实体进行传输,这时你必须实现序列 化,这些都是大家都了解的内容,所以文章也会讲一下序列化的高级内容. 序列化 ...

  10. Mysql使用遇到的问题(一)

    1.在使用MySQL的时候,已经新建好了表,插入数据的时候报这个错误: Incorrect string value: '\xE5\xAF\x92\xE6\xB1\x9F...' for column ...