标签:
poj2728:题目链接
题目大意:给出n个村庄的坐标和高度,给这n个村庄修n-1水管,连接起n个村庄,两个村庄之间修水管的花费是高度差,距离是欧几里得距离(空间距离),要求修的水管的花费和/距离和最小。
按0-1规划来做,注意求最小生成树的时候,用prim,因为边会有n^2条。用c++提交
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std ; #define eqs 1e-6 #define INF 0x3f3f3f3f struct point{ double x , y , z ; }p[1100] ; int vis[1100] , n ; double dis[1100] ; double low , mid , high ; double f(int i,int j,double mid) { return fabs(p[i].z-p[j].z) - mid*sqrt( (p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y) ) ; } double solve(double mid) { int i , j , id , u ; double ans = 0 , min1 ; memset(vis,0,sizeof(vis)) ; for(i = 0 ; i < n ; i++) dis[i] = INF ; vis[0] = 1 ; dis[0] = 0 ; u = 0 ; for( i = 1 ; i < n ; i++ ) { min1 = INF ; id = 0 ; for(j = 0 ; j < n ; j++) { if( vis[j] ) continue ; dis[j] = min( dis[j],f(u,j,mid) ) ; if( min1-dis[j] >= eqs ) { min1 = dis[j] ; id = j ; } } ans += min1 ; vis[id] = 1 ; u = id ; } return ans ; } int main() { int i , j ; double temp , max1 , min1 ; while( scanf("%d", &n) && n ) { low = high = mid = 0.0 ; max1 = 0 ; min1 = INF ; for(i = 0 ; i < n ; i++) { scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].z) ; min1 = min(min1,p[i].z) ; max1 = max(max1,p[i].z) ; } high = (max1-min1)*n ; while( high - low > eqs) { mid = (low + high) / 2.0 ; temp = solve(mid) ; if( fabs(temp) < eqs ) break ; if( temp < 0 ) high = mid ; else low = mid ; } printf("%.3f\n", mid) ; } return 0 ; }
标签:
原文地址:http://blog.csdn.net/winddreams/article/details/46013191