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;
}
原文地址:https://www.cnblogs.com/Skyminer/p/6475840.html