poj2728

Desert King

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15105 Accepted: 4251

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

题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,

         建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差

         现在要求方案使得费用与距离的比值最小

很显然,这个题目是要求一棵最优比率生成树,

以前没写过这种题目

怎么做呢

0-1分数规划,0-1分数规划是分数规划的一种特殊情况,分数规划适用于求解最优化问题的,对于求最大的对应解,该理论也有效

这是从网上找到的具体的最优比率生成树的方法的讲解

////////////////////

概念

有带权图G, 对于图中每条边e[i], 都有benifit[i](收入)和cost[i](花费), 我们要求的是一棵生成树T, 它使得 ∑(benifit[i]) / ∑(cost[i]), i∈T 最大(或最小).

这显然是一个具有现实意义的问题.

 

解法之一 0-1分数规划

设x[i]等于1或0, 表示边e[i]是否属于生成树.

则我们所求的比率 r = ∑(benifit[i] * x[i]) / ∑(cost[i] * x[i]), 0≤i<m .

为了使 r 最大, 设计一个子问题---> 让 z = ∑(benifit[i] * x[i]) - l * ∑(cost[i] * x[i]) = ∑(d[i] * x[i]) 最大 (d[i] = benifit[i] - l * cost[i]) , 并记为z(l). 我们可以兴高采烈地把z(l)看做以d为边权的最大生成树的总权值.

 


然后明确两个性质:

 1.  z单调递减

  证明: 因为cost为正数, 所以z随l的减小而增大.

 2.  z( max(r) ) = 0

  证明: 若z( max(r) ) < 0, ∑(benifit[i] * x[i]) - max(r) * ∑(cost[i] * x[i]) < 0, 可化为 max(r) < max(r). 矛盾;

          若z( max(r) ) >= 0, 根据性质1, 当z = 0 时r最大.

到了这个地步, 七窍全已打通, 喜欢二分的上二分, 喜欢Dinkelbach的就Dinkelbach.

 

复杂度

时间 O( O(MST) * log max(r) )

空间 O( O(MST) )


/////////////////////////////

关于分数规划的学习我找到了一篇论文,里面有讲分数规划,特别详细

算法合集之《最小割模型在信息学竞赛中的应用》

黑书上说求最小生成树有O(n)的方法,没去找

代码很纠结

迭代+prim

 1#include<stdio.h>
 2#include<string.h>
 3#include<math.h>
 4#define inf 0x7ffffff
 5#define eps 0.0001
 6#define MAX 1100
 7int n;
 8int x[MAX],y[MAX],z[MAX];
 9double dist[MAX][MAX],cost[MAX][MAX],dis[MAX];
10short vis[MAX];
11double b,a;
12double prim(double p)
13{
14    int i,j,k,pre[MAX];
15    double mincost,totcost,totdist,v;
16    for(i=1; i<=n; i++)
17    {
18        pre[i]=1;
19    }

20    memset(vis,0,sizeof(vis));
21    vis[1]=1;
22    totcost=0;
23    totdist=0;
24    dis[1]=0;
25    for(j=2; j<=n; j++)
26    {
27        dis[j]=cost[j][1]-p*dist[j][1];
28    }

29    for(i=2; i<=n; i++)
30    {
31        mincost=inf;
32        for(j=2; j<=n; j++)
33            if((!vis[j])&&(mincost>dis[j]))
34            {
35                mincost=dis[j];
36                k=j;
37            }

38
39        vis[k]=1;
40        totcost+=cost[pre[k]][k];
41        totdist+=dist[pre[k]][k];
42        for(j=1; j<=n; j++)
43        {
44            if(!vis[j])
45            {
46                v=cost[k][j]-p*dist[k][j];
47                if (v<dis[j])
48                {
49                    dis[j]=v;
50                    pre[j]=k;
51                }

52            }

53        }

54    }

55    return totcost/totdist;
56}

57int main()
58{
59    int i,j;
60    while(scanf("%d",&n)!=EOF&&n!=0)
61    {
62        for(i=1; i<=n; i++)
63            scanf("%d%d%d",&x[i],&y[i],&z[i]);
64        for (i=1; i<=n; i++ )
65        {
66            for(j=i+1; j<=n; j++)
67            {
68                b=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
69                dist[i][j]=sqrt(b);
70                dist[j][i]=dist[i][j];
71                cost[i][j]=z[i]-z[j];
72                if (cost[i][j]<0)    cost[i][j]=-cost[i][j];
73                cost[j][i]=cost[i][j];
74            }

75        }

76        a=0;
77        while(1)
78        {
79            b=prim(a);
80            if(fabs(b-a)<eps)
81                break;
82            a=b;
83        }

84        printf("%.3lf\n",b);
85    }

86    return 0;
87}

88
89
90/*
91    最有比率生成树
92    01分数规划
93    分数规划的特例
94*/

95


posted on 2012-03-12 23:47 jh818012 阅读(933) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论