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< ...
随机推荐
- flask 基础语法学习
回顾 #6行flask from flask import Flask app = Flask(__name__) @app.route("/") def index(): ret ...
- Android系统目录结构详解
Android系统基于linux内核.JAVA应用,算是一个小巧精致的系统.虽是开源,但不像Linux一般庞大,娇小可亲,于是国内厂商纷纷开发出自己基于Android的操作系统.在此呼吁各大厂商眼光放 ...
- python-类对象以字典模式操作
#类对象以字典模式操作 class Person: def __init__(self): self.cache={} def __setitem__(self, key, value): #增加或修 ...
- C# 动态调用 webservice 的类
封装这个类是为之后使用 webservice 不用添加各种引用了. using System; using System.Collections.Generic; using System.Compo ...
- MySQL保留字不能作为字段名使用
在设计MySQL字段的时候,无意中使用InOut这个名称作为字段名称,结果前端提交后就是没有写入数据库!但后端没有任何提示,跟踪mySQL日志,也没有留下痕迹,反复查,不得其解. 后来实在没有办法情况 ...
- go [第一篇]初识
[第一篇] 简介 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...
- 解决微信小程序wepy真机预览跟本地表现不一样,数据变化了视图没变化
当时搜了很多相关问题都没找到相似的 只看到有这个相似的描述wepy在onLoad里修改data-object的值页面不渲染 ,通过setData解决的. 但是这个还不是根本的解决办法,有些地方用set ...
- 查看oracle表空间
-- 查看oracle表空间 kB, bytes MB, bytes GB from user_segments where segment_type = 'TABLE';
- osgearth介绍
osgEarth为开发osg应用提供了一个地理空间SDK和地形引擎. osgEarth的目标: l 提供基于osg开发3D地理空间应用的支持; l 直接从数据源可视化地形模型和影像变得更加简单: l ...
- 题解-COCI-2015Norma
Problem SPOJ-NORMA2 & bzoj3745 题意概要:给定一个正整数序列 \(\{a_i\}\),求 \[\sum_{i=1}^n\sum_{j=i}^n(j-i+1)\mi ...