Desert King POJ - 2728(最优比率生产树/(二分+生成树))
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 题意:n个村庄,每个村庄给你x、y、z坐标,表示他的三维位置,两个村庄之间距离为不算z轴欧几里得距离、建一条路的花费
为两个村庄z坐标差值,构建一条路网络,使的每个点都被连接(生成树),且让其花费/距离最小(01分数规划) 思路:二分比例,然后用最小(最大)生成树,比较是否符合情况。
例如:花费/距离<=mid == 花费-mid*距离 <= 0, 使用最小生成树(边权:花费-mid*距离),看看是否和小于零(满足) 注:我用前向星不知道为什么超时了,看网上题解改的邻接矩阵,知道的大佬orz请通知一声
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define inf 1e18;
using namespace std; int n;
int x[],y[],z[];
const double eps = 1e-; bool vis[];
double maps[][];
double cost[][]; 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 dist[]; bool prim(double mid)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=inf;
dist[]=;
for(int i=;i<n;i++)
{
int minn = inf;
int id = -;
for(int j=;j<=n;j++)
{
if(!vis[j] && (id == - || dist[j] < dist[id]))id=j;
}
if(id == -)break;
vis[id]=; for(int j=;j<=n;j++)
{
if(!vis[j])dist[j] = min(dist[j],cost[id][j]-mid*maps[id][j]);
}
}
double ans = ;
for(int i=;i<=n;i++)ans += dist[i];
if(ans <= )return ;
return ;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(maps,,sizeof(maps));
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
maps[i][j] = maps[j][i] = dis(i,j);
cost[i][j] = cost[j][i] = abs(z[i]-z[j]);
}
}
double l=,r=;
while(r - l >= eps)
{
double mid =(r-l)/+l;
if(prim(mid))r = mid;
else l = mid;
}
printf("%.3f\n",(l+r)/);
}
}
前向星超时代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define inf 1e18;
using namespace std; int n;
int x[],y[],z[];
const double eps = 1e-;
struct Node
{
int y,next;
double val,c;
}node[];
int cnt,head[];
bool vis[]; void add(int x,int y,double val,double c)
{
node[++cnt].y=y;
node[cnt].val=val;
node[cnt].c=c;
node[cnt].next=head[x];
head[x]=cnt;
} 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 dist[]; bool prim(double mid)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=inf;
dist[]=;
for(int i=;i<n;i++)
{
int minn = inf;
int id = -;
for(int j=;j<=n;j++)
{
if(!vis[j] && (id == - || dist[j] < dist[id]))id=j;
}
if(id == -)break;
vis[id]=;
for(int j=head[id];j;j=node[j].next)
{
int to = node[j].y;
if(!vis[to])dist[to] = min(dist[to],node[j].c-mid*node[j].val);
}
}
double ans = ;
for(int i=;i<=n;i++)ans += dist[i];
if(ans <= )return ;
return ;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
cnt = ;
//printf("================================\n");
memset(head ,,sizeof(head));
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
double td = dis(i,j);
double tz = abs(z[i]-z[j]);
add(i,j,td,tz);
add(j,i,td,tz);
}
}
double l=,r=;
while(r - l >= eps)
{
double mid =(r-l)/+l;
if(prim(mid))r = mid;
else l = mid;
}
printf("%.3f\n",(l+r)/);
}
}
Desert King POJ - 2728(最优比率生产树/(二分+生成树))的更多相关文章
- Desert King (poj 2728 最优比率生成树 0-1分数规划)
Language: Default Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22113 A ...
- [POJ2728] Desert King 解题报告(最优比率生成树)
题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...
- poj 2728 最优比率生成树
思路:设sum(cost[i])/sum(dis[i])=r;那么要使r最小,也就是minsum(cost[i]-r*dis[i]);那么就以cost[i]-r*dis[i]为边权重新建边.当求和使得 ...
- poj 3621(最优比率环)
题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...
- POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分
最优比率环问题.二分答案,对于每一个mid,把节点的happy值归类到边上. 对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大. /*---------------- ...
- poj 3621(最优比率环)
Sightseeing Cows Farmer John has decided to reward his cows for their hard work by taking them on a ...
- POJ 3621 最优比率生成环
题意: 让你求出一个最优比率生成环. 思路: 又是一个01分化基础题目,直接在jude的时候找出一个sigma(d[i] * x[i])大于等于0的环就行了,我是用SPFA跑最长路 ...
- poj 2728 最优比例生成树(01分数规划)模板
/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...
- POJ 2728 JZYZOJ 1636 分数规划 最小生成树 二分 prim
http://172.20.6.3/Problem_Show.asp?id=1636 复习了prim,分数规划大概就是把一个求最小值或最大值的分式移项变成一个可二分求解的式子. #include< ...
随机推荐
- 【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例
#用法说明 select row_number() over(partition by A order by B ) as rowIndex from table A :为分组字段 B:为分组后的排序 ...
- MySQL实战45讲学习笔记:索引(第五讲)
一.需要回表的案例 在下面表T中,执行下面语句,需要执行几次树的搜索操作?会扫描多少行? select * from T where k between 3 and 5 1.初始化语句 mysql&g ...
- JS基础-第5天
复习函数 函数 作用:封装一段代码,封装的功能可以被反复调用执行. 定义函数 // 第一种 - 函数声明 function 函数名(){ } // 第二种 - 函数表达式 var 函数名 = func ...
- LINUX 常用命令(二)
B0.用户相关配置文件 用户信息文件: /etc/passwd密码文件: /etc/shadow用户组文件: /etc/group用户组密 ...
- 电脑右键新建excel工作表,但是扩展名是.xls,而不是.xlsx
怀疑是因为之前安装了wps,然后又卸载了,导致的.上网查阅,如下: excel默认新建xls 不是我的问题 Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法 ...
- 在Windows Server上安装ASP.NET时失败,提示not enough storage is available to process the command
今天在部署ASP.NET网站时出现IIS 500.21错误.环境是Windows Server 2012 +IIS8 于是查找解决方案,发现网上的信息都说是需要重装.NET framerwork4.0 ...
- 使用antd Table + mobx 处理数组 出现的一系列问题
在store中定义了一个数组: @observable list = [...] 若是在table组件中直接使用list: <Table className={styles.table} col ...
- Centos7.2正常启动关闭CDH5.16.1
1.正常的启动.关闭流程 关闭流程 cluster1 stop Cloudera Management Service stop 4台agent:systemctl stop cloudera ...
- 20164305 徐广皓 Exp2 后门原理与实践
实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter(或其他软件)生成可执行文件, ...
- java Socket实例
可以实现客户端与服务端双向通信,支持多客户端连接,客户端断开连接,服务端不会出现异常 服务端代码: package com.thinkgem.jeesite.modules.socketTest.de ...