zoukankan      html  css  js  c++  java
  • poj 2728 Desert King 01分数规划

    题目大意:

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

    题解:

    裸的01分数规划

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    inline void read(int &x){
    	x=0;char ch;bool flag = false;
    	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
    }
    const int maxn = 1560;
    const double eps = 1e-9;
    struct Point{
    	double x,y,z;
    }p[maxn];
    inline double dis(const Point &a,const Point &b){
    	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    double d[maxn],cost[maxn],length[maxn];
    bool vis[maxn];
    int main(){
    	int n;
    	while(1){
    		read(n);if(n == 0) break;
    		for(int i=1;i<=n;++i){
    			scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
    		}
    		double L = .0,ans = .0;
    		while(1){
    			memset(vis,0,sizeof vis);
    			vis[1] = true;
    			for(int i=2;i<=n;++i){
    				cost[i] = fabs(p[1].z-p[i].z);
    				length[i] = dis(p[1],p[i]);
    				d[i] = cost[i] - L*length[i];
    			}
    			int nw = 1;double f_up=.0,f_dn=.0;
    			while(nw < n){
    				double tmp = 1e10;int pos = -1;
    				for(int i=2;i<=n;++i){
    					if(vis[i]) continue;
    					if(tmp > d[i]){
    						tmp = d[i];
    						pos = i;
    					}
    				}
    				vis[pos] = true;++nw;
    				f_up += cost[pos];f_dn += length[pos];
    				for(int i=2;i<=n;++i){
    					if(vis[i]) continue;
    					double co = fabs(p[i].z - p[pos].z);
    					double len = dis(p[i],p[pos]);
    					if(co - L*len < d[i]){
    						d[i] = co - L*len;
    						cost[i] = co;
    						length[i] = len;
    					}
    				}
    			}
    			ans = L;L = f_up/f_dn;
    			if(fabs(ans - L) < eps) break;
    		}
    		printf("%.3lf
    ",ans);
    	}
    
    	getchar();getchar();
    	return 0;
    }
    
  • 相关阅读:
    [剑指offer] 42. 和为S的两个数字
    [剑指offer] 41. 和为S的连续正数序列
    datagridview bindingsource刷新数据
    datagridview bindingsource
    slot-具名插槽
    基于uniapp的ui库
    组件之间通讯
    vue实现word或pdf文档导出的功能
    Vue之富文本tinymce爬坑录
    12道vue高频原理面试题,你能答出几道?
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6475840.html
Copyright © 2011-2022 走看看