Desert King(01分数规划+最小生成树prim算法)
2016-07-22 22:53
435 查看
题目地址 http://poj.org/problem?id=2728
Desert King(01分数规划+最小生成树prim算法)
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.
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
Sample Output
Desert King(01分数规划+最小生成树prim算法)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 23824 | Accepted: 6648 |
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
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
#include <cstdio> #include <string.h> #include <vector> #include <math.h> #include <algorithm> using namespace std; int const MAX = 1100; int const inf = 0x3f3f3f3f; int n; int h[MAX], x[MAX], y[MAX]; double mp[MAX][MAX]; double prim(double mid) { int vis[MAX]; double low[MAX]; double ans = 0, mi; // memset(low, inf, sizeof(low)); memset(vis, 0, sizeof(vis)); int pos = 1; vis[pos] = 1; for(int i = 1; i <= n; i++) low[i] = abs(h[pos]-h[i]) - mid * mp[pos][i]; for(int i = 1; i < n; i++){ mi = inf; pos = -1; for(int j = 1; j <= n; j++) if(!vis[j] && mi > low[j]){ pos = j; mi = low[j]; } if(pos == -1) break; ans += mi; vis[pos] = 1; for(int j = 1; j <= n; j++){ double tmp = abs(h[pos]-h[j]) - mid * mp[pos][j]; if(!vis[j] && low[j] > tmp) low[j] = tmp; } } return ans; } int main() { while(scanf("%d", &n) && n != 0){ for(int i = 1; i <= n; i++){ scanf("%d %d %d", &x[i], &y[i], &h[i]); } for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) mp[i][j] = mp[j][i] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); double l = 0.0, r = 40.0, mid; while((r - l) >= 1e-6){ mid = (l + r) / 2; if(prim(mid) >= 0) l = mid; else r = mid; } printf("%.3f\n", mid); } return 0; }
相关文章推荐
- Spark钨丝计划:让火花(Spark)更接近灯丝(Rare Metal)详解(1)
- 隐隐约约 听 RazorEngine 在 那里 据说 生成代码 很 美。
- 神经网络中的XOR问题
- strlen函数
- Boolean
- JAVA 异常
- C++11 中的右值引用与转移语义
- Java判断字符串是否是整数或者浮点数
- 一道sql的笔试题
- iOS开发 iOS7显示偏差(UITableView下移)解决办法
- hdu 5745 la vie en rose
- Openstack
- java代码显示正确执行,但为何数据未成功插入数据库?原因如下
- ObjectAnimator里面的动画属性值汇总
- 3.2.6工作集
- 实现简单工厂(一)
- 获取本地音乐文件的专辑,图片,音乐信息
- [Unity优化]减少顶点数目
- 关于JavaWeb如何实现用户类似站内信功能
- python DNS处理模块dnspython