码迷,mamicode.com
首页 > 其他好文 > 详细

POJ——T 2728 Desert King

时间:2017-10-15 21:27:44      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:sqrt   let   limit   prim   line   case   esc   inline   with   

http://poj.org/problem?id=2728

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27191   Accepted: 7557

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

 
 
最优比例生成树,对于每条边有两个权值(a,b),求得ans=min( suma / sumb )
二分一个ans,判断 ai-bi*ans与0 的关系
 
技术分享
 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <cmath>
 4 
 5 #define max(a,b) (a>b?a:b)
 6 #define min(a,b) (a<b?a:b)
 7 
 8 inline void read(int &x)
 9 {
10     x=0; register char ch=getchar();
11     for(; ch>9||ch<0; ) ch=getchar();
12     for(; ch>=0&&ch<=9; ch=getchar()) x=x*10+ch-0;
13 }
14 
15 const double eps(1e-4);
16 const int N(1026);
17 struct Node {
18     int x,y,h;
19 }city[N];
20 int n;
21 
22 double L,R,Mid,ans;
23 struct Edge {
24     int u,v;
25     double w;
26     Edge(int u=0,int v=0,double w=0.0):u(u),v(v),w(w){}
27     bool operator < (const Edge&x)const { return w<x.w; }
28 }road[N*N];
29 
30 inline double Dis(Node a,Node b)
31 {
32     double x=1.0*(a.x-b.x)*(a.x-b.x);
33     double y=1.0*(a.y-b.y)*(a.y-b.y);
34     return abs(a.h-b.h)-Mid*sqrt(x+y);
35 }
36 
37 int fa[N];
38 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
39 
40 inline bool check()
41 {
42     double ret=0; int cnt=0,m=0;
43     for(int i=1; i<=n; fa[i]=i++)
44       for(int j=1; j<=n; ++j)
45         if(i!=j) road[++m]=Edge(i,j,Dis(city[i],city[j]));
46     std::sort(road+1,road+m+1);
47     for(int fx,fy,i=1; i<=m; ++i)
48     {
49         fx=find(road[i].u),
50         fy=find(road[i].v);
51         if(fx==fy) continue;
52         fa[fx]=fy; ret+=road[i].w;
53         if(++cnt==n-1) return ret<0;
54     }
55 }
56 
57 int Presist()
58 {
59     for(; scanf("%d",&n)&&n; )
60     {
61         for(int i=1; i<=n; ++i)
62         {
63             read(city[i].x),
64             read(city[i].y),
65             read(city[i].h),
66             R=max(R,1.0*city[i].h);
67         }
68         for(L=0; L+eps<R; )
69         {
70             Mid=(L+R)/2.0;
71             if(check()) R=Mid;
72             else L=Mid;
73         }
74         printf("%.3lf\n",R);
75     }
76     return 0;
77 }
78 
79 int Aptal=Presist();
80 int main(int argc,char**argv){;}
T掉的Kruskal
 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 
 6 #define max(a,b) (a>b?a:b)
 7 
 8 inline void read(int &x)
 9 {
10     x=0; register char ch=getchar();
11     for(; ch>9||ch<0; ) ch=getchar();
12     for(; ch>=0&&ch<=9; ch=getchar()) x=x*10+ch-0;
13 }
14 
15 const double INF(10000000.0);
16 const double eps(1e-9);
17 const int N(1026);
18 
19 double h[N][N],d[N][N];
20 struct Node {
21     int x,y,h;
22 }city[N];
23 int n;
24 
25 double L,R,Mid,ans,dis[N];
26 bool vis[N];
27 
28 inline double Dis(Node a,Node b)
29 {
30     double x=1.0*(a.x-b.x)*(a.x-b.x);
31     double y=1.0*(a.y-b.y)*(a.y-b.y);
32     return (double)sqrt(x+y);
33 }
34 
35 inline bool check()
36 {
37     for(int i=1; i<=n; ++i) vis[i]=0;
38     for(int i=2; i<=n; ++i) dis[i]=h[1][i]-Mid*d[1][i];
39     double ret=0.0,minn; vis[1]=1;
40     for(int i=2,u; i<=n; ++i)
41     {
42         minn=INF;
43         for(int j=2; j<=n; ++j)
44           if(!vis[j]&&minn>dis[j]) minn=dis[u=j];
45         if(minn==INF) break;
46         ret+=minn;    vis[u]=1;
47         for(int v=2; v<=n; ++v)
48           if(!vis[v]&&dis[v]>h[u][v]-Mid*d[u][v])
49               dis[v]=h[u][v]-Mid*d[u][v];
50     }
51     return ret<=0;
52 }
53 
54 int Presist()
55 {
56     for(; scanf("%d",&n)&&n; )
57     {
58         for(int i=1; i<=n; ++i)
59         {
60             read(city[i].x),
61             read(city[i].y),
62             read(city[i].h),
63             R=max(R,city[i].h);
64         }
65         for(int i=1; i<=n; ++i)
66           for(int j=i+1; j<=n; ++j)
67           {
68               d[i][j]=d[j][i]=Dis(city[i],city[j]);
69               h[i][j]=h[j][i]=abs(city[i].h-city[j].h)*1.0;
70           }
71         for(L=0; L+eps<R; )
72         {
73             Mid=(L+R)/2.0;
74             if(check()) R=Mid;
75             else L=Mid;
76         }
77         printf("%.3lf\n",R);
78     }
79     return 0;
80 }
81 
82 int Aptal=Presist();
83 int main(int argc,char**argv){;}

 

POJ——T 2728 Desert King

标签:sqrt   let   limit   prim   line   case   esc   inline   with   

原文地址:http://www.cnblogs.com/Shy-key/p/7673898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!