POJ-2728-Desert king

Source: Internet
Author: User

POJ-2728-Desert king

Http://poj.org/problem? Id = 2728

There are n villages, and the coordinates and altitude of each village are given. benifit is the distance between two points, and cost is the height difference between two points. Now a tree is required to minimize the cost/benift, that is, finding an optimal proportional Spanning Tree

The first time I met this kind of spanning tree, I found a solution on the Internet.

Assume that sigma (h)/SIGMA (L) = K means K is desirable, that is, SIGMA (H) = K * sigma (l)

SIGMA (H) = K * (l1 + l2 + l3 +... lm)

SIGMA (H) = K * l1 + K * l2 + K * L3 +... K * LM

Subtract K * l from the H of each edge.

That is, Hi' = Hi-li '= Hi-li * k

Then the problem can be converted to the Minimum Spanning Tree of the 'Hi' edge.

If the Hi' edge has the minimum generated tree weight and <= 0.0, the mean value of K is desirable.

For K, you can solve the problem in binary mode.

CodeIt basically imitates the internet.

# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # include <cmath> using namespace STD; # define maxn 1010 # define INF 9999999999 struct point {Double X, Y, H ;}; point [maxn]; double mat [maxn] [maxn]; double H [maxn] [maxn]; double L [maxn] [maxn]; int N; double head, tail; double dist (point & A, point & B) {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} double prim () {int I, j, k; double ans = 0.0, m In; int visit [maxn]; double dis [maxn]; for (I = 0; I <n; I ++) {dis [I] = mat [0] [I]; visit [I] = 0;} dis [0] = 1; visit [0] = 1; for (I = 1; I <n; I ++) {min = inf; For (j = 0; j <n; j ++) if (! Visit [J] & dis [J] <min) {min = dis [J]; k = J;} ans + = min; visit [k] = 1; for (j = 0; j <n; j ++) if (! Visit [J] & dis [J]> mat [k] [J]) dis [J] = mat [k] [J];} return ans ;} int main () {int I, j; double maxc, minc, maxl, minl, mid; while (scanf ("% d", & N), n) {maxc =-INF; minc = inf; maxl =-INF; minl = inf; for (I = 0; I <n; I ++) scanf ("% lf", & point [I]. x, & point [I]. y, & point [I]. (h); for (I = 0; I <n-1; I ++) for (j = I + 1; j <n; j ++) {H [I] [J] = H [J] [I] = FABS (point [I]. h-point [J]. h); L [I] [J] = L [J] [I] = dist (point [I], point [J]); if (maxc <H [I] [J]) maxc = H [I] [J]; If (minc> H [I] [J]) minc = H [I] [J]; If (maxl <L [I] [J]) maxl = L [I] [J]; if (minl> L [I] [J]) minl = L [I] [J];} head = minc/maxl; tail = maxc/minl; while (tail-head> 1e-4) {mid = (Head + tail)/2; for (I = 0; I <n-1; I ++) for (j = I + 1; j <n; j ++) mat [I] [J] = mat [J] [I] = H [I] [J]-mid * l [I] [J]; If (prim () <= 0.0) tail = mid; elsehead = mid;} printf ("%. 3f \ n ", head);} return 0 ;}

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.