zoukankan      html  css  js  c++  java
• # 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
", 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
", a);
break;
}
a = b;
}
}
return 0;
}
```

• 相关阅读:
TMD 这个写笔记的号，盗了有意思吗
类成员的指针必须NULL化，否则是乱七八糟的东西
超前引用不可使用类名来定义变量和函数的变量参数，只可用来定义引用或者指针。
XP下，移动窗口产生重影的问题
生成ico格式图标
设置窗口的z-order总是在最底部
关于windows的锁定状态
使用Layered Window遇到的一些问题及解决方法
转-使用wifi调试程序
URL的格式
• 原文地址：https://www.cnblogs.com/dwtfukgv/p/7745597.html