POJ 2728 Desert King (最优比率树)
题意:有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 (最优比率树)的更多相关文章
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
随机推荐
- 前端-CSS-8-浮动与清楚浮动(重点)
<!-- 浮动是css里面布局最多的一个属性 效果: 两个元素并排了,并且两个元素都能够设置宽度和高度 浮动想学好:一定要知道它的四个特性: 1.浮动的元素脱标 2.浮动的元素互相贴靠 3.浮动 ...
- ABAP-BarCode-2-Excel打印二维码
以前用Excel打印过二维码看板标签,将实现过程备注下. 1.安装控件 安装文件:TBarCodeOffice.exe 2.控件注册 打开Excel,找到[选项] 在打开的界面选择[加载项],在活动应 ...
- nginx HttpLuaModule
http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...
- Centos 安装golang beego
刚开始用go和beego,真的还好多不懂~希望对看到的朋友有帮助~ 本人环境:centos 6.3x86_64 1.我在机器上创建了一个用户zww(useradd zww),登录zww(su zww) ...
- tair介绍以及配置
简介 tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化两种使用方式. 非持久化的 tair 可以看成是一个分布式缓存. 持久化的 tair 将数据存 ...
- JDK5并发(2) Locks-ReentrantLock
Java.concurrent.locks(2)-ReentrantLock @(Base)[JDK, locks, ReentrantLock, AbstractQueuedSynchronizer ...
- uwsgi相关问题
启动时报错: !!! no internal routing support, rebuild with pcre support !!!安装时 : sudo pip install uwsgi -I ...
- C#四舍五入说明
string.Format("{0:N2}", d) 与 Math.Round(d, 2).ToString() 不总是相等 string.Format("{0:N2}& ...
- docker registry ui
https://hub.docker.com/r/parabuzzle/docker-registry-ui/
- 可以foreach的 必须继承IEnumable 接口才行
只要是继承IEnumable 都可以用foreach遍历