POJ 2728 Desert King 0-1分数规划

题意:链接

方法: 0-1分数规划

解析:

这是之前没填的坑,现在来填坑。

这道题题意就是n个三维坐标系的点,任意两点都可以连边,每条边的花费是两点在xOy坐标系下的欧几里得距离,每条边的收益是两点的z值差的绝对值。

n个点连成一棵树

求最小的花费比收益。

即求最大的收益比花费。

一看求的东西就可以考虑0-1分数规划咯?

所以二分那个L,然后每条边的get-L*cost就是每条边新的权值,因为要拿最大的n-1条,所以上MST,但是这题是n^2的边,kruscal的话是n^2logn^2*log(二分)应该是T死的。

所以说稠密图上prim嘛。

每次拿值就行辣。

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1010
#define eps 1e-6
#define INF 0x3f3f3f3f
using namespace std;
int n;
double top,bot;
struct point
{
    double x,y,z;
}pt[N];
struct line
{
    int x,y;
    double get,cost,l;
}l[N*N];
int cmp(line a,line b)
{
    return a.l>b.l;
}
double thedis[N];
int theclo[N];
double function[N][N];
double get[N][N];
double cost[N][N];
double prim(int u)
{
    double ret=0;
    for(int i=1;i<=n;i++)
    {
        if(i==u)thedis[i]=-1,theclo[i]=-1;
        else thedis[i]=function[u][i],theclo[i]=u;
    }
    for(int i=1;i<n;i++)
    {
        double val=-10000000;
        int no;
        for(int j=1;j<=n;j++)
        {
            if(theclo[j]!=-1&&thedis[j]>val)val=thedis[j],no=j;
        }
        ret+=thedis[no];
        top+=get[theclo[no]][no];
        bot+=cost[theclo[no]][no];
        theclo[no]=-1;
        for(int j=1;j<=n;j++)
        {
            if(theclo[j]!=-1&&function[no][j]>thedis[j])thedis[j]=function[no][j],theclo[j]=no;
        }
    }
    return ret;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)scanf("%lf%lf%lf",&pt[i].x,&pt[i].y,&pt[i].z);
        int linetot=0;
        double l=0,r=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)continue;
                cost[i][j]=fabs(pt[i].z-pt[j].z);
                get[i][j]=sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y));
                r=max(r,get[i][j]/cost[i][j]);
            }
        }
        double ans=0;
        while(r-l>eps)
        {
            double mid=(l+r)/2.0;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i==j)continue;
                    function[i][j]=get[i][j]-mid*cost[i][j];
                }
            }
            top=0,bot=0;
            double ret=prim(1);
            if(ret-eps>=0)ans=max(ans,top/bot),l=mid+eps;
            else r=mid-eps;
        }
        printf("%.3lf\n",1.0/ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 08:58:06

POJ 2728 Desert King 0-1分数规划的相关文章

[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

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

http://poj.org/problem?id=2728 大致题意:有n个村庄,输入每个村庄的位置和高度,这n个村庄要连在一起,村与村之间的长度为他们之间的欧几里得距离,花费是两村之间的高度差,要求连在一起的花费和与距离和之比的最小值. 思路:明显的最优比率生成树.二分答案λ,每条边重新赋权c[i] - λd[i] ,因为要求比值最小,那么对于所有的生成树,它们的f[λ]必须>=0,所以只需求得基于最小生成树的f'[λ],当f'[λ] = 0时即找到了正解λ*. 二分: #include <

poj 2728 Desert King(最小比率生成树,迭代法)

引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求方案使得费用与距离的比值最小 很显然,这个题目是要求一棵最优比率生成树, 概念 有带权图G, 对于图中每条边e[i], 都有benifit[i](收入)和cost[i](花费), 我们要求的是一棵生成树T, 它使得 ∑(benifit[i]) / ∑(cost[i]), i∈T 最大(或最小). 这

poj 2728 Desert King

Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       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 t

POJ - 2976 Dropping tests &amp;&amp; 0/1 分数规划

POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} b_{i}}\] 你可以考虑放弃 \(K\) 次成绩, 求最大平均成绩 * 100 小插曲: 被精度卡成喜羊羊 0/1分数规划\(from\)人生导师 Solution 01分数规划(不是很)裸题, 在每次 \(check\) 时, 选取较大的 \(num - K + 1\) 次即可 Code #

POJ 2728 Desert King (01分数规划)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 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 count

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(最优比率生成树 01分数规划)

http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简单,因为这题是稠密图,所以用prim算法会好点. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector>

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不可能是最小.这样我