(最优比例生成树) poj 2728

Desert King

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 21855   Accepted: 6103

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

4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1.000

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define eps 1e-8
#define INF 100000000
using namespace std;
int n;
struct node
{
    double x,y,z;
}e[1010];
double len[1010][1010],cost[1010][1010],mp[1010][1010],low[1010];
bool vis[1010];
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double prim()
{
    memset(vis,0,sizeof(vis));
    double minn,ans=0;
    int pos;
    pos=1,vis[pos]=1;
    for(int i=1;i<=n;i++)
    {
        if(i!=pos)
            low[i]=mp[pos][i];
    }
    for(int i=1;i<n;i++)
    {
        minn=INF;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&low[j]<minn)
            {
                pos=j;
                minn=low[j];
            }
        }
        vis[pos]=1;
        ans+=minn;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&low[j]>mp[pos][j])
                low[j]=mp[pos][j];
        }
    }
    return ans;
}
double solve(double mid)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            mp[i][j]=cost[i][j]-mid*len[i][j];
        }
    }
    return prim();
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf",&e[i].x,&e[i].y,&e[i].z);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                len[i][j]=dis(e[i],e[j]);
                cost[i][j]=abs(e[i].z-e[j].z);
            }
        }
        for(int i=1;i<=n;i++)
            mp[i][i]=0;
        double l,r,mid;
        l=0,r=10000000;
        while(r-l>=eps)
        {
            mid=(l+r)*0.5;
            if(solve(mid)>eps)
                l=mid;
            else
                r=mid;
        }
        printf("%.3f\n",r);
    }
    return 0;
}

  

时间: 2024-10-18 06:42:53

(最优比例生成树) poj 2728的相关文章

poj 2728(Desert King) 最优比例生成树 (分数规划)

这题是最小生成树的一种扩展,就是求一棵树  使得 k =sigma(cost)/ sigma(len)  最小.   其中cost 为每条边花费,len为长度.  这实际上就是一个最优比例生成树.最优比例生成树的求解使用了分数规划的方法.  我们先任取k,假设k是最小值,那么sigma(ccost)-k*sigma(len)==0  .那么我们就新建图边权 为  ccosti-k*leni  .求一次最小生成树,如果生成树权值小于0,那么书名其实k还是有减小的空间的,否则,k不可能是最小.这样我

POJ 2728 Desert King 最优比例生成树

最优比例生成树,迭代方法..... Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21086   Accepted: 5916 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

ZOJ 2728 Desert King(最优比例生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20586   Accepted: 5777 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 coun

poj 2718 最优比例生成树(01分数规划)模板

/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1100 #define eps 1e-10 #define inf 0x3fffffff struct node { int u,v,w; }p[N]; double ma[N][N]; double distance(int i,int j) { return sqrt(1.0*(p[i].u-p[j].u

poj 2728 Desert King 参数搜索解最优比例生成树

题意: 图中每条边有两个权值(cost,len),求其一棵生成树,使sum(cost)/sum(len)最小. 分析: 转化为求边权为s0*len-cost的最大生成树+牛顿迭代.s0为具有单调性迭代系数. 代码: //poj 2728 //sep9 #include <iostream> #include <cmath> #include <vector> using namespace std; const int maxN=1024; struct P { int

最优比例生成树模板

/* 01最优(小)比例生成树 求最大比例更改prim即可 poj2728 将图中点连成一棵树满足 总花费/总长度 最小 */ int n,m; double d[1005],cost[1005],ben[1005]; double pic[1005][1005]; ///两点间距离 double cpic[1005][1005]; ///两点间花费 int vis[1005]; int pre[1005]; ///记录父节点 struct Node{ int x,y,h; double ans

poj2728 Desert King,最优比例生成树

题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求方案使得费用与距离的比值最小 很显然,这个题目是要求一棵最优比率生成树, 0-1分数规划,0-1分数规划是分数规划的一种特殊情况,分数规划适用于求解最优化问题的,对于求最大的对应解,该理论也有效 这是从网上找到的具体的最优比率生成树的方法的讲解 //////////////////// 概念 有带权图G, 对于图中每

01分数规划poj2728(最优比例生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21766   Accepted: 6087 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 coun

【POJ 2728】Desert King

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21361   Accepted: 5974 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 coun