2013
11-11

# 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.

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.

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.

4
0 0 0
0 1 1
1 1 2
1 0 3
0


1.000

//* @author:
import java.util.*;
import java.math.*;
class Node{
int x,y,z;
}
public class Main {
static int []mark=new int[1010];
static double []dis=new double[1010];
static int []from=new int [1010];
static double sumup,sumdown;
static double [][]up=new double [1010][1010];
static double [][]down=new double [1010][1010];
static double [][]map=new double [1010][1010];
static double dist(double x1,double y1,double x2,double y2)
{
return Math.sqrt((double)(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
static void prim(int n)
{
Arrays.fill(mark, 0);
mark[1]=1;dis[1]=0;
sumup=sumdown=0;
for (int i=2;i<=n;i++) {
dis[i]=map[1][i];
from[i]=1;
}
for (int i=1;i< n;i++) {
double min=1e100;
int sit=0;
for (int j=1;j<=n;j++) if (mark[j]==0&&dis[j]< min) {
min=dis[j];
sit=j;
}
mark[sit]=1;
sumup+=up[sit][from[sit]];
sumdown+=down[sit][from[sit]];
for (int j=1;j<=n;j++) if (mark[j]==0&&dis[j]>map[sit][j]) {
dis[j]=map[sit][j];
from[j]=sit;
}
}
}
public static void main(String[] args) throws Exception{
Scanner in=new Scanner(System.in);
Node []a=new Node[1010];
while (true) {
int n=in.nextInt();
if (n==0) break;
for (int i=1;i<=n;i++) {
a[i]=new Node();
a[i].x=in.nextInt();
a[i].y=in.nextInt();
a[i].z=in.nextInt();
}
double mid=30;
for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) {
up[i][j]=up[j][i]=Math.abs(a[i].z-a[j].z);
down[i][j]=down[j][i]=dist(a[i].x,a[i].y,a[j].x,a[j].y);
}
double lastmid=30;
while (true) {
for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) {
map[j][i]=map[i][j]=up[i][j]-mid*down[i][j];
}
prim(n);
mid=sumup/sumdown;
if (Math.abs(mid-lastmid)< 0.001) break;
lastmid=mid;
}
System.out.printf("%.3f\n",mid);
}
}
}

1. 第23行：
hash = -1是否应该改成hash[s ] = -1

因为是要把从字符串s的start位到当前位在hash中重置

修改提交后能accept，但是不修改居然也能accept