poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 18595 | Accepted: 5245 |
Description
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
Output
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
Source
题意:给定三维的点,求这样一棵树,使得高度差的和与水平距离的和的比值最小
这题是很显然的最优比例生成树,不能用贪心求出cost/len,再建MST。
注意输出用% .3f 用%.3lf会WA
/**
Judge Status:Accepted Memory:732K
Time:454MS Language:G++
Code Length:1772B Author:cj
*/ #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h> #define N 1010
#define INF 1000000000
//using namespace std; //加了这句居然报CE,什么情况没搞懂 double dis[N];
int pre[N],vis[N];
int n; struct Nod
{
int x,y,z;
}node[N]; double distance(Nod a,Nod b)
{
return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
} int abs(int x){return x>?x:-x;} double prim(double r)
{
memset(vis,,sizeof(vis));
int i;
for(i=;i<=n;i++)
{
dis[i] = abs(node[].z-node[i].z) - distance(node[],node[i])*r;
pre[i]=;
}
dis[] = ;
vis[] = ;
double cost=,len=;
int j;
for(i=;i<n;i++)
{
double mins = INF;
int k = -;
for(j=;j<=n;j++)
{
if(!vis[j]&&mins>dis[j])
{
mins = dis[j];
k = j;
}
}
if(k==-) break;
vis[k] = ;
cost += abs(node[pre[k]].z-node[k].z);
len += distance(node[pre[k]],node[k]);
for(j=;j<=n;j++)
{
double val = abs(node[k].z-node[j].z) - distance(node[k],node[j])*r;
if(!vis[j]&&dis[j]>val)
{
dis[j] = val;
pre[j] = k;
}
}
}
return cost/len;
} int main()
{
while(~scanf("%d",&n)&&n)
{
int i;
for(i=;i<=n;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z);
}
double a=,b=; //初始r为0
while()
{
b = prim(a);
if(fabs(a-b)<1e-) break;
a=b;
}
printf("%.3f\n",b); //printf("%.3lf\n",b); %.3lf居然WA 改 %.3f就AC 神马神马情况
}
return ;
}
poj 2728 Desert King (最小比例生成树)的更多相关文章
- poj 2728 Desert King(最小比率生成树,迭代法)
引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求 ...
- POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25310 Accepted: 7022 Desc ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
- poj 2728 Desert King(最优比例生成树)
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #i ...
随机推荐
- BOOTCAMP版本适配机型表
这些只支持64位的win7 win8 ******************************************** 5.1.5640 MacBook Air(11 英寸,2013 年中)M ...
- Java对Excel表格的操作
import java.io.File;//引入类import java.io.IOException;import java.util.Scanner;import jxl.Cell;import ...
- 极其简单的使用基于gulp和sass前端工作流
简单的记录自己如何在实际工作中使用gulp和sass的.我的原则是,小而美! gulp与sass介绍 gulp 什么是gulp?和Grunt一样,是一种任务管理工具:和Grunt又不一样,gulp是一 ...
- 第六节 ADO.NET
ADO.NET是一组为.NET编程人员提供数据访问服务的类,封装在System.Data.dll 中,是.NET框架的一部分,它是一种数据访问技术. 使用SQLServer数据提供程序 类 说明 Sq ...
- 面试之SQL(2)--left join, inner join 和 right join的区别
表A记录如下: aID aName 1 a1 2 a2 3 a3 4 a4 5 a5 ...
- response小结(二)——文件下载
我们先来看一个最简单的文件下载的例子: package com.yyz.response; import java.io.FileInputStream; import java.io.IOExcep ...
- 用命令行将Java程序打包为jar文件
如何把写好的Java程序打包为jar文件呢?有两种方式可以选择 1.命令行的方式: 打包jar cf JAR文件名称 程序文件名称或者程序所在的文件夹举例:jar cf MyApp.jar D:Jav ...
- java 下载文件功能代码例子
public static void down(HttpServletRequest request, HttpServletResponse response) throws Exceptio ...
- 分享:在微信公众平台做HTML5游戏经验谈(转载与http://software.intel.com/zh-cn/blogs/2013/04/03/html5)
分享:在微信公众平台做HTML5游戏经验谈 Dawei Cheng 程大伟... 于 星期三, 03/04/2013 - 03:19 提交 最近微信公众游戏平台讨论得如火如荼,大有HTML5游戏即将引 ...
- Mac下安装Twig模版引擎的方法
本人收集了几种方法: 安装前: twig需要依赖PHP 5.2.7 才能运行. 1 通过composer安装(2种) 方法一: 首先确保你已经安装了composer,如果没有安装,可以参考https: ...