标签:des style blog http color io os java ar
4 0 0 0 0 1 1 1 1 2 1 0 3 0
1.000
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 int cost[maxn][maxn],p[maxn]; 19 int n,x[maxn],y[maxn],z[maxn]; 20 bool vis[maxn]; 21 double d[maxn],w[maxn][maxn]; 22 double Prim(double tmp) { 23 double sumx = 0,sumy = 0; 24 for(int i = 1; i <= n; i++) { 25 p[i] = 1; 26 d[i] = cost[1][i] - tmp*w[1][i]; 27 vis[i] = false; 28 } 29 vis[1] = true; 30 d[1] = 0; 31 for(int i = 2; i <= n; i++) { 32 double MST = INF; 33 int index = -1; 34 for(int j = 2; j <= n; j++) 35 if(!vis[j] && d[j] < MST) MST = d[index = j]; 36 vis[index] = true; 37 sumx += cost[p[index]][index]; 38 sumy += w[p[index]][index]; 39 for(int j = 1; j <= n; j++) 40 if(!vis[j] && d[j] > cost[index][j] - tmp*w[index][j]) { 41 d[j] = cost[index][j] - tmp*w[index][j]; 42 p[j] = index; 43 } 44 } 45 return sumx/sumy; 46 } 47 int main() { 48 while(scanf("%d",&n),n) { 49 for(int i = 1; i <= n; i++) { 50 scanf("%d %d %d",x+i,y+i,z+i); 51 for(int j = 1; j < i; j++) { 52 cost[i][j] = cost[j][i] = abs(z[i] - z[j]); 53 double tmp = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]); 54 w[i][j] = w[j][i] = sqrt(tmp); 55 } 56 } 57 double ans = 0; 58 while(true) { 59 double tmp = Prim(ans); 60 if(fabs(ans - tmp) < (1e-4)) break; 61 ans = tmp; 62 } 63 printf("%.3f\n",ans); 64 } 65 return 0; 66 }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3986113.html