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;
}

Desert King (poj 2728 最优比率生成树 0-1分数规划)的更多相关文章

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

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

  2. [POJ2728] Desert King 解题报告(最优比率生成树)

    题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...

  3. poj 2728 最优比率生成树

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

  4. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

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

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  6. Desert King POJ - 2728(最优比率生产树/(二分+生成树))

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

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

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

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

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

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

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

随机推荐

  1. Android项目源码分享

    http://blog.csdn.net/gao_chun/article/details/47263063 Android项目源码分享 给大家分享几个Android开发项目源码,大部分功能相信可以在 ...

  2. xlsx 读取文件日期问题

    xlsx 的版本:0.13.5,可以取到日期 xlsx 的版本:0.14.3,取到的日期转为数字了,没有找到方法转为日期, 可以开启   cellDates: true,但是这个时区不对, dateN ...

  3. Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作

    使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...

  4. Global Round 2

    A - Ilya and a Colorful Walk CodeForces - 1119A Ilya lives in a beautiful city of Chordalsk. There a ...

  5. InnoDB INFORMATION_SCHEMA Metrics Table

    InnoDB INFORMATION_SCHEMA Metrics Table INNODB_METRICS表将所有InnoDB性能和资源相关计数器合并到一个INFORMATION_SCHEMA表中. ...

  6. Mac下复制粘贴的快捷键是什么?随记

    刚从window换成Mac OS系统的用户对于一些常用的快捷键一定非常的不习惯,“mac复制粘贴快捷键是什么?”这一简单的问题相信很多刚刚从Windows平台转到Mac平台的用户会问到的问题,因为Ma ...

  7. php 快速导出大量CSV文件

    原文链接 https://segmentfault.com/a/1190000005366832 /** * 导出excel(csv) * @data 导出数据 * @headlist 第一行,列名 ...

  8. Python 文件操作(一)

    一.注意事项 A.能调用方法的一定是对象 B.文件的操作流程: 1. 打开文件,得到文件句柄并赋值给一个变量 2. 通过句柄对文件进行操作 3. 关闭文件 二.操作实现方法 '''文件名:小双双文件内 ...

  9. CentOS 6.5 x64 安装Tomcat8 并配置两个Tomcat8

    1.首先,安装tomcat的前提是已经配置好jdk环境变量,若没配好可以参考我的上一篇博文:CentOS 6.5 x64安装jdk8,当然也可以通过网络搜索安装步骤~~ 2.下载: 可以通过官网下载: ...

  10. 电源模块PCB设计

    电源模块的PCB设计 电源电路是一个电子产品的重要组成部分,电源电路设计的好坏,直接牵连产品性能的好坏.我们电子产品的电源电路主要有线性电源和高频开关电源.从理论上讲,线性电源是用户需要多少电流,输入 ...