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. AJPFX关于多态中的动态绑定和静态绑定的总结

    在多态中:成员变量和静态方法编译和运行都看左边:成员方法编译看左边,运行看右边,这是为什么:在Java中存在两种绑定方式,一种为静态绑定,又称作早期绑定.另一种就是动态绑定,亦称为后期绑定1.静态绑定 ...

  2. Java语法基础-final关键字

    final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...

  3. install nginx error

    the error info : the HTTP gzip module requires the zlib library.You can either disable the module by ...

  4. javaEE web 系统安装时自定义初始化

    通常JavaWeb项目在第一次启动时我们需要做一些初始化工作,比如:初始化一个管理员的登录账户和密码,配置缓存.定时任务等,这些操作可以通过手工修改数据库完成,但是容易出错且繁琐,而且也很麻烦.如果这 ...

  5. 阿里云设置指定ip访问实例

    添加安全组规则 添加允许访问的外网IP,优先级设置为1,并将所有ip设置为拒绝访问,优先级设置为2. 参考地址: https://help.aliyun.com/document_detail/254 ...

  6. (转载)Sql注入的分类:数字型+字符型

    Sql注入: 就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.通过构造恶意的输入,使数据库执行恶意命令,造成数据泄露或者修改内容等,以 ...

  7. php 阿里云短信验证码

    阿里云短信服务:https://dysms.console.aliyun.com 1.准备 1.1.创建签名.模板 1.2.创建.使用阿里云秘钥 地址:https://usercenter.conso ...

  8. MFC线程获取主窗口句柄

    CWnd* h_q = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄

  9. 字符集匹配:\s 匹配一个空格,一边后面加量词表示多个空格,\s*表示0个以上空格,\s+表示1个以上空格,\s相当于[\f\r\n\t ]5种空白字符。

    字符集匹配:\s 匹配一个空格,一边后面加量词表示多个空格,\s*表示0个以上空格,\s+表示1个以上空格,\s相当于[\f\r\n\t ]5种空白字符.

  10. selenium click radio

    radio = dr.find_element_by_xpath('//*[@id="contentTable"]/tbody/tr[1]/td[1]/input') webdri ...