Desert King【二分答案】【Prime算法】【最优比例生成树】

  最近一段时间,POJ似乎炸了,于此同时,心态也有点爆炸,这样的一道题与这样一道题【简单的二分答案】有点类似,不过是升华版了。

  题目让我们求通道总成本比上总长度的值最小,也就是说高度的总和/水平距离的总和最小,那么我们用大化小策略,将总的分成一部分子块的和:化为h/len=ans,且使得ans尽可能小,此处就用到了二分答案思路,假设我们知道了这样一个答案(mid),然后将其套回原来的公式,如果h-mid*len>=0,就说明mid的值还是偏小,只有增大mid才能使mid尽量接近答案;反之亦然。

  接下来说到了对于图的处理,我们将原来的点换成线来处理,不然也没法处理然后将所有的线进入处理,线的长度可不是水平长度,我们把所要求的h-mid*len看作线长(所以每次更新mid后,线长也要更新),然后就是求最短生成树了,最后生成的树长就是此情况下的情况了。

  利用Prime算法,先放进起点,也就是‘1’那点,然后对于所有与‘1’邻接的点进行处理,一一更新最短路径,生成最小生成树。

题面:

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

 

完整代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const double MaxDouble=100000007.0;
double mid,L,R;     //初始化L=R=0,然后改变R的值
double lowercost[1005];
bool vis[1005];
int N,cnt;      //cnt用来标示线的数量
struct node
{
    int x,y,h;
}city[1005];
double cost[1005][1005];
double mp[1005][1005];
bool prime()
{
    double minn,res=0;
    int pos;
    memset(vis, false, sizeof(vis));
    vis[1]=true;
    pos=1;
    for(int i=1; i<=N; i++) lowercost[i]=(double)cost[pos][i]-mid*mp[pos][i];
    for(int i=0; itemp) lowercost[j]=temp;
        }
    }
    if(res>=0) return true;
    else return false;
}
double erfen()
{
    while(fabs(R-L)>1e-5)
    {
        mid=(L+R)/2.;
        if(prime()) L=mid;
        else R=mid;
    }
    return L;
}
int main()
{
    while(scanf("%d",&N)&&N)
    {
        L=R=0;
        cnt=0;
        memset(cost, 0, sizeof(cost));
        memset(mp, 0, sizeof(mp));
        for(int i=1; i<=N; i++)
        {
            scanf("%d%d%d",&city[i].x, &city[i].y, &city[i].h);
        }
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j

 

你可能感兴趣的:(图论,ACM,二分答案,Prime算法,最优比例生成树)