题意
多组测试数据,N等于0时结束。
有N个村庄,然后N行给你每个村庄的坐标(x,y)和海拔z,连接两个村庄需要修欧几里得距离len[i][j]的路,连接海拔不同的村庄需要付出海拔差的花费cost[i][j],现在要使得他们连通起来,求最小单位花费(即最小)
题解
01分数规划问题。把cost[i][j] - x*len[i][j]看成边权,然后求最小生成树即可。二分的下界显然是0,上界比较迷(100就能过)?
精度至少要1e-6才能AC。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;
int n;
double cost[MAXN][MAXN], len[MAXN][MAXN], G[MAXN][MAXN], dis[MAXN];
bool vis[MAXN];
struct Node
{
int x, y, z;
}a[MAXN];
double Distance(int i, int j) { return sqrt(pow(a[i].x-a[j].x, 2) + pow(a[i].y-a[j].y, 2)); }
double prim()
{
for (int i = 0; i < n; i++) dis[i] = G[0][i];
memset(vis, 0, sizeof(vis));
dis[0] = 0; vis[0] = true;
double ans = 0;
for (int i = 0; i < n-1; i++)
{
double MIN = INF; int pos;
for (int j = 0; j < n; j++)
if (!vis[j] && dis[j] < MIN) MIN = dis[pos=j];
vis[pos] = true;
ans += dis[pos];
for (int j = 0; j < n; j++)
if (!vis[j] && G[pos][j] < dis[j]) dis[j] = G[pos][j];
}
return ans;
}
bool check(double x)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
G[i][j] = cost[i][j] - x*len[i][j];
}
}
return prim() >= 0;
}
int main()
{
while(~scanf("%d", &n) && n)
{
for (int i = 0; i < n; i++) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cost[i][j] = fabs(a[i].z-a[j].z);
len[i][j] = Distance(i, j);
}
}
double l = 0, r = 100;
while (r-l > eps)
{
double mid = (l+r)/2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%.3f\n", l);
}
return 0;
}
/*
4
0 0 0
0 1 1
1 1 2
1 0 3
0
*/