poj_2728 Desert King(最优比率生成树+01分数规划+二分+prim)
来源:互联网 发布:互联网大数据的应用 编辑:程序博客网 时间:2024/05/21 06:30
Desert King
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 25081 Accepted: 6951
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
求一棵花费和与路径和的比值最小的生成树,输出比值。即求一个R <= ∑cost[i][j] * x[i][j] / ∑dist[i][j] * x[i][j]
(x[i][j]表示边i->j是否在最小生成树中,是则等于1,反之为0)构造函数F(R) = ∑(cost[i][j] - R * dist[i][j]) * x[i][j]则当F(R)为0时的R为所求,二分求解即可。#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Point{ int x, y, z;}p[maxn];int n;double dist[maxn][maxn];double cost[maxn][maxn];double get_dist(Point a, Point b){ double x = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); return sqrt(x);}double get_cost(Point a, Point b){ return abs(a.z-b.z);}bool vis[maxn];double dis[maxn];double prim(double mid){ memset(vis, 0, sizeof(vis)); double ans = 0; int mi, pos; dis[1] = 0, vis[1] = 1; for(int i = 2; i <= n; i++) dis[i] = cost[1][i] - mid * dist[1][i]; for(int i = 2; i <= n; i++) { mi = inf; for(int j = 2; j <= n; j++) { if(!vis[j] && mi > dis[j]) mi = dis[j], pos = j; } vis[pos] = 1; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] > cost[pos][j] - mid * dist[pos][j]) dis[j] = cost[pos][j] - mid * dist[pos][j]; } } for(int i = 1; i <= n; i++) ans += dis[i]; return ans;}int main(){ while(~scanf("%d", &n) && n) { for(int i = 1; i <= n; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z); double l = 0, r = 0, mid; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { dist[i][j] = get_dist(p[i], p[j]); cost[i][j] = get_cost(p[i], p[j]); r += cost[i][j]; } } while(l+1e-6 < r) { mid = (l+r)/2; if(prim(mid) > 0) l = mid; else r = mid; } printf("%.3f\n", mid); } return 0;}
0 0
- poj_2728 Desert King(最优比率生成树+01分数规划+二分+prim)
- poj 2728 Desert King(最优比率生成树,01分数规划)
- POJ 2728 Desert King (最优比率生成树 01分数规划问题)
- poj 2728 Desert King (最优比率生成树/01分数规划)
- poj 2728 Desert King(01分数规划--最优比率生成树)
- POJ 2728 Desert King(最优比率生成树) prim+二分
- poj2728 Desert King(最优比率生成树,二分答案+prim)
- poj 2728 Desert King 最优比率生成树 分数规划
- 【分数规划,最优比率生成树】POJ 2728 Desert King
- 【分数规划】【最优比率环 & 最优比率生成树】poj3621 Sightseeing Cows && poj2728 Desert King
- 【POJ】2728 Desert King 最优比率生成树——01分数规划【经典】
- POJ 2728 Desert King (最优比率生成树---01分数规划)
- POJ 2728-Desert King(01分数规划_最优比率生成树)
- POJ 2728 Desert King 01分数规划 最优比率生成树
- Desert King (poj 2728 最优比率生成树 0-1分数规划)
- POJ 2728 Desert King(01分数规划+二分+最小生成树-Prim)
- poj2728 Desert King 【最优比例生成树 01分数规划】
- poj Desert King 最优比例生成树 01分数规划
- java框架学习日志
- javascript中的this
- 主流浏览器内核介绍(前端开发值得了解的浏览器内核历史)
- MD5
- 使用MyEclipse工具简化Hibernate数据库开发
- poj_2728 Desert King(最优比率生成树+01分数规划+二分+prim)
- C#Treeview在拖拽时实现Image dragging效果
- MM60增强
- Android音视频基础 mp3和aac格式有什么区别
- C++继承:公有,私有,保护
- 熬,是人生最深的滋味
- Mybatis3+Spring4+SpringMVC4 整合
- 测试用例中截取异常图片时,单独存储一个目录,并记录到测试执行记录中
- Android之BroadcastReceiver详解