POJ2728 Desert King-(0/1) Fractional planning

Source: Internet
Author: User

The problem is to find a solution that makes the diagram fully connected and minimizes all side costs and distances
\ (\sum_{i∈e}cost_i\) divided by \ (\sum_{i∈e}dis_i\) min
Two-part solution can be considered
You can assume that this value is less than or equal to L when there is a solution, and then check if there is a solution, if there is no explanation l take small
The question is why to assume the "existence", in fact, if the assumption of "arbitrary", then it is necessary to check every possible is less than, it is very troublesome, so it is the best to ask for an arbitrary existence
But this solution is hard to find ... It is not possible to test each one, but the numbers except L are input data.
To deform a formula, you have to:
\[l*\sum_{i∈e}dis_i-\sum_{i∈e}cost_i >= 0\]
\[\sum_{i∈e}dis_i*l-\sum_{i∈e}cost_i >= 0\]
The fractional plan is to find a relationship through the column, and finally the solution problem of existence is transformed into a positive and negative problem.
Transform the problem into a problem by changing the formula flexibly
For example, to turn some problems into negative loops, if the negative ring, then the answer is feasible , so that one solution to determine whether there is a solution to the problem form is multiply a minus, bashi to less than or equal to 0
In addition, the role of EPs, because the second is the real number, and because the accuracy of the problem L and R will never coincide, then it is necessary to set up EPs, when L and R difference is less than EPS when they think they are the same, and judge positive or negative when not required, because then I did take small
Corresponding to the Angry birds that the problem, the parabola because of the accuracy of the target, but it is supposed to hit the

pay attention to two points when the real number two may use bitwise operation instead of (L+R)/2 not very good ... After all, not integers .

Hey, the upper bound of L is difficult to estimate, the big will be T, I took to 1000 cards over the past ...

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <  cmath>using namespace std; #define DEBUG (x) cerr << #x << "=" << x << endl;const int MAXN = 1000 + 10;const Double EPS = 1e-6;const int INF = (1<<30)/3;typedef long long ll; int N,LAST[MAXN],TOT,FA[MAXN],VIS[MAXN];d ouble ans, esum, l,ttem[maxn][maxn],ddis[maxn][maxn],d[maxn],gra[maxn][ maxn];struct viii{int x, y, Z;}    Vil[maxn];int abab (int x) {if (x < 0) return-x; return x;}    void Prim () {for (int i=1; i<=n; i++) {d[i] =-inf;    } memset (Vis, 0, sizeof (VIS));        for (int i=1; i<=n; i++) {int x = 0;        for (int j=1; j<=n; J + +) {if (!vis[j] && (x = = 0 | | d[j] > D[X])) x = j;        } Vis[x] = 1;        for (int j=1; j<=n; J + +) {if (!vis[j]) d[j] = max (D[j], gra[x][j]);        }}}int Main () {while (1) {esum = 0.0;         CIN >> N; if (n = = 0) break;         for (int i=1; i<=n; i++) {cin >> vil[i].x >> vil[i].y >> vil[i].z;                } for (int i=1, i<=n; i++) {for (int j=1; j<=n; J + +) {Double temp = 0;                int sum = 0;                int x1 = vil[i].x, y1 = vil[i].y, x2 = vil[j].x, y2 = vil[j].y;                sum = (x1-x2) * (X1-X2) + (y1-y2) * (Y1-Y2);                temp = (double) sum;                temp = sqrt (temp);                int dist = ABAB (vil[i].z-vil[j].z);                TTEM[I][J] = ttem[j][i] = dist;                DDIS[I][J] = ddis[j][i] = temp;            Esum + = temp;        }}//double L = 0, r = esum;        Double L = 0, r = 1000;            while (r-l >= EPS) {Double mid = (l+r)/2;            tot = 0;                        for (int i=1, i<=n; i++) {for (int j=1; j<=n; J + +) {if (i! = j) GRA[I][J] = Gra[j][i] = ddis[i][j] * mid-tTEM[I][J];            }} double mst = 0.0;            Prim ();            for (int i=2; i<=n; i++) {MST + = D[i];                } if (MST >= 0) {r = Mid;            Ans = mid;            } else {L = mid;    }} printf ("%.3lf\n", ans); } return 0;}

POJ2728 Desert King-(0/1) fractional planning

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.