您的位置:首页 > 其它

【poj2728】Desert King

2016-04-04 11:14 337 查看

Problem

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.

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

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

4

0 0 0

0 1 1

1 1 2

1 0 3

0

Sample Output

1.000

题目大意

平面上给定n个点,每个点用(x,y,z)表示,求一棵生成树E,使得

∑(i,j)∈E|zi−zj|∑(i,j)∈E(xi−xj)2+(yi−yj)2−−−−−−−−−−−−−−−−−−−√

最小

Solution

这个模型叫:最优比率生成树

看到这个丑陋的分式,自然想到0-1分数规划,与简单数列问题不同的是,这里要求一棵树。

我们给边集编号,则不妨记分子为costi,分母为leni,xi表示每条边取或不取,题目就变为最小化∑|E|i=1xi∗costi∑|E|i=1xi∗leni

令di=costi−L∗leni,这就变回了0-1分数规划的模型。

大框架上,使用Dinkelbach算法

对于L的检验,因为是要最小化FL,所以对di做一遍最小生成树即可

p.s 这道题的图是完全图,因此用prim算法更优,Kruskal算法会TLE

Code

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long

inline int read() {
char c = getchar(); int x = 0, f = 1;
while(!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

const double eps = 1e-6;
const double inf = 2000000000;
const int N = 1111, M = N * N;
double cost

, len

, dis

;
struct point{
double x, y, z;
}p
;
int fa
, vis
, u
, v
;
double low
;
int n, tail;

double getdis(int i, int j) {
return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
}

void addedge(int x, int y) {
len[x][y] = len[y][x] = getdis(x, y);
cost[x][y] = cost[y][x] = fabs(p[x].z - p[y].z);
}

void solve() {
double L = 0, tmp = 0;
while(1) {
rep(i, 1, n) rep(j, 1, n) if (i != j) dis[i][j] = cost[i][j] - tmp * len[i][j];
memset(vis, 0, sizeof(vis));
vis[1] = 1; fa[1] = 1;
rep(i, 2, n) low[i] = dis[1][i], fa[i] = 1;
int m = 0;
while(m < n - 1) {
m++;
double mn = inf; int fv = -1;
rep(i, 1, n) {
if (vis[i]) continue;
if (low[i] < mn) mn = low[i], fv = i;
}
if (fv != -1) {
u[m] = fa[fv]; v[m] = fv; vis[fv] = 1;
rep(i, 1, n) {
if (vis[i]) continue;
if (dis[fv][i] < low[i]) low[i] = dis[fv][i], fa[i] = fv;
}
}
}
double qa = 0, qb = 0;
rep(i, 1, m) qa += cost[u[i]][v[i]], qb += len[u[i]][v[i]];
tmp = qa / qb;
if (fabs(L - tmp) < eps) break;
L = tmp;
}
printf("%.3lf\n", L);
}

int main() {
n = read();
while(n != 0) {
rep(i, 1, n) scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
rep(i, 1, n) rep(j, i + 1, n) addedge(i, j);
solve();
n = read();
}
return 0;
}


尾声

平面K近点对到底怎么做。。

看了一篇论文,对理论计算机科学家仇恨值MAX

马上就要省选了

End.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: