1. 程式人生 > >poj2728 Desert King【最優比率生成樹】【Prim】【0/1分數規劃】

poj2728 Desert King【最優比率生成樹】【Prim】【0/1分數規劃】

題目 mem end pst int must out connected har

含【最小生成樹Prim】模板。 Prim復雜度為$O(n^2),適用於稠密圖,特別是完全圖的最小生成樹的求解。 Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions:31622 Accepted: 8670

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

Beijing 2005

題意:

有$n$個城市,每個城市有對應的坐標和海拔。兩個城市之間的距離是坐標的距離,路的花費是海拔之差。

現在要建$n-1$條路,使得花費之和比距離之和最小。

思路:

一道0/1分數規劃的題目。

0/1分數規劃模型指:給定整數$a_1,a_2,...,a_n$,以及$b_1,b_2,...,b_n$,求一組解$x_i(1\leq i \leq n,x_i=0或1)$,

使得$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i}$最大化。

這種類型的題目我們可以通過二分答案來做。

因為如果對於$mid$,存在一組解,使得$\sum_{i=1}^{n}(a_i - mid * b_i) * x_i \geq 0$那麽我們可以變形得到

存在一組解,使得$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i} \geq mid$

也就是說,$mid$比我們實際的答案要小。

反之,如果對於任意一組解,都有$\sum_{i=1}^{n}(a_i - mid * b_i) * x_i <0$那麽我們可以得到

任意的解都有$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i} < mid$

也就是說,$mid$比我們實際的答案要大。

所以我們每次只需要計算$\sum_{i=1}^{n}(a_i - mid * b_i)*x_i$的最大值,如果最大值非負,令$st = mid$,否則$ed = mid$

對於這道題,也是類似。我們二分最終的答案。

每一次重新建圖,城市和城市之間的邊變為$cost - mid * length$

然後在新圖上跑最小生成樹。

如果最小生成樹的值非負,說明實際答案比$mid$要大,令$st = mid$

由於這道題是完全圖,而點的個數只有$1000$所以用prim比較好

WA了好久,後來發現是對d賦初值的問題。

對於double的數組賦初值$+\infty$不能用$0x3f$而應該用$0x7f$,就改了這裏就過了。

  1 #include<iostream>
  2 //#include<bits/stdc++.h>
  3 #include<cstdio>
  4 #include<cmath>
  5 //#include<cstdlib>
  6 #include<cstring>
  7 #include<algorithm>
  8 //#include<queue>
  9 #include<vector>
 10 //#include<set>
 11 //#include<climits>
 12 //#include<map>
 13 using namespace std;
 14 typedef long long LL;
 15 #define N 100010
 16 #define pi 3.1415926535
 17 #define inf 0x3f3f3f3f
 18 
 19 const int maxn = 1005;
 20 const double eps = 1e-7;
 21 int n;
 22 struct city{
 23     double x, y, height;
 24 }c[maxn];
 25 double dist[maxn][maxn], cost[maxn][maxn], g[maxn][maxn];
 26 
 27 double get_dist(int i, int j)
 28 {
 29     return sqrt((c[i].x - c[j].x) * (c[i].x - c[j].x) + (c[i].y - c[j].y) * (c[i].y - c[j].y));
 30 }
 31 
 32 
 33 double d[maxn];
 34 bool vis[maxn];
 35 void prim()
 36 {
 37     memset(d, 0x7f, sizeof(d));
 38     memset(vis, 0, sizeof(vis));
 39     /*for(int i = 1; i <= n; i++){
 40         d[i] = (double)inf;
 41         vis[i] = 0;
 42     }*/
 43     d[1] = 0;
 44     for(int i = 1; i <= n; i++){
 45         int x = 0;
 46         for(int j = 1; j <= n; j++){
 47             if(!vis[j] && (x == 0 || d[j] < d[x]))x = j;
 48         }
 49         vis[x] = 1;
 50         for(int y = 1; y <= n; y++){
 51             if(!vis[y])d[y] = min(d[y], g[x][y]);
 52         }
 53     }
 54 }
 55 
 56 void getgraph(double mid)
 57 {
 58     for(int i = 1; i <= n; i++){
 59         for(int j = i; j <= n; j++){
 60             g[i][j] = g[j][i] = cost[i][j] - mid * dist[i][j];
 61         }
 62     }
 63 }
 64 
 65 bool check(double mid)
 66 {
 67     getgraph(mid);
 68     prim();
 69     double res = 0;
 70     for(int i = 1; i <= n; i++){
 71         res += d[i];
 72     }
 73     return res >= 0;
 74 }
 75 
 76 
 77 int main()
 78 {
 79     while(scanf("%d", &n) != EOF && n){
 80         for(int i = 1; i <= n; i++){
 81             scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].height);
 82         }
 83 
 84         for(int i = 1; i <= n; i++){
 85             for(int j = i; j <= n; j++){
 86                 dist[i][j] = dist[j][i] = get_dist(i, j);
 87                 cost[i][j] = cost[j][i] = fabs(c[i].height - c[j].height);
 88             }
 89         }
 90         double st = 0.0, ed = 100.0;
 91         while(ed - st >= eps){
 92             double mid = (st + ed) / 2;
 93             if(check(mid))st = mid;
 94             else ed = mid;
 95         }
 96         printf("%.3f\n", ed);
 97     }
 98 
 99     return 0;
100 }

poj2728 Desert King【最優比率生成樹】【Prim】【0/1分數規劃】