题目链接:http://poj.org/problem?id=2728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26878 | Accepted: 7459 |
Description
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
Output
Sample Input
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e3+10; 19 20 int n; 21 double x[MAXN], y[MAXN], z[MAXN]; 22 double dis[MAXN][MAXN], height[MAXN][MAXN], d[MAXN][MAXN]; 23 //dis为两点间的距离, height为两点间的高度差。d[i][j] = height[i][j] - L*dis[i][j],作为图的边权。 24 25 bool vis[MAXN]; 26 double cost[MAXN]; 27 double prim() 28 { 29 ms(vis, 0); 30 for(int i = 2; i<=n; i++) 31 cost[i] = d[1][i]; 32 vis[1] = true; 33 34 double sum = 0; 35 for(int i = 1; i<=n-1; i++) 36 { 37 int k; 38 double minn = INF; 39 for(int j = 2; j<=n; j++) 40 if(!vis[j] && minn>cost[j]) 41 minn = cost[k=j]; 42 43 vis[k] = true; 44 sum += cost[k]; //加上边权 45 for(int j = 2; j<=n; j++) 46 if(!vis[j]) 47 cost[j] = min(cost[j], d[k][j]); 48 } 49 return sum; 50 } 51 52 bool test(double L) 53 { 54 for(int i = 1; i<=n; i++) 55 for(int j = 1; j<=n; j++) 56 d[i][j] = height[i][j]-L*dis[i][j]; 57 58 return prim()>=0; 59 } 60 61 int main() 62 { 63 while(scanf("%d", &n) && n) 64 { 65 for(int i = 1; i<=n; i++) 66 scanf("%lf%lf%lf", &x[i], &y[i], &z[i]); 67 68 for(int i = 1; i<=n; i++) 69 for(int j = 1; j<=n; j++) 70 { 71 dis[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); 72 height[i][j] = fabs(z[i]-z[j]); 73 } 74 75 double l = 0, r = 100.0; 76 while(l+EPS<=r) 77 { 78 double mid = (l+r)/2; 79 if(test(mid)) 80 l = mid + EPS; 81 else 82 r = mid - EPS; 83 } 84 printf("%.3f\n", r); 85 } 86 }