首页 > 代码库 > ZOJ 2728 Desert King(最优比例生成树)
ZOJ 2728 Desert King(最优比例生成树)
Desert King
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 20586 | Accepted: 5777 |
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.
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
40 0 00 1 11 1 21 0 30
Sample Output
1.000
最优比例生成树:http://www.cnblogs.com/lotus3x/archive/2009/03/21/1418480.html
这是求最小值的情况。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 #include <cmath> 6 #include <iomanip> 7 using namespace std; 8 #define maxn 1010 9 #define INF 999999910 int N;11 struct Node{12 double x, y, z;13 }node[maxn];14 double l ,r ,m;15 double len[maxn][maxn], cost[maxn][maxn];16 int vis[maxn];17 double low[maxn];18 double cal(double x1, double y1, double x2, double y2){19 return (sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));20 }21 double prime(double L){22 int pos = 1; double result = 0;23 memset(vis, 0, sizeof(vis));24 vis[pos] = 1; 25 for(int i = 1; i <= N; i++){26 if(i != pos) low[i] = cost[pos][i] - L*len[pos][i];27 }28 29 for(int i = 1; i <= N-1; i++){30 double min = INF;31 for(int j = 1; j <= N; j++){32 if(!vis[j] && min > low[j]){33 pos = j; min = low[j];34 }35 } 36 result += min;37 vis[pos] = 1;38 39 for(int j = 1; j <= N; j++){40 if(!vis[j] && cost[pos][j] - L*len[pos][j] < low[j]) low[j] = cost[pos][j] - L*len[pos][j];41 }42 43 }44 return result;45 }46 int main(){47 while(scanf("%d", &N) && N){48 for(int i = 1; i <= N; i++){49 scanf("%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z);50 }51 memset(len, 0, sizeof(len));52 memset(cost, 0, sizeof(cost));53 for(int i = 1; i <= N; i++){54 for(int j = 1; j <= N; j++){55 if(i != j){56 len[i][j] = len[j][i] = cal(node[i].x, node[i].y, node[j].x, node[j].y);57 cost[i][j] = cost[j][i] = fabs(node[i].z - node[j].z);58 }59 }60 } 61 l = 0, r = 100.0;62 while( r - l > 1e-7){63 m = (l+r)/2;64 if(prime(m) >= 0) l = m;65 else r = m;66 }67 cout<<fixed<<setprecision(3)<<m<<endl;68 69 }70 71 return 0;72 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。