题干:
David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.
After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.
His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line.
As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.
Input
There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.
Output
For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
题目大意:
有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目是要求一棵最优比率生成树。
一句话题意:
解题报告:
除了二分,还有一种算法叫Dinkelbach算法
每次将r'代入z函数中计算以后,我们将得到一组x
让r''=(∑(ci*xi))/(∑(di*xi))
当r''=r'时,r''就是我们需要的解
否则将r'=r'',继续迭代
这种方法比二分法要快一点
AC代码:(迭代法)
using namespace std;
const int MAX = 2e5 + 5;
int n;
const double eps = 1e-5;
struct Node {
double x,y,z;
} node[MAX];
struct NN {
int u,v;
double x,y;
} ee[20 * MAX];
struct Edge {
int u,v;
double x,y,w;
} e[20 * MAX];
int f[MAX];
int tot;
bool cmp(Edge a,Edge b) {
return a.w < b.w;
}
int getf(int v) {
return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {
int t1 = getf(u);
int t2 = getf(v);
f[t2] = t1;
}
void init() {
for(int i = 1; i<=n; i++) f[i] = i;
}
double kls() {
sort(e+1,e+tot+1,cmp);
init();
int cnt = 0;
double xx = 0,yy = 0;
for(int i = 1; i<=tot; i++) {
if(getf(e[i].u) != getf(e[i].v)) {
xx += e[i].x;
yy += e[i].y;
cnt ++;
merge(e[i].u,e[i].v);
if(cnt == n - 1) break;
}
}
return xx/yy;
}
double ok(double mid) {
for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].x = ee[i].x,e[i].y = ee[i].y,e[i].w = ee[i].x - mid*ee[i].y;
double res = kls();
return res;
}
int main()
{
while(~scanf("%d",&n)) {
if(n == 0) break;
tot = 0;
//init()
for(int i = 1; i<=n; i++) f[i] = i;
for(int i = 1; i<=n; i++) {
scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);
}
for(int i = 1; i<=n; i++) {
for(int j = 1; j<i; j++) {
ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));
ee[tot].x = fabs(node[i].z - node[j].z);
ee[tot].u = i;ee[tot].v = j;
}
}
double l = 0,r = 1e9;
// double mid = (l+r)/2,ans=mid;
// while(l+eps < r) {
// mid = (l+r)/2;
// if(ok(mid)) r = mid;
// else l = mid;
// }
double mid = (l+r)/2,ans = -1;
while(fabs(ans-mid) >= eps) {
ans = mid;
mid = ok(mid);
}
printf("%.3f\n",ans - eps);
}
return 0 ;
}
TLE代码:(二分)
using namespace std;
const int MAX = 2e5 + 5;
int n;
const double eps = 1e-5;
struct Node {
double x,y,z;
} node[MAX];
struct NN {
int u,v;
double x,y;
} ee[20 * MAX];
struct Edge {
int u,v;
double w;
} e[20 * MAX];
int f[MAX];
int tot;
bool cmp(Edge a,Edge b) {
return a.w < b.w;
}
int getf(int v) {
return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {
int t1 = getf(u);
int t2 = getf(v);
f[t2] = t1;
}
void init() {
for(int i = 1; i<=n; i++) f[i] = i;
}
double kls() {
sort(e+1,e+tot+1,cmp);
init();
int cnt = 0;
double res = 0;
for(int i = 1; i<=tot; i++) {
if(getf(e[i].u) != getf(e[i].v)) {
res += e[i].w;
cnt ++;
merge(e[i].u,e[i].v);
if(cnt == n - 1) break;
}
}
return res;
}
bool ok(double mid) {
for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].w = ee[i].x - mid*ee[i].y;
double res = kls();
if(res <=0) return 1;
else return 0 ;
}
int main()
{
while(~scanf("%d",&n)) {
if(n == 0) break;
tot = 0;
init();
for(int i = 1; i<=n; i++) {
scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);
}
for(int i = 1; i<=n; i++) {
for(int j = 1; j<i; j++) {
ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));
ee[tot].x = fabs(node[i].z - node[j].z);
ee[tot].u = i;ee[tot].v = j;
}
}
double l = 0,r = 1e9;
double mid = (l+r)/2;
while(l+eps < r) {
mid = (l+r)/2;
if(ok(mid)) r = mid;
else l = mid;
}
printf("%.3f\n",mid - eps);
}
return 0 ;
}
AC代码: