Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 25729   Accepted: 7143

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

先贴个代码  明天找时间来补解释
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const int MAX=+;
int vis[N];
double x[N],y[N],z[N];
double w[MAX][MAX],v[MAX][MAX];
double low[N];
int n;
double fun(double a,double b,double c,double d){
double ans=(a-c)*(a-c)+(b-d)*(b-d);
return sqrt(ans);
}
int prime(double x){
double sum=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)low[i]=v[][i]-x*w[][i];
vis[]=;
for(int i=;i<n;i++){
int k;
double maxx=INF*1.0;
for(int j=;j<n;j++)if(vis[j]==&&low[j]<maxx){
maxx=low[j];
k=j;
}
if(maxx==1.0*INF)break;
sum=sum+maxx;
vis[k]=;
for(int j=;j<n;j++)
if(vis[j]==&&low[j]>v[k][j]-x*w[k][j])
low[j]=v[k][j]-x*w[k][j];
}
if(sum>)return ;
else
return ;
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n==)break;
double maxx=;
double minn=INF*1.0;
for(int i=;i<n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
double t=fun(x[i],y[i],x[j],y[j]);
w[i][j]=w[j][i]=t;
v[i][j]=v[j][i]=fabs(z[i]-z[j]);
maxx=max(v[i][j],maxx);
minn=min(minn,t);
}
double low=0.0;
double high=maxx/minn;
double ans;
while(low+eps<high){
double mid=(low+high)/;
if(prime(mid)){
ans=mid;
low=mid;
}
else
high=mid;
}
printf("%.3f\n",ans);
}
}

POJ 2728(最优比率生成树+01规划)的更多相关文章

  1. Desert King (poj 2728 最优比率生成树 0-1分数规划)

    Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   A ...

  2. poj 2728 最优比率生成树

    思路:设sum(cost[i])/sum(dis[i])=r;那么要使r最小,也就是minsum(cost[i]-r*dis[i]);那么就以cost[i]-r*dis[i]为边权重新建边.当求和使得 ...

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

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

  4. poj 2728 最优比例生成树(01分数规划)模板

    /* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...

  5. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

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

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

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

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

  8. [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

    01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...

  9. poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)

    题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...

随机推荐

  1. IP访问频率限制不能用数组循环插入多个限制条件原因分析及解决方案

    14.IP频率限制不能用数组循环插入多个限制条件原因分析及解决方案: define("RATE_LIMITING_ARR", array('3' => 3, '6' => ...

  2. Tcl之Lab1

    Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...

  3. THREE.js代码备份——webgl - geometry - dynamic(模拟海浪,通过时间(毫秒)来控制平面点的运动模拟海浪,鼠标控制写在另外的js中)

    HTML: <!DOCTYPE html> <html lang="en"> <head> <title>three.js webg ...

  4. 为什么有些异常throw出去需要在函数头用throws声明,一些就不用

    throw new IllegalStateException(".");不用在函数头声明throws IllegalStateExceptionthrow new IOExcep ...

  5. R语言数据重塑

    使用cbind()函数连接多个向量来创建数据帧.此外,使用rbind()函数合并两个数据帧   使用merge()函数合并两个数据帧.数据帧必须具有相同的列名称,在其上进行合并   melt()拆分数 ...

  6. jenkins配置发送测试结果邮件

    简单版: https://www.cnblogs.com/gcgc/p/5631385.html https://blog.51cto.com/qicheng0211/1919341 升级版 http ...

  7. transition-分栏按钮动画

      => css: .cateBtn{ position: relative; background: #fff; border: 1px solid #ddd; border-radius: ...

  8. python开发 面试题

    一.简述列表与元组的区别 答: 元组tuple与列表List相同点 元组tuple与列表List都是序列类型的容器对象,可以存放任何类型的数据.支持切片.迭代等操作. 元组tuple与列表List区别 ...

  9. C 利用strtok, feof 截取字符串

    #cat /tmp/fff 10:hugetlb:/hello/06b11c9967cc0e106f5f4673246f671aa7388f623f58b250d9d9cb0f8c0f2b18 9:d ...

  10. C语言中的DEBUG

    #cat aa.c #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include < ...