Poj2728 least Ratio Spanning Tree/0-1 score planning/binary/iteration (iterative)

Source: Internet
Author: User

With the 01 score plan + prime + second point, the Qaq was passed through the 2950ms thrilling experience.

The premise is that the =

Question meaning:
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 water pipes is the Euclidean distance between the coordinates, the cost is the difference between the altitude and the distance. Now the scheme is required to minimize the cost to the distance. Obviously, this question requires an optimal rate spanning tree.

Solution:

The answer is divided into two parts. When the replaced answer is used to calculate the Minimum Spanning Tree, once the total path length is 0, the answer is required.


What is 0-1 Planning?
 
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.

 

Code:

 1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler 2 #include <stdio.h> 3 #include <iostream> 4 #include <climits> 5 #include <cstring> 6 #include <cmath> 7 #include <stack> 8 #include <vector> 9 #include <algorithm>10 #define ll long long11 using namespace std;12 13 const int INF = 0x3f3f3f3f;14 const int MAXN = 1001;15 struct node{16     double x,y,h;17 }dot[MAXN];18 19 double map[MAXN][MAXN];20 int n;21 double dis(double x1,double y1,double x2,double y2){22     return sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) );23 }24 25 void creat(int n,double l){26     for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)27         map[i][j]=fabs(dot[i].h-dot[j].h) - l * dis(dot[i].x,dot[i].y,dot[j].x,dot[j].y);28 }29 30 double prim(){31     bool vis[MAXN];32     memset(vis, 0, sizeof(vis));33     double dis[MAXN];34     double ans = 0;35     int i,j;36     vis[1] = true;37     for(i = 1; i <= n; ++i)38         dis[i] = map[1][i];39     for(i = 1; i < n; ++i){40         int temp = INF, flag;41         for(j = 1; j <= n; ++j){42             if(!vis[j] && dis[j] <= temp){43                 temp = dis[j];44                 flag = j;45             }46         }47         vis[flag] = true;48         ans += dis[flag];49         for(j = 1; j <= n; ++j){50             if(!vis[j] && map[flag][j] < dis[j])51                 dis[j] = map[flag][j];52         }53     }54     return ans;55 }56 57 int main(){58     int i,j;59     double res,front,rear, mid;60     while(EOF != scanf("%d",&n)){61         if(n==0)    break;62         for(i=1;i<=n;i++)63             scanf("%lf%lf%lf",&dot[i].x,&dot[i].y,&dot[i].h);64         front = 0;65         rear = 100.0;//doubt66         while(front <= rear){67             mid = (front + rear) / 2;68             creat(n,mid);69             res = prim();70             if(fabs(res) < 1e-4)71                 break;72             else if(res > 1e-4)73                 front = mid;74             else75                 rear = mid;76         }77         printf("%.3f\n",mid);78     }79     return 0;80 }

 

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.