poj2728 Desert King——01分数规划

题目:http://poj.org/problem?id=2728

第一道01分数规划题!(其实也蛮简单的)

这题也可以用迭代做(但是不会),这里用了二分;

由于比较裸,不作过多说明了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-6
using namespace std;
int const inf=0x3f3f3f;
int n;
double d[1005][1005],h[1005][1005],w[1005][1005],dis[1005];
double xx[1005],yy[1005],zz[1005];
bool vis[1005];
void add(int x,int y)
{
    double ds=sqrt((xx[x]-xx[y])*(xx[x]-xx[y])+(yy[x]-yy[y])*(yy[x]-yy[y]));
    d[x][y]=d[y][x]=ds;
    h[x][y]=h[y][x]=fabs(zz[x]-zz[y]);
}
double prim(double mid)
{
    double s=0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            w[i][j]=w[j][i]=h[i][j]-mid*d[i][j];
    for(int i=0;i<n;i++)vis[i]=0,dis[i]=inf;
    dis[0]=0;
    while(1)
    {
        int u=-1;
        for(int i=0;i<n;i++)
            if(!vis[i]&&(u==-1||dis[i]<dis[u]))u=i;
        if(u==-1)break;
        s+=dis[u];vis[u]=1;
        for(int i=0;i<n;i++)
            if(!vis[i])dis[i]=min(dis[i],w[u][i]);
    }
    return s;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(!n)return 0;
        for(int i=0;i<n;i++)
            scanf("%lf%lf%lf",&xx[i],&yy[i],&zz[i]);
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                add(i,j);
        double l=0,r=100.00;//
        while(r-l>eps)
        {
            double mid=(l+r)/2;
            if(prim(mid)<eps)r=mid;//
            else l=mid;
        }
        printf("%.3lf\n",r);
    }
}

原文地址:https://www.cnblogs.com/Zinn/p/8948349.html

时间: 2024-10-08 01:51:01

poj2728 Desert King——01分数规划的相关文章

poj2728 Desert King --- 01分数规划 二分水果。。

这题数据量较大,普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; #define N 1010 double mp[N][N],c[N][N],x

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 0-1分数规划

题意:链接 方法: 0-1分数规划 解析: 这是之前没填的坑,现在来填坑. 这道题题意就是n个三维坐标系的点,任意两点都可以连边,每条边的花费是两点在xOy坐标系下的欧几里得距离,每条边的收益是两点的z值差的绝对值. n个点连成一棵树 求最小的花费比收益. 即求最大的收益比花费. 一看求的东西就可以考虑0-1分数规划咯? 所以二分那个L,然后每条边的get-L*cost就是每条边新的权值,因为要拿最大的n-1条,所以上MST,但是这题是n^2的边,kruscal的话是n^2logn^2*log(

$POJ$2728 $Desert\ King$ 01分数规划

正解:01分数规划 解题报告: 传送门! 感觉挺板子的,,, 因为还没做,先瞎口胡一个做法$QAQ$ 如果翻车了请当做没看见我如果错了等$get$正确做法会重新编辑的$QAQ$ 就因为有$n\leq 100$,于是如果把它联成一个完全图边数连$1e4$都没有 所以考虑直接连成一个完全图,就成了之前寒假考试题二分专题那次的$T1$辽,就直接无脑01分数规划+最大生成树就欧克 $over$? 原文地址:https://www.cnblogs.com/lqsukida/p/10827485.html

Desert King POJ2728(Prim+迭代+0-1分数规划)

[原题地址]: POJ2728 [题目大意]: 选 \(n-1\) 条边,最小化这些边的\[\frac{\sum w_i} {\sum d_i}\] 其中 \(w_i\) 为第 \(i\) 条边的花费,\(d_i\) 为这条边所连接的两个点的距离. \(w_i\) 为连接的两个点的 \(z\) 值差的绝对值, \(d_i\) 为欧几里得距离 [题解]: 这道题是0-1分数规划的经典题目 我们令\[\frac{\sum w_i} {\sum d_i}=k\] 这样二分 \(k\) 的值 将所有的边

[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

Desert King (poj 2728 最优比率生成树 0-1分数规划)

Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   Accepted: 6187 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 2728 Desert King(最优比率生成树,01分数规划)

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

Desert King(01分数规划问题)(最优斜率生成树)

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