Poj2728 desert king, optimal proportion Spanning Tree

Source: Internet
Author: User
There are n villages in different coordinates and heights. Now we need to supply water to all the villages, as long as there is a road between the two villages,
The distance between the construction pipes is the Euclidean distance between the coordinates (like the Euclidean distance), and the cost is the elevation difference.
Now, the solution is required to minimize the cost-to-distance ratio.
Obviously, this question requires an optimal rate spanning tree,


0-1 score planning: 0-1 score planning is a special case of score planning. Score planning is suitable for solving the optimization problem. This theory is also effective for finding the maximum logarithm.
This is an explanation of the specific optimal rate spanning tree method found on the Internet.
////////////////////
Concept
There is a weighted graph G. For each E [I] in the graph, there are benifit [I] (revenue) and cost [I] (cost ), what we need is a spanning tree T, which makes Sigma (benifit [I])/Σ (cost [I]), iε t maximum (or minimum ).
This is obviously a practical issue.
Solution 1: 0-1 score Planning
If X [I] is equal to 1 or 0, it indicates whether edge e [I] belongs to the Spanning Tree.
Then the ratio r = Σ (benifit [I] * X [I])/Σ (cost [I] * X [I]), 0 ≤ I <m.
To maximize R, design a subproblem ---> let Z = Σ (benifit [I] * X [I]) -L * Σ (cost [I] * X [I]) = Σ (d [I] * X [I]) the maximum value (d [I] = benifit [I]-l * cost [I]) is Z (l ). we can happily regard Z (l) as the total weight of the largest spanning tree with D as edge weight.
Then define two properties:
1. monotonically decreasing Z
Proof: Because cost is a positive number, Z increases with the decrease of L.
2. Z (max (R) = 0
Proof: If Z (max (R) <0, Σ (benifit [I] * X [I])-max (r) * sigma (cost [I] * X [I]) <0, which can be converted to max (r) <max (R ). conflict;
If Z (max (R) is greater than or equal to 0, the R is the largest when Z is equal to 0.
At this point, all the seven S have been connected, like the top two of the two, like dinkelbach.

Complexity
Time O (MST) * log max (r ))
Space O (MST ))
/////////////////////////////
I have found a thesis on score planning, which details the score planning.
Application of the minimal cut model in the competition for Informatics
In the black book, the method for finding the Minimum Spanning Tree with O (n) is not found.

Iteration + prim

# Include <cmath> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <set> # include <map> # include <stack> # include <queue> # include <vector> # include <string> # define for0 (, b) For (a = 0; A <B; ++ A) # define for1 (a, B) for (a = 1; A <= B; ++) # define foru (I, a, B) for (I = A; I <= B; ++ I) # define Ford (I, a, B) for (I = A; I> = B; -- I) using namespace STD; typedef long ll; const int maxn = 1000 + 5; const I NT maxm = 100005; const int INF = 1e9; Double X [maxn], Y [maxn], Z [maxn]; double cost [maxn] [maxn], dist [maxn] [maxn]; int N; double dist (int I, Int J) {return SQRT (X [I]-X [J]) * (X [I]-X [J]) + (Y [I]-y [J]) * (Y [I]-y [J]);} void Init () {int I, j; for1 (I, n) scanf ("% lf", & X [I], & Y [I], & Z [I]); for1 (I, n) foru (J, I + 1, n) {Dist [I] [J] = DIST [J] [I] = dist (I, j ); cost [I] [J] = cost [J] [I] = FABS (Z [I]-Z [J]);} int Vis [maxn], pre [maxn]; double dis [maxn]; double prim (Double P) {int I, j; memset (VIS, 0, sizeof vis ); vis [1] = 1; foru (I, 2, n) {dis [I] = cost [1] [I]-Dist [1] [I] * P; pre [I] = 1;} double cost = 0, Len = 0; for0 (I, n-1) {double mincost = inf; int K =-1; for1 (J, n) {If (! Vis [J] & dis [J] <mincost) {mincost = dis [k = J] ;}} if (k =-1) break; vis [k] = 1; Cost + = cost [pre [k] [k]; Len + = DIST [pre [k] [k]; for1 (J, n) {double TMP = cost [k] [J]-Dist [k] [J] * P; If (! Vis [J] & dis [J]> TMP) {dis [J] = TMP; Pre [J] = K ;}} return cost/Len ;} void solve () {// Newton Iteration double X0 = 0, x = 0; while (true) {x = prim (x0); If (FABS (x-x0) <1e-4) break; X0 = x;} printf ("%. 3f \ n ", x);} int main () {# ifndef online_judge freopen (" in. CPP "," r ", stdin); freopen (" out. CPP "," W ", stdout); # endif // online_judge int I, j; while (~ Scanf ("% d", & N) {If (n = 0) break; Init (); solve ();} return 0 ;}


Poj2728 desert king, optimal proportion Spanning Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.