【POJ2728】Desert King 最优比率生成树

题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小。

题解:要求的答案可以看成是 0-1 分数规划问题,即:选定一个数 mid,每次重新构建边权为 \(a[i]-mid*b[i]\) 的图,再在图上跑一遍最小生成树(这里由于是完全图,应该采用 Prim 算法)判断最小值和给定判定的最小值的关系即可,这里为:若最小值大于 mid,则下界提高,否则上界下降。

代码如下

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1010;
const double eps=1e-5;

int n;bool vis[maxn];
double cost[maxn][maxn],d[maxn][maxn],mp[maxn][maxn],x[maxn],y[maxn],h[maxn],mx,s[maxn];

inline double calc(int i,int j){
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

void read_and_parse(){
    for(int i=1;i<=n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&h[i]);
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            cost[i][j]=cost[j][i]=fabs(h[i]-h[j]);
            d[i][j]=d[j][i]=calc(i,j);
            mx=max(mx,cost[i][j]/d[i][j]);
        }
}

double prim(){
    fill(vis+1,vis+n+1,0);
    double res=0;
    s[1]=0,vis[1]=1;
    for(int i=1;i<=n;i++)s[i]=mp[1][i];
    for(int i=1;i<n;i++){
        int u=0;
        for(int j=1;j<=n;j++)if(!vis[j]&&(!u||s[j]<s[u]))u=j;
        vis[u]=1,res+=s[u];
        for(int v=1;v<=n;v++)if(!vis[v])s[v]=min(s[v],mp[u][v]);
    }
    return res;
}

bool check(double mid){
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            mp[i][j]=mp[j][i]=cost[i][j]-mid*d[i][j];
    return prim()>0;
}

void solve(){
    double l=0,r=mx;
    while(r-l>eps){
        double mid=(l+r)/2.0;
        if(check(mid))l=mid;
        else r=mid;
    }
    printf("%.3lf\n",l);
}

int main(){
    while(scanf("%d",&n)&&n){
        read_and_parse();
        solve();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10076941.html

时间: 2024-10-11 09:59:21

【POJ2728】Desert King 最优比率生成树的相关文章

POJ 2728 Desert King (最优比率生成树---01分数规划)

题目地址:POJ 2728 01分数规划的应用之一-最优比率生成树. 跟普通的01分数规划类似,只是这题的验证函数改成了最小生成树来验证.弱用的迭代法. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map>

【POJ】【2728】 Desert King 最优比率生成树

题意:给出每个点的坐标(x,y,z),两点间距离是x,y的直线距离,边权为z差,求∑边权 / ∑距离 的最小值. 最优比率生成树!(分数规划) 就是根据分数规划的思想建树,每次看得到的总和是正是负. 二分代码: #include<string.h> #include<stdio.h> #include<stdlib.h> #include<math.h> #define N 1010 typedef struct KSD { int x,y,z; }ksd;

POJ 2728 Desert King 最优比率生成树

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Description] 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 co

ZOJ 2728 Desert King(最优比例生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20586   Accepted: 5777 Description 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 coun

POJ 2728 Desert King 最优比例生成树

最优比例生成树,迭代方法..... Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21086   Accepted: 5916 Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels

poj-2728Desert King(最优比率生成树)

题目连接: Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 23729   Accepted: 6631 Description 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 hi

poj 2728(Desert King) 最优比例生成树 (分数规划)

这题是最小生成树的一种扩展,就是求一棵树  使得 k =sigma(cost)/ sigma(len)  最小.   其中cost 为每条边花费,len为长度.  这实际上就是一个最优比例生成树.最优比例生成树的求解使用了分数规划的方法.  我们先任取k,假设k是最小值,那么sigma(ccost)-k*sigma(len)==0  .那么我们就新建图边权 为  ccosti-k*leni  .求一次最小生成树,如果生成树权值小于0,那么书名其实k还是有减小的空间的,否则,k不可能是最小.这样我

【最优比率生成树】poj2728 Desert King

最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能用Kruscal,因为是完全图 Code: 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struc

[POJ 2728]Desert King(0-1分数规划/最优比率生成树)

Description 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