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. PHP第二阶段学习 一、php的基本语法

    php的基本语法 输出语句:a.  echo输出:可以输出多个字符串,逗号隔开 b.  print输出:只能输出一个字符串,返回true或false c.  print_r():可以把字符串和数字简单 ...

  2. SQL基本操作——函数

    函数的类型:在 SQL 中,基本的函数类型和种类有若干种.函数的基本类型是:Aggregate 函数.Scalar 函数. Aggregate 函数:操作面向一系列的值,并返回一个单一的值,下面是SQ ...

  3. js的replace, 高亮

    ";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...

  4. 在centOS环境搭建airtest时遇到 Xlib.error.DisplayNameError: Bad display name "" 和Xlib.error.XauthError异常

    现在的问题 (airtestVenv) [root@67 airtest_selenium]# python3 proxy.pyTraceback (most recent call last):  ...

  5. 【Flutter学习】基本组件之AppBar顶部导航栏

    一,概述 AppBar 显示在app的顶部.AppBar包含5大部分,如下图: 二,构造函数及参数含义 构造函数 AppBar({ Key key, this.leading, //在标题前面显示的一 ...

  6. Luogu 2951 捉迷藏Hide and Seek

    P2951 [USACO09OPEN]捉迷藏Hide and Seek 题目描述 Bessie is playing hide and seek (a game in which a number o ...

  7. 关于while((c=getchar()))的一些应用与思考

    最近做题发现一个特别牛逼又特别神奇的读取入字符串的方法 while((c=getchar())!=....) { //do something } 为什么说强大呢,首先这个表达式对空格回车都不怕,他不 ...

  8. Java 初学者

    在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...

  9. 在Python脚本中调用Django环境(方便、右键运行,可用于ORM测试)

    随便创建一个py文件即可: import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODUL ...

  10. JS练习:商品的左右选择

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...