poj 2728 Desert King 01分数规划

本文通过一个具体的POJ题目(ID:2728),介绍了如何使用01分数规划来解决特定类型的问题。文章提供了完整的C++代码实现,并详细解释了算法流程,包括初始化、迭代更新阈值直至收敛的过程。

题目大意:

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\n",ans);
    }

    getchar();getchar();
    return 0;
}

转载于:https://www.cnblogs.com/Skyminer/p/6475840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值