POJ——T 2728 Desert King
http://poj.org/problem?id=2728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 27191 | Accepted: 7557 |
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
#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的更多相关文章
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- 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 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2728 Desert King
Description David the Great has just become the king of a desert country. To win the respect of his ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
- POJ 2728 Desert King | 01分数规划
题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
随机推荐
- svn 使用手册
版本控制器:SVN 1 开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目 ...
- IDEA安装使用
下载地址: https://www.jetbrains.com/idea/download/previous.html 这里我下载的是:2016.3.8版本的 安装: 安装成功后,需要秘钥的话,在 h ...
- [转] 随机数是骗人的,.Net、Java、C为我作证
(转自:随机数是骗人的,.Net.Java.C为我作证 - 杨中科 原文日期:2014.05.12) 几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生 ...
- 中国版 Office 365 (X-Tenant / Tango) 功能验证报告 - 1 简介
花了点时间做了一次Office 365 X-Tenant的 POC,对过程做了记录和总结,在这里会陆续分享: (一) 简介 这次POC的系统环境是模拟一个公司的生产环境: 1. 公司总部在国外,拥有 ...
- java web 学习笔记 - tomcat数据源
1. 数据库源 以前的JDBC连接步骤为: 1.加载数据库驱动 2.通过DriverManger获取数据库连接connection 3.通过connection执行prepareStatement的响 ...
- 【东软实训】SQL函数
SQL函数 SQL是用于访问和处理数据库的标准的计算机语言,我们所使用的的是Oracle SQL 一个数据库通常包含一个或多个表,每个表有一个名字表示,下图即为一个名为“emp”的表,接下来的操作都将 ...
- react-native 手势响应以及触摸事件的处理
react-native 的触摸事件: TouchableHighlight , TouchableNativeFeedBack , TouchableOpacity , TouchableWitho ...
- mysql批量插值
将查询结果集插入到表中(适用批量插值) 将结果集插入 不需要添加VALUES INSERT INTO `erp`.`role_menu` (`ROLEUUID`, `MENUUUID`) (SELEC ...
- 树莓派 -- i2c学习
硬件平台 RaspberryPi-3B+ Pioneer600外扩版 i2c芯片为DS3231,adddress 0x68 首先来看一下i2ctool的使用 i2ctool 使用 https://i2 ...
- Python之trutle库-五角星
Python之trutle库-五角星 #!/usr/bin/env python # coding: utf-8 # Python turtle库官方文档:https://docs.python.or ...