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
7
int n;
8
int x[MAX],y[MAX],z[MAX];
9
double dist[MAX][MAX],cost[MAX][MAX],dis[MAX];
10
short vis[MAX];
11
double b,a;
12
double 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
}
57
int 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