zoukankan      html  css  js  c++  java
  • 【知识点】拉格朗日乘数法

    简介:

    在满足约束条件$varphi(x_1 ,x_2 ,cdots ,x_n )=0$时求$f(x_1 ,x_2 ,cdots ,x_n )$的极值。

    结论:

    令$L(x_1 ,x_2 ,cdots ,x_n )=f(x_1 ,x_2 ,cdots ,x_n )+lambda varphi(x_1 ,x_2 ,cdots ,x_n )$。

    再令$frac{partial L}{partial {x_i}}$为L关于$x_i$的偏导数。(即视其他$x_j$为常量,仅对$x_i$求导)

    则方程组

    $egin{cases}frac{partial L}{partial {x_1}}=0\frac{partial L}{partial {x_2}}=0\ cdots \ frac{partial L}{partial {x_n}}=0 \ varphi(x_1 ,x_2 ,cdots ,x_n )=0end{cases}$

    的某个解$(lambda_{k},x_{k,1} ,x_{k,2} ,cdots ,x_{k,n} )$一定对应着f的极值点。

    实现:

    先通过枚举/二分/三分确定$lambda$,然后根据题意算出一组合法解更新答案即可。

    代码(NOI2012骑行川藏):

    #include<bits/stdc++.h>
    #define maxn 200005
    #define maxm 500005
    #define inf 0x7fffffff
    #define eps 1e-14
    #define ll long long
    #define rint register int
    #define debug(x) cerr<<#x<<": "<<x<<endl
    #define fgx cerr<<"--------------"<<endl
    #define dgx cerr<<"=============="<<endl
    
    using namespace std;
    struct road{double s,k,v;}A[maxn];
    int n; double E;
    
    inline int read(){
        int x=0,f=1; char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    
    inline int dcmp(double x){return (abs(x)<=eps)?0:(x<0?-1:1);}
    inline double phi(double lam){
        double res=0;
        for(int i=1;i<=n;i++){
            double l=max(A[i].v,0.0),r=1e5;
            while(dcmp(r-l)>0){
                double mid=(l+r)/2.0;
                if(dcmp(2.0*lam*A[i].k*(mid-A[i].v)*mid*mid-1.0)<=0) l=mid; else r=mid;
                //printf("%.8lf %.8lf %.8lf
    ",2.0*lam*A[i].k*(mid-A[i].v)*mid*mid-1.0,l,r);
            }
            res+=A[i].k*(l-A[i].v)*(l-A[i].v)*A[i].s;
        }
        //printf("%lf
    ",res-E);
        return res-E;
    }
    inline double calc(double lam){
        double res=0;
        for(int i=1;i<=n;i++){
            double l=max(A[i].v,0.0),r=1e5;
            while(dcmp(r-l)>0){
                double mid=(l+r)/2.0;
                if(dcmp(2*lam*A[i].k*(mid-A[i].v)*mid*mid-1)<=0) l=mid; else r=mid;
            }
            res+=A[i].s/l;
        }
        return res;
    }
    
    
    int main(){
        //freopen("bicycling17.in","r",stdin);
        n=read(),scanf("%lf",&E);
        //cout<<n<<":"<<E<<endl;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf",&A[i].s,&A[i].k,&A[i].v);
        double l=0,r=1e5;
        while(dcmp(r-l)>0){
            double mid=(l+r)/2.0;
            if(dcmp(phi(mid))>=0) l=mid; else r=mid;
        }
        //printf("%lf %lf
    ",l,r);
        printf("%.6lf
    ",calc(l));
        return 0;
    }
    骑行川藏
  • 相关阅读:
    C# 删除文件夹
    XML操作类
    C# winform 安装程序打包(自定义操作)
    复制Datatable结构和数据,并按条件进行筛选
    Sql_Case_When
    C# using 与Trycatchfinally的区别和用法
    Winform datagridview Excel 导入导出
    矩阵树定理学习笔记
    minmax容斥笔记及例题
    平衡树学习笔记
  • 原文地址:https://www.cnblogs.com/YSFAC/p/13292505.html
Copyright © 2011-2022 走看看