博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2728 Desert King (最优比率生成树)
阅读量:4684 次
发布时间:2019-06-09

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

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 23729   Accepted: 6631

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

40 0 00 1 11 1 21 0 30

Sample Output

1.000 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 这个其实应该是属于01数分规划里面的 迭代很快但是我不会 就二分吧 二分这个比率 double cost(double mid,int i,int j){ return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z)); } 上面这个是二分枚举比率的式子 if (ret>eps) return 1; return 0; 这个是判断条件 01其实对应的是选和不选
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 using namespace std;12 #define bug printf("******\n");13 const int maxn = 1e6 + 10;14 #define rtl rt<<115 #define rtr rt<<1|116 const double eps=1e-6;17 int n,vis[1010];18 double mp[1010][1010],p[1010];19 struct node {20 double x,y,z;21 }a[1010];22 double cost(double mid,int i,int j){23 return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z));24 }25 int pri(double mid) {26 for (int i=0 ;i<=n ;i++) vis[i]=0,p[i]=-100000000;27 p[0]=0;28 double ret=0;29 for (int i=0 ;i
maxx) {35 maxx=p[j];36 idx=j;37 }38 }39 if (idx==-1) break;40 vis[idx]=1;41 ret+=maxx;42 for (int j=0 ;j
eps) return 1;48 return 0;49 }50 int main() {51 while(scanf("%d",&n),n){52 for (int i=0 ;i

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9415782.html

你可能感兴趣的文章
appium+python 环境搭建
查看>>
WampServer下修改和重置MySQL密码
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
Part2_lesson3---ARM寄存器详解
查看>>
深入理解vsto,开发word插件的利器
查看>>
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同!
查看>>
导入properties时的坑
查看>>
python——网络编程
查看>>
Spark的39个机器学习库
查看>>
Electron学习笔记(一)
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
查看>>
配置NRPE的通讯
查看>>
VS2005编译VTK5.10.1
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
总结上海永辉云商高级前端职位面试题集
查看>>
中国计算机学会推荐国际学术会议和期刊目录
查看>>
文本元素
查看>>
各种可以远程
查看>>
对服务器的认识
查看>>