标签:needed spec space eve vertica string nec cst int
Input
Output
Sample Input
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
题意:在这么一个图中求一棵生成树,这棵树点权和边权之比最大是多少?
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #define N 1007 8 #define eps 0.000001 9 using namespace std; 10 11 int n; 12 double dis[N][N],h[N][N],line[N],p[N][N]; 13 bool vis[N]; 14 struct Node 15 { 16 double x,y,h; 17 }a[N]; 18 19 double get_dis(int x,int y) 20 {return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));} 21 /*struct cmp 22 { 23 bool operator()(int x,int y) 24 {return line[x]>line[y];} 25 };*/ 26 double prim(double num) 27 { 28 for (int i=1;i<=n;i++) 29 for (int j=1;j<=n;j++) 30 p[i][j]=h[i][j]-dis[i][j]*num; 31 //priority_queue<int,vector<int>,cmp>q; 32 //while(!q.empty()) q.pop(); 33 memset(vis,0,sizeof(vis)); 34 memset(line,127,sizeof(line)); 35 line[1]=0; 36 //for (int i=1;i<=n;i++) q.push(i); 37 for (int i=1;i<=n;i++) 38 { 39 int u=0; 40 for (int j=1;j<=n;j++) 41 if (!vis[j]&&line[j]<line[u]) u=j; 42 vis[u]=1; 43 for (int j=1;j<=n;j++) 44 if (!vis[j]) line[j]=min(line[j],p[u][j]); 45 } 46 double sum=0; 47 for (int i=1;i<=n;i++) 48 sum+=line[i]; 49 return sum; 50 } 51 int main() 52 { 53 while(~scanf("%d",&n)&&n) 54 { 55 for (int i=1;i<=n;i++) 56 scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].h); 57 for (int i=1;i<=n;i++) 58 for (int j=1;j<=n;j++) 59 { 60 dis[i][j]=get_dis(i,j); 61 h[i][j]=fabs(a[i].h-a[j].h); 62 } 63 double l=0.0,r=100.0; 64 while(r-l>=eps) 65 { 66 double mid=(l+r)*1.0/2; 67 if (prim(mid)>=0) l=mid; 68 else r=mid; 69 } 70 printf("%.3f\n",l); 71 } 72 }
标签:needed spec space eve vertica string nec cst int
原文地址:http://www.cnblogs.com/fengzhiyuan/p/7678281.html