最优比率生成树 POJ 2728 迭代或者二分

别人解题报告的链接:

http://blog.sina.com.cn/s/blog_691190870101626q.html

 说明一下关于精度的问题,当结果是精确到小数点后3为,你自然要把误差定为至少10^(-4),我定的是10^(-8)````这里多定点没事的···

然后对于POJ上的提交,如果是用C++提交,可以写printf("%.3lf ",ans);

但是如果是用的G++提交,就得用printf("%.3f ",ans);

当然ans定义的是double型的·····

补充的解题报告链接:

http://blog.csdn.net/sdj222555/article/details/7490797

 1 //#define debug
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 using namespace std;
 6 #define N 1005
 7 #define eps 1e-8
 8 #define INF 1e300
 9 struct point
10 {
11     int x,y,z;
12 } p[N];
13 double dis(point a,point b)
14 {
15     double t = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
16     return sqrt(t);
17 }
18 struct arc
19 {
20     double len,cost;
21 } edge[N][N];
22 double lowcost[N];
23 int pre[N];
24 bool vis[N];
25 int n;
26 double prim(double d)
27 {
28     double tl =0,tc=0,ra;
29     memset(vis,0,sizeof(vis));
30     vis[0] = 1;
31     for(int i=1; i<n; ++i)
32     {
33         lowcost[i] = edge[0][i].cost-d*edge[0][i].len;
34         pre[i] = 0;
35     }
36     for(int k=1; k<n; ++k)
37     {
38         double mi = INF;
39         int v;
40         for(int i=1; i<n; ++i)
41         {
42             if(!vis[i] && lowcost[i] < mi)
43             {
44                 mi = lowcost[i];
45                 v = i;
46             }
47         }
48         vis[v] = 1;
49         tc += edge[v][pre[v]].cost;
50         tl += edge[v][pre[v]].len;
51         for(int i=1; i<n; ++i)
52         {
53             if(!vis[i] && lowcost[i] > edge[v][i].cost-d*edge[v][i].len)
54             {
55                 lowcost[i] = edge[v][i].cost-d*edge[v][i].len;
56                 pre[i] = v;
57             }
58         }
59     }
60     ra = tc/tl;
61     return ra;
62 }
63 int main()
64 {
65 #ifdef debug
66     freopen("in.c","r",stdin);
67 #endif
68     while(scanf("%d",&n),n)
69     {
70         for(int i=0; i<n; ++i)
71             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
72         for(int i=0; i<n; ++i)
73         {
74             for(int j=i+1; j<n; ++j)
75             {
76                 edge[i][j].len = edge[j][i].len = dis(p[i],p[j]);
77                 edge[i][j].cost =  edge[j][i].cost = fabs(p[i].z-p[j].z);
78             }
79         }
80         double r=0;
81         while(true)
82         {
83             double  t = prim(r);
84             if(fabs(t-r) < eps) break;
85             r = t;
86         }
87         printf("%.3f
",r);
88     }
89     return 0;
90 }
View Code
原文地址:https://www.cnblogs.com/allh123/p/3250738.html