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 ...
随机推荐
- subprocess模块和sys模块
1.import sys # sys.path # sys.argv # 用来接收python解释器执行py文件后跟的参数#例如:python cp.py argv1 argv2 arg3#sys.a ...
- poj3040 Allowance
思路: 贪心. 看了题解说是 先把面值从大到小排序然后从头往尾扫,只要不超额,能取多少去多少然后如果还有剩余,就从尾往头扫,尽量取,让他恰好超额 不过并不懂证明. 实现: #include <i ...
- 在WIndowsPhone8 上制作的简单的计算器
今天,闲着没事,就自己做了一个小小的计算器...虽说自己刚学wp8开发没多长时间,望大神多多指教..1.这是前台页面的代码 <Grid x:Name=" Margin="10 ...
- webpack3.0版本的一些改动
npm install --save / npm install -S 项目发布上线之后还会依赖用到的插件,没有这些插件,项目不能运行 npm install --save-dev / npm ins ...
- java规范与标准?
所谓规范,即指由很多人同时遵守的行为或理论. java的规范并不是指其中一种,而是有很多种,比如java编码规范,java命名规范,java虚拟机规范等等,甚至于一个编码规范都有很多种,不同的公司.组 ...
- AUSU 安装Win10注意事项
Win10 U盘原版安装 安装前在电脑店PE里用DiskGenius分区分区表类型:GUID勾选:创建新ESP分区 . 创建MSR分区 安装必须使用UEFI引导和GPT硬盘,否则会提示无法安装Win1 ...
- Jenkins总结(ant+jmeter+java)
1.jdk与ant都需要在Jenkins-->系统管理-->全局工具配置里面配置各自的安装目录 2.修改Jenkins配置文件后,通过命令行重启: source /etc/profile ...
- BZOJ4318: OSU! (概率DP)
题意:一个串 给出每个字符为1的可能性 否则为0 一段连续的1能获得长度的立方的收益 问总收益的期望 题解:设x_i为到第i位时连续的1的期望长度 由i-1递推来的贡献 如果这一位是0没有贡献 如果是 ...
- 在计算机中简单的hello程序的运行
我之前很好奇就是你所写的代码到底是如何在计算机中执行的,到底是怎样开始,从哪里开始,在哪里执行,到哪里结束,之间到底是怎么的一个过程,带着这些问题,逐渐看这方面的书籍,最近买的<深入理解计算机系 ...
- Oracle存储过程和程序包
一.为什么要用存储过程? 如果在应用程序中经常需要执行特定的操作,可以基于这些操作简历一个特定的过程.通过使用过程可以简化客户端程序的开发和维护,而且还能提高客户端程序的运行性能. 二.过程的优点? ...