POJ 2728:Desert King
2017-05-05 13:59
274 查看
Desert King
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
题目意思:
给出N个村庄的坐标他们之间修建渠道的距离是水平方向的距离,而修建这个渠道的花费是竖直方向的距离。
求一个生成树,是铺设一米的管道费用最小。
最优比率生成树。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005;
const double eps = 0.0001;
int N;
struct point
{
double x;
double y;
double h;
}p[maxn];
double dis(struct point p1,struct point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double cost(struct point p1,struct point p2)
{
return fabs(p1.h-p2.h);
}
double prim(double r)
{
double lowcost[maxn];
int vis[maxn];
int closest[maxn];
double Min;
double length = 0;
double Cost = 0;
int u;
for(int i = 1; i <= N; i++)
{
lowcost[i] = cost(p[1],p[i])-r*dis(p[1],p[i]);
closest[i] = 1;
vis[i] = 0;
}
lowcost[1] = 0;
vis[1] = 1;
for(int i = 1; i < N; i++)
{
Min = inf;
for(int j = 2; j <= N; j++)
{
if(vis[j]==0 && lowcost[j]<Min)
{
Min = lowcost[j];
u = j;
}
}
vis[u] = 1;
int pre = closest[u];
length += dis(p[u],p[pre]);
Cost += cost(p[u],p[pre]);
for(int j = 2; j <= N; j++)
{
double temp = cost(p[j],p[u])-r*dis(p[u],p[j]);
if(vis[j]==0 && temp < lowcost[j])
{
lowcost[j] = temp;
closest[j] = u;
}
}
}
return Cost/length; ///米/元
}
int main()
{
while(~scanf("%d",&N))
{
if(N == 0) break;
for(int i = 1; i <= N; i++)
{
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].h);
}
double ans,temp;
ans = 0;
while(true)
{
temp = prim(ans);
if(fabs(temp-ans)<eps) break;
ans = temp;
}
printf("%.3lf\n",ans);
}
return 0;
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 25635 | Accepted: 7117 |
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
题目意思:
给出N个村庄的坐标他们之间修建渠道的距离是水平方向的距离,而修建这个渠道的花费是竖直方向的距离。
求一个生成树,是铺设一米的管道费用最小。
最优比率生成树。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005;
const double eps = 0.0001;
int N;
struct point
{
double x;
double y;
double h;
}p[maxn];
double dis(struct point p1,struct point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double cost(struct point p1,struct point p2)
{
return fabs(p1.h-p2.h);
}
double prim(double r)
{
double lowcost[maxn];
int vis[maxn];
int closest[maxn];
double Min;
double length = 0;
double Cost = 0;
int u;
for(int i = 1; i <= N; i++)
{
lowcost[i] = cost(p[1],p[i])-r*dis(p[1],p[i]);
closest[i] = 1;
vis[i] = 0;
}
lowcost[1] = 0;
vis[1] = 1;
for(int i = 1; i < N; i++)
{
Min = inf;
for(int j = 2; j <= N; j++)
{
if(vis[j]==0 && lowcost[j]<Min)
{
Min = lowcost[j];
u = j;
}
}
vis[u] = 1;
int pre = closest[u];
length += dis(p[u],p[pre]);
Cost += cost(p[u],p[pre]);
for(int j = 2; j <= N; j++)
{
double temp = cost(p[j],p[u])-r*dis(p[u],p[j]);
if(vis[j]==0 && temp < lowcost[j])
{
lowcost[j] = temp;
closest[j] = u;
}
}
}
return Cost/length; ///米/元
}
int main()
{
while(~scanf("%d",&N))
{
if(N == 0) break;
for(int i = 1; i <= N; i++)
{
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].h);
}
double ans,temp;
ans = 0;
while(true)
{
temp = prim(ans);
if(fabs(temp-ans)<eps) break;
ans = temp;
}
printf("%.3lf\n",ans);
}
return 0;
}
相关文章推荐
- POJ 2728 Desert King 最优比率生成树
- POJ 2728 Desert King
- POJ 2728 Desert King 最优比率生成树
- poj 2728 Desert King (最优比率生成树)
- poj-2728-Desert King-01分数规划+最小生成树
- POJ 2728 Desert King
- POJ 2728 Desert King(初遇最优比率生成树)
- POJ 2728 Desert King 最小生成树 分数规划
- poj 2728 Desert King 二分+O(n^2)最小生成树验证
- POJ 2728 Desert King(最优比率生成树 01分数规划)
- POJ2728 Desert King 最优比例生成树
- poj 2728 Desert King 01分数规划
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
- POJ2728 Desert King 最优比率生成树
- POJ2728 Desert King
- POJ 2728 Desert King (最优比例生成树)
- 【POJ】【2728】 Desert King 最优比率生成树
- POJ 2728 Desert King 最优比例生成树
- POJ 2728 Desert King(最优比例生成树)
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)