博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2728 Desert King
阅读量:4967 次
发布时间:2019-06-12

本文共 4069 字,大约阅读时间需要 13 分钟。

Desert King

Time Limit: 3000ms
Memory Limit: 65536KB
This problem will be judged on 
PKU. Original ID: 
64-bit integer IO format: %lld      Java class name: Main
 
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

40 0 00 1 11 1 21 0 30

Sample Output

1.000

Source

 
解题:最有比率生成树
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define LL long long14 #define pii pair
15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1010;18 int cost[maxn][maxn],p[maxn];19 int n,x[maxn],y[maxn],z[maxn];20 bool vis[maxn];21 double d[maxn],w[maxn][maxn];22 double Prim(double tmp) {23 double sumx = 0,sumy = 0;24 for(int i = 1; i <= n; i++) {25 p[i] = 1;26 d[i] = cost[1][i] - tmp*w[1][i];27 vis[i] = false;28 }29 vis[1] = true;30 d[1] = 0;31 for(int i = 2; i <= n; i++) {32 double MST = INF;33 int index = -1;34 for(int j = 2; j <= n; j++)35 if(!vis[j] && d[j] < MST) MST = d[index = j];36 vis[index] = true;37 sumx += cost[p[index]][index];38 sumy += w[p[index]][index];39 for(int j = 1; j <= n; j++)40 if(!vis[j] && d[j] > cost[index][j] - tmp*w[index][j]) {41 d[j] = cost[index][j] - tmp*w[index][j];42 p[j] = index;43 }44 }45 return sumx/sumy;46 }47 int main() {48 while(scanf("%d",&n),n) {49 for(int i = 1; i <= n; i++) {50 scanf("%d %d %d",x+i,y+i,z+i);51 for(int j = 1; j < i; j++) {52 cost[i][j] = cost[j][i] = abs(z[i] - z[j]);53 double tmp = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);54 w[i][j] = w[j][i] = sqrt(tmp);55 }56 }57 double ans = 0;58 while(true) {59 double tmp = Prim(ans);60 if(fabs(ans - tmp) < (1e-4)) break;61 ans = tmp;62 }63 printf("%.3f\n",ans);64 }65 return 0;66 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3986113.html

你可能感兴趣的文章
DELPHI搭建centos开发环境
查看>>
IdHTTPServer允许跨域访问
查看>>
DELPHI开发LINUX包
查看>>
更新.net core 3.0,dotnet ef命令无法使用的解决办法
查看>>
React躬行记(13)——React Router
查看>>
前端利器躬行记(1)——npm
查看>>
前端利器躬行记(2)——Babel
查看>>
前端利器躬行记(3)——webpack基础
查看>>
前端利器躬行记(4)——webpack进阶
查看>>
前端利器躬行记(5)——Git
查看>>
前端利器躬行记(6)——Fiddler
查看>>
每次阅读外文技术资料都头疼,终于知道原因了。
查看>>
zabbix短信网关调用问题总结
查看>>
130242014034-林伟领-实验一
查看>>
Forbidden You don't have permission to access / on this server.
查看>>
Windows server 2008 R2中安装MySQL !
查看>>
Intellij Idea新建web项目(转)
查看>>
raspberry 安装apache2,使其支持ssl ,并创建自签名证书
查看>>
Trie树:应用于统计和排序
查看>>
C语言结构体和函数
查看>>