题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目是要求一棵最优比率生成树。

析:也就是求 r = sigma(x[i] * d) / sigma(x[i] * dist)这个值最小,变形一下就可以得到 d * r - dist <= 0,当r 最小时,取到等号,也就是求最大生成树,然后进行判断,有两种方法,一种是二分,这个题时间长一点,另一种是迭代,这个比较快。

代码如下:

二分:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Point{
int x, y, d;
};
Point a[maxn];
double dist[maxn][maxn];
double dis[maxn];
bool vis[maxn]; bool judge(double mid){
ms(vis, 0);
for(int i = 1; i <= n; ++i) dis[i] = -inf;
dis[1] = 0;
double ans = 0;
for(int i = 1; i <= n; ++i){
int mark = -1;
for(int j = 1; j <= n; ++j) if(!vis[j]){
if(mark == -1) mark = j;
else if(dis[j] > dis[mark]) mark = j;
}
if(mark == -1) break;
vis[mark] = 1;
ans += dis[mark];
for(int j = 1; j <= n; ++j) if(!vis[j]){
dis[j] = max(dis[j], dist[j][mark] * mid - abs(a[mark].d - a[j].d));
}
}
return ans >= 0.0;
} int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 1; i <= n; ++i){
scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].d);
for(int j = 1; j < i; ++j)
dist[i][j] = dist[j][i] = sqrt(sqr(a[i].x*1.-a[j].x) + sqr(a[i].y*1.-a[j].y));
}
double l = 0.0, r = 1e6;
while(r - l > eps){
double m = (l + r) / 2.0;
if(judge(m)) r = m;
else l = m;
}
printf("%.3f\n", l);
}
return 0;
}

 迭代:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Point{
int x, y, d;
};
Point a[maxn];
double dist[maxn][maxn];
double dis[maxn];
double A[maxn], B[maxn];
bool vis[maxn]; double judge(double mid){
ms(vis, 0);
for(int i = 1; i <= n; ++i) dis[i] = -inf;
dis[1] = 0; A[1] = B[1] = 0;
double ans = 0, aa = 0, bb = 0;
for(int i = 1; i <= n; ++i){
int mark = -1;
for(int j = 1; j <= n; ++j) if(!vis[j]){
if(mark == -1) mark = j;
else if(dis[j] > dis[mark]) mark = j;
}
if(mark == -1) break;
vis[mark] = 1;
ans += dis[mark];
aa += A[mark];
bb += B[mark];
for(int j = 1; j <= n; ++j) if(!vis[j]){
if(dis[j] < dist[j][mark] * mid - abs(a[mark].d - a[j].d)){
dis[j] = dist[j][mark] * mid - abs(a[mark].d - a[j].d);
A[j] = dist[j][mark];
B[j] = abs(a[mark].d - a[j].d);
}
}
}
return bb / aa;
} int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 1; i <= n; ++i){
scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].d);
for(int j = 1; j < i; ++j)
dist[i][j] = dist[j][i] = sqrt(sqr(a[i].x*1.-a[j].x) + sqr(a[i].y*1.-a[j].y));
}
double a = 0.0;
while(1){
double b = judge(a);
if(fabs(b - a) < eps){
printf("%.3f\n", a);
break;
}
a = b;
}
}
return 0;
}

  

POJ 2728 Desert King (最优比率树)的更多相关文章

  1. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  2. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  3. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  4. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

  5. poj 2728 Desert King (最小比例生成树)

    http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  6. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  7. poj 2728 Desert King (最优比率生成树)

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

  8. POJ 2728 Desert King(最优比率生成树 01分数规划)

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  9. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

随机推荐

  1. Nginx Windows 安装启动

    原文连接:http://tengine.taobao.org/book/appendix_c.html#nginxwindows 下载 Nginx是开源软件,用户可以访问 http://nginx.o ...

  2. mime设置

    ie9对mime有特殊要求,必须要有type才可以. 如果出现css的mime类型不支持.则没有加 type="css/text" 查看本机的mime支持: regedit > ...

  3. HibernateTemplate使用注意点

    1.  findByExample(vo) 可轻松根据vo的内部数据作为参数查找数据,vo中的基本类型不能为null,同时不支持主键查找. 2. get(vo.class, id) 根据主键来查找数据 ...

  4. 12 python json&pickle&shelve模块

      1.什么叫序列化 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes(字节) 2.用于序列化的两个模块,json和pickle ...

  5. 转载:mysql binlog同步redis

    ref: https://wenku.baidu.com/view/5d9d04ac6394dd88d0d233d4b14e852458fb39c4.html

  6. nginx配置【转】

    转自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_proc ...

  7. jenkins systemctl启动失败

    centos yum或者rpm安装jenkins后起不来 vi /etc/init.d/jenkins candidates="/usr/local/jdk1.8.0_171/bin/jav ...

  8. Centos7 配置ssh 免秘钥登陆

    1.yum install -y openssh 2.servier1: ssh-keygen -t rsa #有提示的直接enter 3.server 2: ssh-keygen -t rsa # ...

  9. bedtools简介及应用

    1)背景处理基因组数据中,比较基因组不同区域,例如寻找overlap等,是一种基本的且常见的问题.虽然UCSC 中‘Table Browser’或者Galaxy可以用来处理,但是当这些工具面对大的数据 ...

  10. 配置Tomcat 7 Gzip

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...