Desert King


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.


空间中有$N$个点。定义点$(x_0, y_0, z_0)$与点$(x_1, y_1, z_1)$之间的距离为$\sqrt{(x_0-x_1)^2+(y_0-y_1)^2}$,在它们之间连边的花费为$|z_0-z_1|$。要使得任意两点之间恰有一条路径,求总花费除以总距离的最小值。
数据范围:$2 \le N \le 1000$。


{\sum_{i \in S} c_i \over \sum_{i \in S} l_i} \ge k
\sum_{i \in S} c_i &\ge k\sum_{i \in S} l_i \\
\sum_{i \in S} (c_i-kl_i) &\ge 0


 * Good news from afar can bring you a welcome visitor.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

#define sqr(x) (1. * (x) * (x))

static int const N = 1005;
static double const EPS = 1e-5;
int fa[N], vis[N], pre[N];
double dist[N], mp[N][N];
struct Point {
  int x, y, z;
} p[N];

int main() {
  int n;
  for (scanf("%d", &n); n; scanf("%d", &n)) {
    for (int i = 1; i <= n; ++i)
      scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
    double ans = 0;
    for (;;) {
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
          if (i != j)
            mp[i][j] = abs(p[i].z - p[j].z) -
                       ans * sqrt(sqr(p[i].x - p[j].x) + sqr(p[i].y - p[j].y));
      double cost = 0, len = 0;
      memset(vis, 0, sizeof vis), vis[1] = 1;
      for (int i = 2; i <= n; ++i)
        dist[i] = mp[1][i], pre[i] = 1;
      for (int i = 2; i <= n; ++i) {
        int pos = 0;
        double mn = 1e15;
        for (int j = 1; j <= n; ++j)
          if (!vis[j] && dist[j] < mn)
            mn = dist[pos = j];
        vis[pos] = 1, cost += abs(p[pos].z - p[pre[pos]].z),
        len +=
            sqrt(sqr(p[pos].x - p[pre[pos]].x) + sqr(p[pos].y - p[pre[pos]].y));
        for (int j = 1; j <= n; ++j)
          if (!vis[j] && mp[pos][j] < dist[j])
            dist[j] = mp[pos][j], pre[j] = pos;
      if (abs(cost / len - ans) < EPS) {
        printf("%.3lf\n", ans);
      ans = cost / len;
  return 0;

RegMs If

418 I'm a teapot

Leave a Reply

Your email address will not be published. Required fields are marked *